public UnityAction <T> GetAction <T>(object target, string setMethodName) { System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[] { typeof(T) }); if (targetinfo == null) { Debug.LogError("no method " + setMethodName + "(" + typeof(T).Name + ") in " + target.ToString()); } return(Delegate.CreateDelegate(typeof(UnityAction <T>), target, targetinfo, false) as UnityAction <T>); }
public void Bind(UnityEvent_string @event) { #if UNITY_EDITOR UnityEventTools.AddPersistentListener(@event, GetAction <string>(target, methodName)); #else System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[0]); @event.AddListener((str) => targetinfo.Invoke(target, new object[] { str })); #endif }
public void Bind <T>(UnityEvent <T> @event) { #if UNITY_EDITOR UnityEventTools.AddPersistentListener(@event, GetAction <T>(target, methodName)); #else System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[] { typeof(T) }); @event.AddListener((val) => targetinfo.Invoke(target, new object[] { val })); #endif }
public void AddPersistentListener(ref UnityEvent @event, MonoBehaviour mb, string methodName) { var targetinfo = UnityEvent.GetValidMethodInfo(mb, methodName, new Type[] { }); UnityAction action = Delegate.CreateDelegate(typeof(UnityAction), mb, targetinfo, false) as UnityAction; UnityEventTools.AddPersistentListener(@event, action); EditorUtility.SetDirty(mb.gameObject); }
void Update() { if (Input.anyKey) { //Post a notification, making all observers of that particular Notfication call whatever function they've got subscribed to it with the latter parameter. this.PostNotification(Notification.AnyKey, 5); //Method 2, inbuilt unity events Notification.AnyKeyUnity.Invoke(); UnityEvent.GetValidMethodInfo(this, "Update", null); } }
void OnValidate() { if (LocalizeCallBack.HasCallback()) { var methodInfo = UnityEvent.GetValidMethodInfo(LocalizeCallBack.Target, LocalizeCallBack.MethodName, new Type[0]); UnityAction methodDelegate = System.Delegate.CreateDelegate(typeof(UnityAction), LocalizeCallBack.Target, methodInfo) as UnityAction; UnityEditor.Events.UnityEventTools.AddPersistentListener(LocalizeEvent, methodDelegate); LocalizeCallBack.Target = null; LocalizeCallBack.MethodName = null; } }
public void AddListener(object target, string setFloatMethodName) { System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setFloatMethodName, new Type[] { typeof(float) }); if (targetinfo == null) { Debug.LogError("no method " + setFloatMethodName + " in " + target.ToString()); } UnityAction <float> action = Delegate.CreateDelegate(typeof(UnityAction <float>), target, targetinfo, false) as UnityAction <float>; if (axisEvent.onAxisChange == null) { axisEvent.onAxisChange = new AxBind.UnityEventFloat(); } UnityEventTools.AddPersistentListener(axisEvent.onAxisChange, action); }
public void Bind(UnityEvent @event) { #if UNITY_EDITOR if (value == null) { System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[0]); if (targetinfo == null) { Debug.LogError("no method " + setMethodName + "() in " + target.ToString()); } UnityAction action = Delegate.CreateDelegate(typeof(UnityAction), target, targetinfo, false) as UnityAction; UnityEventTools.AddVoidPersistentListener(@event, action); } else if (value is int) { UnityEventTools.AddIntPersistentListener(@event, GetAction <int>(target, setMethodName), (int)value); } else if (value is float) { UnityEventTools.AddFloatPersistentListener(@event, GetAction <float>(target, setMethodName), (float)value); } else if (value is string) { UnityEventTools.AddStringPersistentListener(@event, GetAction <string>(target, setMethodName), (string)value); } else if (value is bool) { UnityEventTools.AddBoolPersistentListener(@event, GetAction <bool>(target, setMethodName), (bool)value); } else if (value is GameObject) { Bind <GameObject>(@event); } else if (value is Transform) { Bind <Transform>(@event); } else { Debug.LogError("unable to assign " + value.GetType()); } #else System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[0]); @event.AddListener(() => targetinfo.Invoke(target, new object[] { value })); #endif }
// 代码实现拖拽赋值 绑定Buttong点击事件 public static bool AddPlayAudioToGameObj(GameObject ins) { var btns = ins.transform.GetComponentsInChildren <Button>(true); bool change = false; foreach (var btn in btns) { if (!btn.transform.GetComponent <Image>()) { var pa = btn.gameObject.AddComponent <Image>(); pa.raycastTarget = true; UnityEvent BigExplosionEvent = new UnityEvent(); var targetInfo = UnityEvent.GetValidMethodInfo(pa, "Play", new Type[0]);// "Play" 为目标组件中需要绑定的共有方法 UnityAction methodDelegate = Delegate.CreateDelegate(typeof(UnityAction), pa, targetInfo) as UnityAction; UnityEventTools.AddPersistentListener(btn.onClick, methodDelegate); change = true; } } return(change); }
protected virtual void Awake() { if (componentType == null) { componentType = ReflectionHelper.GetType(targetComponentAssemblyName); } if (objComponent == null) { objComponent = targetObject.GetComponent(componentType); } if (parameterTypes == null) { parameterTypes = GetParameterTypes(); } if (objMethod == null) { objMethod = UnityEvent.GetValidMethodInfo(objComponent, targetMethod, parameterTypes); } }
void OnValidate() { if (LocalizeCallBack.HasCallback()) { try { var methodInfo = UnityEvent.GetValidMethodInfo(LocalizeCallBack.Target, LocalizeCallBack.MethodName, new Type[0]); if (methodInfo != null) { UnityAction methodDelegate = System.Delegate.CreateDelegate(typeof(UnityAction), LocalizeCallBack.Target, methodInfo, false) as UnityAction; if (methodDelegate != null) { UnityEditor.Events.UnityEventTools.AddPersistentListener(LocalizeEvent, methodDelegate); } } } catch (Exception) {} LocalizeCallBack.Target = null; LocalizeCallBack.MethodName = null; } }