unity--鼠标画线、画图实现

http://blog.csdn.net/chenggong2dm/article/details/24488469


首先让我们来看一下效果:


实现方法:

1.首先生成一个GameObject物体,在其中通过Component-->effects-->LineRender添加组件

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class TextNGUI : MonoBehaviour {  
  5.   
  6.     /// <summary>  
  7.     /// 鼠标画图功能  
  8.     /// </summary>  
  9.     private GameObject clone;  
  10.     private LineRenderer line;  
  11.     private int i;  
  12.     public GameObject tf;  
  13.     void Start () {  
  14.       
  15.     }  
  16.       
  17.     // Update is called once per frame  
  18.     void Update () {  
  19.         if (Input.GetMouseButtonDown(0))  
  20.         {  
  21.   
  22.             clone = (GameObject)Instantiate(tf, tf.transform.position, transform.rotation);//克隆一个带有LineRender的物体  
  23.             //clone.gameObject.GetComponent<LineRendersTest>().enabled=false;  
  24.             //clone.GetComponent<LineRenderer>().enabled=true;  
  25.             line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件  
  26.             line.SetColors(Color.blue, Color.red);//设置颜色  
  27.             line.SetWidth(0.2f, 0.1f);//设置宽度  
  28.             i = 0;  
  29.         }  
  30.         if (Input.GetMouseButton(0))  
  31.         {  
  32.   
  33.             i++;  
  34.             line.SetVertexCount(i);//设置顶点数  
  35.             line.SetPosition(i - 1, Camera.mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//设置顶点位置  
  36.             //line.enabled=false;  
  37.         }  
  38.     }  
  39. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值