Unity:Effects(Particle、Trail、Line)
作者:互联网
案例1.用Line实现简易画图功能(可以选画笔颜色和粗细)
1)建一个Line预制体,和一个空物体来挂载脚本
2)思路:Line会自动将上一个点与下一个点连接起来,这样无论怎么画都是连接在一起的一条线。要实现每次按下鼠标都是一个新的线段,可以每次按下鼠标都实例化一个Line。写两个脚本,一个控制整个流程,一个挂载在Line上,控制Line的功能。
3)Line实现绘制线条功能
1 public class DrawLine : MonoBehaviour 2 { 3 public LineRenderer lr; 4 public List<Vector3> points; 5 private void Awake() //这里之所以在Awake里初始化不是在Start里,因为试验之后发现Start还未运行,另一个脚本已经需要调用points了。 6 { 7 lr = GetComponent<LineRenderer>(); 8 points = new List<Vector3>(); 9 } 10 public void Draw() 11 { 12 lr.positionCount = points.Count; 13 for (int i = 0; i < lr.positionCount; i++) 14 { 15 lr.SetPosition(i, points[i]); 16 } 17 } 18 }Line根据传过来的鼠标点进行绘制
4)空物体挂载控制总流程的脚本
1 public class NewLine : MonoBehaviour { 2 GameObject line; 3 GameObject lineCp; 4 bool isDraw; 5 Color color=Color.black; 6 float width=0.02f; 7 8 void Start () { 9 line = Resources.Load("Prefabs/Line") as GameObject; 10 } 11 12 void Update () { 13 if (Input.GetMouseButtonDown(0)) 14 { 15 isDraw = true; 16 lineCp = Instantiate(line); 17 } 18 if (Input.GetMouseButtonUp(0)) 19 isDraw = false; 20 if (isDraw) 21 { 22 Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 2); 23 pos = Camera.main.ScreenToWorldPoint(pos); 24 DrawLine dl = lineCp.GetComponent<DrawLine>(); 25 dl.points.Add(pos); 26 dl.lr.startColor = color; 27 dl.lr.endColor = color; 28 dl.lr.startWidth = width; 29 dl.lr.endWidth = width; 30 dl.Draw(); 31 } 32 } 33 private void OnGUI() 34 { 35 if (GUILayout.Button("红色")) 36 color = Color.red; 37 if (GUILayout.Button("黄色")) 38 color = Color.yellow; 39 if(GUILayout.Button("蓝色")) 40 color = Color.blue; 41 if (GUILayout.Button("绿色")) 42 color = Color.green; 43 if (GUILayout.Button("紫色")) 44 color = Color.magenta; 45 if (GUILayout.Button("黑色")) 46 color = Color.black; 47 if (GUILayout.Button("加粗")) 48 width+=0.02f; 49 if (GUILayout.Button("加细")) 50 width-=0.02f; 51 } 52 }实例化、鼠标点转换、按钮设置
标签:color,Button,GUILayout,Trail,Unity,lr,Effects,Line,Color 来源: https://www.cnblogs.com/tomatokely/p/15337507.html