Unity3D开发之利用LineRenderer画曲线
2013-10-30 14:59
351 查看
using UnityEngine; using System.Collections; using System.Collections.Generic; public class LinearDrawLine: MonoBehaviour { private int pointCnt = 0; public Color c1 = Color.red; public Color c2 = Color.blue; private LineRenderer lineRenderer; private Vector3 screenPoint; private Vector3 scanPos; private Vector3 first_pos = Vector3.zero; void Start () { scanPos = gameObject.transform.position; lineRenderer = (LineRenderer)gameObject.GetComponent("LineRenderer"); lineRenderer.material = new Material (Shader.Find("Particles/Additive")); lineRenderer.SetColors(c1, c2); lineRenderer.SetWidth(0.02F, 0.02F); lineRenderer.SetVertexCount(0); } void Update() { } void OnMouseDown(){ Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); screenPoint = Camera.main.WorldToScreenPoint(scanPos); Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); pointCnt = 0; DrawRenderLine(lineRenderer, curPosition); first_pos = curPosition; } void OnMouseDrag(){ iTween.ScaleFrom(gameObject,iTween.Hash("x",2,"y",2,"z",2,"time",.1,"easetype","linear")); Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); DrawRenderLine(lineRenderer, curPosition); } void DrawRenderLine(LineRenderer line, Vector3 vect3){ if((Mathf.Abs(first_pos.x - vect3.x) < 0.1) && (Mathf.Abs(first_pos.y - vect3.y) < 0.1)) return; first_pos = vect3; line.SetVertexCount(++pointCnt); line.SetPosition(pointCnt-1, vect3); print("new point: "+vect3+"+"+pointCnt); } }
这种方式相比于GL方式来说,它不需要添加到相机中,可以是点击某个物品来画图。
可以直接把这个脚本拉到物品上面就可以用了。
相关文章推荐
- 【分享】Community Question Answering Datasets(社区问答数据集)
- Unity3D开发之利用GL画曲线
- Unity3d截图保存到Android相册的实现
- 关于Unity -Vuforia -Android 开发 ,平台的搭建(极品菜鸟完整版)
- 简单的小程序演示Unity的三种依赖注入方式
- [unity3d]加载模型资源
- [unity3d]加载模型资源
- Unity 事件2
- Unity在一个场景中使用其他场景烘焙的物体
- Paper Notes: Empirical Comparison of Algorithms for Network Community Detection
- unity3d 常规性能优化技巧
- Unity3D游戏开发——Asset Server搭建
- unity实现折线图功能
- Unity 事件2
- 教你怎样用unity3d发布一个Android的apk文件.绝对精细
- Unity3d 如何找到游戏对象并改变其颜色
- unity3d的性能优化
- Unity (游戏引擎)
- [unity3d]链接mysql的Project build出错
- [unity3d]链接mysql的Project build出错