Unity 使用LineRenderer 画箭头
2016-02-16 15:42
417 查看
using UnityEngine; using System.Collections; public class LineRendererScript : MonoBehaviour { [SerializeField] private LineRenderer BodyLineRenderer; [SerializeField] private LineRenderer HeadLineRenderer; private bool IsFinished = false; private bool IsReady = false; private Vector3 StartPos; private Vector3 EndPos; private float zDepth = 10f; private float headWidth = 0.4f; private float bodyWidth = 0.3f; void Awake() { if(BodyLineRenderer == null) BodyLineRenderer = GetComponent<LineRenderer>(); } void Update() { if(IsFinished) return; if(Input.GetMouseButtonDown(0)) { IsReady = true; StartPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); StartPos.z = zDepth; } else if(Input.GetMouseButtonUp(0)) { IsReady = false; IsFinished = true; } if(IsReady) { EndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); EndPos.z = zDepth; BodyLineRenderer.SetPosition(0, StartPos); BodyLineRenderer.SetPosition(1, EndPos); // 设置头部 Vector3 HeadStart = EndPos; Vector3 HeadEnd = HeadStart; Vector3 direction = EndPos - StartPos; // 根据当前线的长短,适当的调整箭头的大小 if(direction.magnitude > 2) { headWidth = 0.4f; bodyWidth = 0.2f; } else if(direction.magnitude > 1) { headWidth = 0.3f; bodyWidth = 0.15f; } else { headWidth = 0.2f; bodyWidth = 0.1f; } HeadLineRenderer.SetWidth(headWidth, 0); BodyLineRenderer.SetWidth(0, bodyWidth); direction.Normalize(); HeadEnd = HeadEnd + direction * headWidth; HeadLineRenderer.SetPosition(0, HeadStart); HeadLineRenderer.SetPosition(1, HeadEnd); } } }
只需要在场景重创建一个对象,然后为对象创建两个子对象,两个子对象都拥有组件 LineRenderer即可.....
相关文章推荐
- [Unity3d]水果忍者-声音和刀光的实现
- unity3d 可见射线
- LineRenderer组件
- Unity Shader 学习笔记(十四)使用屏幕特效实现亮度饱和度和对比度
- 【Unity3D】 Unity3D中自带事件函数的执行顺序
- 【Unity】unity 枚举分隔符
- Unity3D 封装自己的Debug类库dll
- 开始写博客啦!!
- unity3d 导出NGUI图集中的图片
- DrawCall的简单介绍
- 【Unity插件】NGUI核心组件之UICamera
- 【Unity插件】NGUI核心组件之UIAtlas
- 【Unity插件】NGUI核心组件之UIAnchor
- 【Unity插件】NGUI核心组件之UIPanel
- Unity3D在后台打包的方法
- Unity中HideInInspector和SerializeField
- Unity-Animator深入系列---fullPathHash和shortNameHash
- Unity3d UGUI序列帧动画
- Unity3D 播放MP4 的几种状态
- Unity编辑器扩展