本文共 1613 字,大约阅读时间需要 5 分钟。
首先做一个预设,假设命名为line,
添加LineRender组件,添加材质,让其可以正常显示
把line预设保存在Assets/Resources目录中
接下来创建一个LineRenderTest.cs脚步,挂到摄像机上。
脚本如下:
using UnityEngine;using System.Collections;using Holoville.HOTween;public class LineRenderTest: MonoBehaviour { private LineRenderer m_lineRander; private Vector3 m_curPos; void Start() { // 加载资源 GameObject lineObjPrefab = Resources.Load("line",typeof(GameObject)) as GameObject; if (lineObjPrefab == null) { Debug.LogError("lineObjPrefab == null"); } // 实例化 GameObject lineObj = GameObject.Instantiate(lineObjPrefab) as GameObject; m_lineRander = lineObj.GetComponent(); if (m_lineRander != null) { // 设置起点和终点 m_lineRander.transform.localPosition = Vector3.zero; m_lineRander.SetVertexCount(2); m_lineRander.SetPosition(0, new Vector3(0, 0, 0)); m_lineRander.SetPosition(1, new Vector3(0, 0, 0)); startMove(); } } private void startMove() { // 使用HoTween对终点进行插值变换 TweenParms tp = new TweenParms(); tp.Prop("CurPos", new Vector3(50, 100, 100)); tp.Ease(EaseType.Linear); tp.SpeedBased(true); tp.OnComplete(moveEnd); HOTween.To(this, 100f, tp); } private void moveEnd() { Debug.Log("moveEnd"); } public Vector3 CurPos { get { return m_curPos; } set { m_curPos = value; if (m_lineRander != null) { m_lineRander.SetPosition(1, m_curPos); } } }}
搞定
转载地址:http://rrzvf.baihongyu.com/