查看: 878|回复: 1
打印 上一主题 下一主题

[其他] unity GUI绘制直线条

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-12-31 22:19:43 |只看该作者 |倒序浏览
  1. using UnityEngine;

  2. using System.Collections;

  3. using System;

  4. public class DrawLine : MonoBehaviour {

  5. public Vector2[] m_point;//特征点位置

  6. public Color m_lineColor;

  7. private static Texture2D m_texure;//最终渲染得到的带有折线的纹理

  8. public void InitCanvas(Vector2[] point, int width, int height) {

  9. m_point = point;

  10. m_texure = new Texture2D(width,height);

  11. }

  12. public IEnumerator Draw()

  13. {

  14. //清空纹理对象

  15. for (int i = 0; i < 100; i++)

  16. {

  17. for (int j = 0; j < 100; j++)

  18. {

  19. m_texure.SetPixel(i, j, Color.white);

  20. }

  21. }

  22. Vector2 currentPoint = m_point[0];

  23. for (int i = 1; i < m_point.Length; i++) {

  24. for (float j = 0; j < 1; j = j + 0.01f) {

  25. Vector2 temp = Vector2.Lerp(m_point[i-1],m_point[i],j);

  26. m_texure.SetPixel(Convert.ToInt32(temp.x),Convert.ToInt32(temp.y),m_lineColor);

  27. }

  28. currentPoint = m_point[i];

  29. }

  30. m_texure.Apply();

  31. yield return m_texure;

  32. }

  33. void OnPostRender()

  34. {

  35. StartCoroutine(Draw());

  36. }

  37. void Start() {

  38. InitCanvas(m_point, 100, 100);

  39. }

  40. void OnGUI()

  41. {

  42. GUI.DrawTexture(new Rect(0, 0, 100, 100), m_texure);

  43. }

  44. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

16

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
3
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2015-1-3 11:11:10 |只看该作者
好代码,不错, 学习了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-29 06:08 , Processed in 0.080849 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部