1 using UnityEngine; 2 using UnityEngine.Timeline; 3 using UnityEngine.Playables; 4 5 [System.Serializable] 6 public class LinePlayableAsset : PlayableAsset 7 { 8 public ExposedReference<Transform> m_StartObj; 9 public ExposedReference<Transform> m_EndObj; 10 public ExposedReference<LineRenderer> m_Line; 11 public Color m_LineColr = Color.red; 12 13 public override Playable CreatePlayable(PlayableGraph graph, GameObject go) 14 { 15 var bhr= ScriptPlayable<LineBhr>.Create(graph); 16 bhr.GetBehaviour().m_StartObj = m_StartObj.Resolve(graph.GetResolver()); 17 bhr.GetBehaviour().m_EndObj = m_EndObj.Resolve(graph.GetResolver()); 18 bhr.GetBehaviour().m_EndObj = m_EndObj.Resolve(graph.GetResolver()); 19 bhr.GetBehaviour().m_Line = m_Line.Resolve(graph.GetResolver()); 20 bhr.GetBehaviour().m_LineColr = m_LineColr; 21 return bhr; 22 } 23 } 24 25 public class LineBhr:PlayableBehaviour,ITimelineClipAsset 26 { 27 public Transform m_StartObj; 28 public Transform m_EndObj; 29 public Color m_LineColr = Color.red; 30 public LineRenderer m_Line; 31 32 33 34 public override void OnBehaviourPlay(Playable playable, FrameData info) 35 { 36 base.OnBehaviourPlay(playable, info); 37 LineParameterSetting(); 38 } 39 40 void LineParameterSetting() 41 { 42 if (m_Line != null) 43 { 44 //设置材质 45 m_Line.material = new Material(Shader.Find("Legacy Shaders/Particles/Additive")); 46 m_Line.sharedMaterial.color = m_LineColr; 47 //设置颜色 48 m_Line.startColor = m_LineColr; 49 m_Line.endColor = m_LineColr; 50 // m_Line.useWorldSpace = false; 51 52 53 //设置宽度 54 m_Line.startWidth = 0.05f; 55 m_Line.endWidth = 0.05f; 56 } 57 else 58 { 59 Debug.LogError("Line is Null"); 60 return; 61 } 62 } 63 64 public override void ProcessFrame(Playable playable, FrameData info, object playerData) 65 { 66 base.ProcessFrame(playable, info, playerData); 67 if (m_Line != null) 68 { 69 var curTime = (float) (playable.GetTime()/ playable.GetDuration()); 70 var endPos = Vector3.Lerp(m_StartObj.position, m_EndObj.position, curTime); 71 m_Line.SetPosition(0, m_StartObj.position); 72 m_Line.SetPosition(1, endPos); 73 } 74 else 75 { 76 Debug.LogError("Line is Null"); 77 } 78 } 79 80 public ClipCaps clipCaps 81 { 82 get { return ClipCaps.None; } 83 } 84 }
上面一部分是划线逻辑,下面是Track设置
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.Timeline; 5 using UnityEngine.Playables; 6 using UnityEngine.Timeline; 7 8 [System.Serializable] 9 [TrackClipType(typeof(LinePlayableAsset))] 10 [TrackColor(0.8f, 0.2f, 0.5f)] 11 12 public class LineTrack : TrackAsset 13 { 14 public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount) 15 { 16 return ScriptPlayable<LineMixerBhr>.Create(graph, inputCount); 17 } 18 } 19 20 public class LineMixerBhr:PlayableBehaviour 21 { 22 23 }
浙公网安备 33010602011771号