public string GetDelegateText(UnityEvent ue, Func <bool> a) { StringBuilder text = new StringBuilder(); if (ue != null) { for (int i = 0; i < ue.GetPersistentEventCount(); ++i) { if (text.Length > 0) { text.Append("\n"); } UnityEngine.Object obj = ue.GetPersistentTarget(i); string t = obj != null?obj.name : "<???>"; text.Append(t).Append(".").Append(FilterMethodName(ue.GetPersistentMethodName(i))); } } if (a != null) { Delegate[] delegates = a.GetInvocationList(); for (int i = 0; i < delegates.Length; ++i) { if (text.Length > 0) { text.Append("\n"); } text.Append(delegates[i].Target).Append(".").Append(delegates[i].Method.Name); } } return(text.ToString()); }
static public void TrackEvent(UnityEvent e) { for (int i = 0; i < e.GetPersistentEventCount(); i++) { Debug.Log(string.Format("Tracker # Method:{0} Target:{1}", e.GetPersistentMethodName(i), e.GetPersistentTarget(i))); } }
/// <summary> /// Adds the listener with editor. /// </summary> /// <param name="myEvent">My event.</param> /// <param name="action">Action.</param> /// <typeparam name="T0">The 1st type parameter.</typeparam> public static void AddListenerWithEditor <T0>(this UnityEvent <T0> myEvent, UnityAction <T0> action) {/* * for (int i = 0; i < myEvent.GetPersistentEventCount (); i++) { * if(myEvent.GetPersistentTarget (i) == null) * { * myEvent.RemoveAllListeners (); * break; * } * }*/ #if UNITY_EDITOR bool _hasNull = true; while (_hasNull) { _hasNull = false; for (int i = 0; i < myEvent.GetPersistentEventCount(); i++) { if (myEvent.GetPersistentTarget(i) == null) { UnityEditor.Events.UnityEventTools.RemovePersistentListener(myEvent, i); _hasNull = true; break; } } } #endif if (myEvent.GetPersistentEventCount() == 0) { #if UNITY_EDITOR UnityEditor.Events.UnityEventTools.AddPersistentListener(myEvent, action); #else myEvent.AddListener(action); #endif return; } for (int i = 0; i < myEvent.GetPersistentEventCount(); i++) { string _storedMethodName = myEvent.GetPersistentMethodName(i); Object _storedTargetObj = myEvent.GetPersistentTarget(i); string _storedTypeName = _storedTargetObj.GetType().Name; if (_storedTargetObj == (Object)action.Target && _storedMethodName == action.Method.Name && _storedTypeName == action.Method.DeclaringType.Name) { //Debug.LogFormat ("{0}.{1}.{2} has exsit in {3} !", _storedTargetObj.name, _storedTypeName, _storedMethodName, myEvent.ToString()); //Debug.Log ("Skip add listener"); return; } } #if UNITY_EDITOR UnityEditor.Events.UnityEventTools.AddPersistentListener(myEvent, action); #else myEvent.AddListener(action); #endif }
public IEnumerator Invoke() { if (time > 0) { yield return(new WaitForSeconds(time)); } else { yield return(null); } Response.Invoke(); var targetType = Response.GetPersistentTarget(0).GetType(); if (targetType == typeof(SequencerMono)) { if (Response.GetPersistentMethodName(0) == "BeginSequence") { float seqtime = 0; var seq = Response.GetPersistentTarget(0) as SequencerMono; foreach (SequenceEvent s in seq.Sequence) { seqtime += s.time * seq.loops; } yield return(new WaitForSeconds(seqtime)); } } _onDone.Invoke(); }
public void Update() { System.Text.StringBuilder name = new System.Text.StringBuilder(); if (delayed && FramesDelay <= 0) { FramesDelay = 1; } if (onEnable != null) { int count = onEnable.GetPersistentEventCount(); if (count > 0) { name.Append(delayed ? "DELAYED: " : ""); for (int j = 0; j < count; j++) { if (j > 0) { name.Append(", "); } name.Append(onEnable.GetPersistentMethodName(j)); } } else { name.Append("EMPTY"); } this.name = name.ToString(); } }
internal static Delegate[] ConvertDelegateList <TParameter>(UnityEvent <TParameter> unityEvent) { int eventCount = unityEvent.GetPersistentEventCount(); Delegate[] delegateList = new Delegate[eventCount]; for (int i = 0; i < eventCount; i++) { UnityEngine.Object target = unityEvent.GetPersistentTarget(i); string methodName = unityEvent.GetPersistentMethodName(i); View view = target as View; if (view == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]Target object : {0} is not a Core.View component", target.name)); } Controller controller = view.GetController(); MethodInfo methodInfo = controller.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance); if (methodInfo == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]NotImplemented viewEvent method : {0} in {1}", methodInfo.Name, controller.GetType().Name)); } ParameterInfo[] parameterInfos = methodInfo.GetParameters(); if (parameterInfos.Length != 1 || parameterInfos[0].ParameterType != typeof(TParameter)) //只有一个对应类型参 { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]The illegal method : {0}.{1} its parameter is not match the Type : {2}", controller.GetType().Name, methodInfo.Name, typeof(TParameter).Name)); } if (methodInfo.ReturnType != typeof(void)) //无返回值 { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]The illegal method : {0}.{1} It's not supported to return value", controller.GetType().Name, methodInfo.Name)); } Delegate action = methodInfo.CreateDelegate(typeof(Action <TParameter>), controller); delegateList[i] = action; } return(delegateList); }
public void ButtonClicked() { Debug.Log("Button Clicked event"); int count = 0; Image image; Selected = 0; foreach (RectTransform rt in Content) { image = rt.GetComponentInChildren <Image> (); if (EventSystem.current.currentSelectedGameObject == rt.gameObject) { image.color = SelectedColor; Selected = count; } else { image.color = NormalColor; } count++; } string method = OnSelect.GetPersistentMethodName(0); object target = OnSelect.GetPersistentTarget(0); (target as MonoBehaviour).InvokeWithArgument(method, new IntArg(Selected) as Object); }
/// <summary>将序列化的UnityEvent转化为委托链</summary> internal static Delegate[] ConvertDelegateList(UnityEvent unityEvent) { int eventCount = unityEvent.GetPersistentEventCount(); Delegate[] delegateList = new Delegate[eventCount]; for (int i = 0; i < eventCount; i++) { UnityEngine.Object target = unityEvent.GetPersistentTarget(i); string methodName = unityEvent.GetPersistentMethodName(i); View view = target as View; if (view == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]Target object : {0} is not a Core.View component", target.name)); } MethodInfo viewMethodInfo = view.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); if (viewMethodInfo == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]NotImplemented viewEvent method : {0} in {1}", methodName, view.GetType().Name)); } ImplementedInControllerAttribute attributeInfo = viewMethodInfo.GetCustomAttribute(typeof(ImplementedInControllerAttribute)) as ImplementedInControllerAttribute; if (attributeInfo == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]The viewEvent method : {0} in {1} doesn't has ImplementedInControllerAttribute", viewMethodInfo.Name, view.GetType().Name)); } Controller controller = view.GetController(); MethodInfo methodInfo = controller.GetType().GetMethod(attributeInfo.IsCustomMethodName ? attributeInfo.MethodName : methodName, BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); if (methodInfo == null) { throw new CoreException(string.Format("[ViewBaseEventHandler.ConvertDelegateList]NotImplemented viewEvent method : {0} in {1}", attributeInfo.MethodName, controller.GetType().Name)); } Delegate action = methodInfo.CreateDelegate(typeof(Action), controller); delegateList[i] = action; } return(delegateList); }
private void OnEnable() { Function.RegisterListener(this); type = Response.GetPersistentTarget(0).GetType(); methodName = Response.GetPersistentMethodName(0); component = GetComponent(type); }
public Action GetSerializedEventDelegate(int eventIndex) { UnityEngine.Object target = serializedEvent.GetPersistentTarget(eventIndex); System.Reflection.MethodInfo targetMethod = target.GetType().GetMethod(serializedEvent.GetPersistentMethodName(eventIndex)); #if NET_4_6 return(targetMethod.CreateDelegate(typeof(Action), target) as Action); #else return(Delegate.CreateDelegate(typeof(Action), target, serializedEvent.GetPersistentMethodName(eventIndex)) as Action); #endif }
void ShowAllMethodsOfEvent <T>(UnityEvent <T> e) { var color = GUI.color; for (var i = 0; i < e.GetPersistentEventCount(); i++) { GUI.color = i % 2 == 0 ? Color.cyan : Color.yellow; GUILayout.Label(e.GetPersistentTarget(i).ToString() + "\n." + e.GetPersistentMethodName(i)); } GUI.color = color; }
public void PrintButtonListeners() { UnityEvent buttonEvent = button.onClick; int totalRegisteredEvents = buttonEvent.GetPersistentEventCount(); for (int i = 0; i < totalRegisteredEvents; ++i) { Debug.Log("Component: " + buttonEvent.GetPersistentTarget(i)); Debug.Log("Method Name: " + buttonEvent.GetPersistentMethodName(i)); } }
public virtual void OnMoveTrigger(float movement) { if (hMovement) { string eventName = unityEvent.GetPersistentMethodName(0); if (target != null) { target.BroadcastMessage(eventName, movement); } } }
void StartEvent() { //call method string name = callback.GetPersistentMethodName(0); if (!usedEvents.Contains(name)) { usedEvents.Add(name); callback.Invoke(); } }
public static bool EventContainsPersistentListener <T0, T1, T2, T3>(UnityEvent <T0, T1, T2, T3> unityEvent, UnityAction <T0, T1, T2, T3> listener) { for (int i = unityEvent.GetPersistentEventCount() - 1; i >= 0; i--) { if (unityEvent.GetPersistentMethodName(i).Equals(listener.Method.Name) && unityEvent.GetPersistentTarget(i).Equals(listener.Target)) { return(true); } } return(false); }
static bool HasCall <T>(this UnityEvent <T> @event, UnityEngine.Object caller, string methodName) { for (int i = 0; i < @event.GetPersistentEventCount(); i++) { if (@event.GetPersistentTarget(i) == caller && @event.GetPersistentMethodName(i) == methodName) { return(true); } } return(false); }
public static bool IfNotAlready <T>(UnityEvent <T> @event, UnityEngine.Object target, UnityAction <T> action) { for (int i = 0; i < @event.GetPersistentEventCount(); ++i) { if (@event.GetPersistentTarget(i) == target && @event.GetPersistentMethodName(i) == action.Method.Name) { return(false); } } On(@event, target, action); return(true); }
public static bool HasUnityEvent(UnityEvent targetEvent, string HasMethodName, UnityEngine.Object hasTarget) { int count = targetEvent.GetPersistentEventCount(); for (int i = 0; i < count; i++) { if (targetEvent.GetPersistentMethodName(i) == HasMethodName && targetEvent.GetPersistentTarget(i) == hasTarget) { return(true); } } return(false); }
public override string ToString() { string s = "Event: "; for (int i = 0; i < indexEvent.GetPersistentEventCount(); i++) { var target = indexEvent.GetPersistentTarget(i); string targetName = target != null?indexEvent.GetPersistentTarget(i).name : "(none)"; s += targetName + " " + indexEvent.GetPersistentMethodName(i); } return(s); }
public void DrawEditorView() { try { GUI.color = new Color(0.48f, 0.78f, 1f); GUILayout.Label($"Call UnityEvent. {Event.GetPersistentMethodName(0)}"); } catch (Exception e) { GUI.color = new Color(1f, 0.13f, 0.17f); GUILayout.Label($"Call UnityEvent. !Error!"); Console.WriteLine(e); } }
// Returns whether or not even one callback is registered in the UnityEvent of the inspector. //(*) Note that those registered with AddListener() are ignored. // // インスペクタの UnityEvent にコールバックが1つでも登録されてるか否かを返す。 //※AddListener() で登録したものは無視されるので注意。 protected static bool IsNullOrEmpty <T0> (UnityEvent <T0> obj) { if (obj != null) { int count = obj.GetPersistentEventCount(); //for inspector only for (int i = 0; i < count; i++) { if (!string.IsNullOrEmpty(obj.GetPersistentMethodName(i))) { return(false); } } } return(true); }
private void DrawHelperLines(UnityEvent unityEvent) { Color colorDep = Color.white; for (int i = 0; i < unityEvent.GetPersistentEventCount(); i++) { GameObject go = unityEvent.GetPersistentTarget(i) as GameObject; // Получить целевой компонент слушателя по индексу индекса. string method = unityEvent.GetPersistentMethodName(i); // Получить имя целевого метода слушателя на индекс индекс if (go != null) { Custom.Utility.NGizmos.DrawDottedLine(transform.position, go.transform.position, colorDep); Custom.Utility.NGizmos.DrawCube(transform.position, Vector3.one * 0.08f, colorDep); Custom.Utility.NGizmos.DrawCube(go.transform.position, Vector3.one * 0.08f, colorDep); } } }
static bool IsEventCallMethod(UnityEvent ev, MonoBehaviour target, string method) { var n = ev.GetPersistentEventCount(); for (int i = 0; i < n; i++) { if (ev.GetPersistentMethodName(i) == method) { if (ev.GetPersistentTarget(i) == target) { return(true); } } } return(false); }
private void AutoAdd() { // Keeping track of already registered events var listenersName = new List <string>(); var listenersTarget = new List <Type>(); for (var i = 0; i < listeners.GetPersistentEventCount(); i++) { listenersName.Add(listeners.GetPersistentMethodName(i)); listenersTarget.Add(listeners.GetPersistentTarget(i).GetType()); } // Find all behaviours in the current gameObject var allBehaviours = GetComponents <MonoBehaviour>(); // Keep track of event name var eventName = fsBoolEventSo.name.Replace(" ", ""); foreach (var monoBehaviour in allBehaviours) { if (monoBehaviour == this) { continue; } // Get all method names in the behaviour MethodInfo[] methodName = monoBehaviour.GetType().GetMethods(); foreach (var methodInfo in methodName) { // Reject non equal names if (eventName != methodInfo.Name) { continue; } // Reject already registered events if (listenersTarget.Contains(monoBehaviour.GetType()) && listenersName.Contains(methodInfo.Name)) { continue; } // If not rejected create new delegate and finish the method var action = methodInfo.CreateDelegate(typeof(UnityAction), monoBehaviour) as UnityAction <bool>; UnityEventTools.AddPersistentListener(listeners, action); } } }
public static int IndexOf <T>(this UnityEvent <T> evt, UnityAction <T> action) { var count = evt.GetPersistentEventCount(); for (int i = 0; i < count; ++i) { var isTarget = evt.GetPersistentTarget(i) == (action.Target as Object) && evt.GetPersistentMethodName(i) == action.Method.Name; if (isTarget == false) { continue; } return(i); } return(-1); }
static bool CheckEventError(ActionNode actionNode, UnityEvent uEvent, bool showLogError = false, string eventName = "event") { if ( uEvent.GetPersistentEventCount() > 0 && ( uEvent.GetPersistentTarget(0) == null || uEvent.GetPersistentMethodName(0) == "" || uEvent.GetPersistentTarget(0).ToString().Equals("Execute ()") ) ) { if (showLogError) { Debug.LogError(string.Format("Target or function is not setted on {0}", eventName), actionNode); } return(true); } return(false); }
public static void ExtendedInvoke(this UnityEvent unityEvent, UnityEngine.Object owner = null) { #if UNITY_EVENTS_DEBUG if (LogDebug) { int listenersCount = unityEvent.GetPersistentEventCount(); if (listenersCount > 0) { UnityEngine.Debug.Log("Event invoked info (listeners: " + listenersCount.ToString() + ")", owner); for (int i = 0; i < listenersCount; i++) { UnityEngine.Debug.Log( unityEvent.GetPersistentTarget(i).ToString() + " : " + unityEvent.GetPersistentMethodName(i), unityEvent.GetPersistentTarget(i)); } } } #endif unityEvent.Invoke(); }
// Local methods public override void show() { MessageBoxParams boxDef = new MessageBoxParams(); boxDef.MessageBoxTitle = Title; boxDef.MessageTitle = Header; boxDef.Message = Message; boxDef.TemplateId = TemplateID; if (ClickAction != null) { boxDef.Button1Action = ClickAction; } else { string actionname = onClick.GetPersistentMethodName(0); object actionobject = onClick.GetPersistentTarget(0); boxDef.Button1Action = (UnityAction)Delegate.CreateDelegate(typeof(UnityAction), actionobject, actionname); } EasyMessageBox.Show(boxDef); }
public static bool Validate(GameObject gameObject, UnityEvent buttonClickEvent) { bool result = true; if (Application.isEditor) { for (int index = 0; index < buttonClickEvent.GetPersistentEventCount(); index++) { UnityEngine.Object temp = buttonClickEvent.GetPersistentTarget(index); if (temp == null) { gameObject.LogError("TARGET[" + index + "] is null"); result = false; } if (buttonClickEvent.GetPersistentMethodName(index) == null) { gameObject.LogError("METHOD[" + index + "] is null"); result = false; } } } return(result); }
/// <summary> Removes a Method from a Unity Event </summary> public static void RemovePersistentListener(UnityEvent _event, string methodName, UnityEngine.Object Methodtarget) { #if UNITY_EDITOR int isThere = -1; for (int i = 0; i < _event.GetPersistentEventCount(); i++) { var L_methName = _event.GetPersistentMethodName(i); UnityEngine.Object targetListener = _event.GetPersistentTarget(i); Debug.Log("Method: " + L_methName + " Target: " + targetListener); if (L_methName == methodName && targetListener == Methodtarget) { isThere = i; break; } } if (isThere != -1) { UnityEditor.Events.UnityEventTools.RemovePersistentListener(_event, isThere); } #endif }