纳金网

标题: Unity3D开发之利用LineRenderer画曲线 [打印本页]

作者: 狂风大尉    时间: 2015-2-28 23:38
标题: Unity3D开发之利用LineRenderer画曲线

Unity3D开发之利用LineRenderer画曲线
  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Collections.Generic;

  4. public class LinearDrawLine: MonoBehaviour {

  5. private int pointCnt = 0;

  6. public Color c1 = Color.red;

  7. public Color c2 = Color.blue;

  8. private LineRenderer lineRenderer;

  9. private Vector3 screenPoint;

  10. private Vector3 scanPos;

  11. private Vector3 first_pos = Vector3.zero;

  12. void Start () {

  13. scanPos = gameObject.transform.position;

  14. lineRenderer = (LineRenderer)gameObject.GetComponent(“LineRenderer”);

  15. lineRenderer.material = new Material (Shader.Find(“Particles/Additive”));

  16. lineRenderer.SetColors(c1, c2);

  17. lineRenderer.SetWidth(0.02F, 0.02F);

  18. lineRenderer.SetVertexCount(0);

  19. }

  20. void Update()

  21. {

  22. }

  23. void OnMouseDown(){

  24. Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

  25. screenPoint = Camera.main.WorldToScreenPoint(scanPos);

  26. Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

  27. Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);

  28. pointCnt = 0;

  29. DrawRenderLine(lineRenderer, curPosition);

  30. first_pos = curPosition;

  31. }

  32. void OnMouseDrag(){

  33. iTween.ScaleFrom(gameObject,iTween.Hash(“x”,2,“y”,2,“z”,2,“time”,.1,“easetype”,“linear”));

  34. Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

  35. Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);

  36. DrawRenderLine(lineRenderer, curPosition);

  37. }

  38. void DrawRenderLine(LineRenderer line, Vector3 vect3){

  39. if((Mathf.Abs(first_pos.x - vect3.x) < 0.1) && (Mathf.Abs(first_pos.y - vect3.y) < 0.1))

  40. return;

  41. first_pos = vect3;

  42. line.SetVertexCount(++pointCnt);

  43. line.SetPosition(pointCnt-1, vect3);

  44. print(“new point: ”+vect3+“+”+pointCnt);

  45. }

  46. }
复制代码





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