using System; using UnityEngine; using UnityEngine.Playables; namespace Thousandto.Launcher.ExternalLibs { [Serializable] public class ScriptActivationBehaviour : PlayableBehaviour { //当前设置的是否激活 private bool _isActived = false; //当前对象的骄傲本 private MonoBehaviour _script; //当前关联的对象 private GameObject _go; //脚本名字 private string _scriptName; private object[] _msgData = new object[4]; //脚本名字 public string ScriptName { set { if (_scriptName != value) { _scriptName = value; _script = null; _go = null; } } } //发送消息 public bool NeedSendMessage { get; set; } //消息ID public int IntParam { get; set; } //消息内容 public string StrParam { get; set; } //设置当前脚本是否激活 public void SetActive(GameObject go, bool value) { if (_script == null) InitScript(go); if (_isActived != value) { _isActived = value; if (_script != null) { _script.enabled = _isActived; } } } //发送消息 public void SendMessage(double duration, double current) { if (NeedSendMessage) { if (_script != null) { _msgData[0] = IntParam; _msgData[1] = StrParam; _msgData[2] = current; _msgData[3] = duration; _script.SendMessage("OnTimelineProcessFrame", _msgData, SendMessageOptions.DontRequireReceiver); } } } //初始化脚本 private void InitScript(GameObject go) { if (go != _go) { _go = go; _script = null; } if (_script == null && !string.IsNullOrEmpty(_scriptName) && _go != null) { var mbs = _go.GetComponents(); for (int i = 0; i < mbs.Length; i++) { if (mbs[i] != null && mbs[i].GetType().Name == _scriptName) { _script = mbs[i]; break; } } if (_script != null) { _script.enabled = _isActived; } } } } }