纳金网

标题: unity GUI绘制直线条 [打印本页]

作者: 王者再临    时间: 2014-12-31 22:19
标题: unity GUI绘制直线条
  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. }
复制代码

作者: 我不再年轻    时间: 2015-1-3 11:11
好代码,不错, 学习了!




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5