SignalTrack信号轨道和自定义带参数的Marker信号和轨道
MySignalReceiver
using System;
using System.ComponentModel;
using UnityEngine.Playables;
using UnityEngine.Events;
namespace UnityEngine.Timeline
{
public class BaseSignalReceiver<T, Q> : MonoBehaviour, INotificationReceiver
where T : MySignalAssetData where Q : BaseSignalEmitter<T>
{
[SerializeField] UnityEvent<T> m_Events = new UnityEvent<T>();
[SerializeField] UnityEvent<string> m_EventStrs = new UnityEvent<string>();
public virtual void OnNotify(Playable origin, INotification notification, object context)
{
if (notification is Q)
{
Q signalEmitter = (Q)notification;
InvokeMember(signalEmitter.mySignalAssetData);
}
}
public virtual void InvokeMember(T mySignalAssetData)
{
Debug.Log(mySignalAssetData.ToString());
m_Events?.Invoke(mySignalAssetData);
m_EventStrs?.Invoke(UnityEngine.JsonUtility.ToJson(mySignalAssetData));
}
public void AddEvent(UnityAction<T> action)
{
m_Events.AddListener(action);
}
public void RemoveEvent(UnityAction<T> action)
{
m_Events.RemoveListener(action);
}
public void AddEvent(UnityAction<string> action)
{
m_EventStrs.AddListener(action);
}
public void RemoveEvent(UnityAction<string> action)
{
m_EventStrs.RemoveListener(action);
}
}
public class BaseSignalEmitter<T> : Marker, INotification, INotificationOptionProvider
{
NotificationFlags INotificationOptionProvider.flags =>
NotificationFlags.TriggerOnce | NotificationFlags.TriggerInEditMode;
public PropertyName id { get; }
public T mySignalAssetData;
}
}
namespace UnityEngine.Timeline
{
[TrackBindingType(typeof(MySignalReceiver))]
[DisplayName("我的信号轨道")]
public class MySignalTrack : MarkerTrack
{
}
[DisplayName("我的信号接收器")]
public class MySignalReceiver : BaseSignalReceiver<MySignalAssetData, BaseSignalEmitter<MySignalAssetData>>
{
public override void OnNotify(Playable origin, INotification notification, object context)
{
base.OnNotify(origin, notification, context);
if (notification is MySignalEmitter_Variant)
{
MySignalEmitter_Variant signalEmitter = (MySignalEmitter_Variant)notification;
InvokeMember(signalEmitter.mySignalAssetData);
}
}
}
[DisplayName("我的信号")]
public class MySignalEmitter : BaseSignalEmitter<MySignalAssetData>
{
}
[Serializable]
[DisplayName("我的信号数据")]
public class MySignalAssetData
{
public string methodName;
public string msg;
public int msgType = 0;
public virtual string ToString()
{
return $"{methodName} {msg}";
}
}
[DisplayName("我的信号 变种1")]
public class MySignalEmitter_Variant : BaseSignalEmitter<MySignalAssetData_Variant>
{
}
[Serializable]
[DisplayName("我的信号数据 变种1")]
public class MySignalAssetData_Variant : MySignalAssetData
{
public string sex;
public int age;
public override string ToString()
{
return $"{methodName} {msg} {sex} {age}";
}
}
}
TimeLinePlayable_Ctrl
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
public class TimeLinePlayable_Ctrl : MonoBehaviour
{
public MySignalReceiver receiver;
private void Start()
{
receiver.AddEvent(Test_DataEvent);
receiver.AddEvent(Test_Msg);
}
public void Test_Msg(string parameter)
{
MySignalAssetData data = UnityEngine.JsonUtility.FromJson<MySignalAssetData>(parameter);
if (data.msgType == 1)
{
MySignalAssetData_Variant parameter2 = UnityEngine.JsonUtility.FromJson<MySignalAssetData_Variant>(parameter);
Debug.Log("TestStr:" + parameter2.ToString());
}
else
{
Debug.Log("TestStr:" + parameter.ToString());
}
}
public void Test_DataEvent(MySignalAssetData parameter)
{
if (parameter.msgType == 1)
{
MySignalAssetData_Variant parameter2 = parameter as MySignalAssetData_Variant;
Debug.Log("Test:" + parameter2.ToString());
}
else
{
Debug.Log("Test:" + parameter.ToString());
}
}
}
SignalTrack信号轨道和自定义带参数的Marker信号和轨道