Пример #1
0
        /// <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
        }
Пример #2
0
        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 OnGrab(GameObject go)
 {
     Debug.Log("Grab " + transform.name + ", " + actions.GetPersistentTarget(0));
     if (whileHeldOnly)
     {
         ControlsManager.AddControl(go.name.Contains("Left") ? (ControlName)((int)controlButton - 1) : controlButton, this);
     }
 }
Пример #4
0
    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);
    }
Пример #5
0
        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);
    }
Пример #7
0
            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());
            }
        public override string GetDisplayNameForEditor(int index)
        {
            string persistentTargetNames = String.Empty;

            for (int i = 0; i < callback.GetPersistentEventCount(); i++)
            {
                if (callback.GetPersistentTarget(i) == null)
                {
                    continue;
                }

                persistentTargetNames = $"{string.Join(", ", callback.GetPersistentTarget(i).name).Truncate(45)}";
            }

            return($"{index}. {DisplayName}: {persistentTargetNames}");
        }
Пример #9
0
        /// <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);
        }
Пример #10
0
        public static void Draw(UnityEvent e, Transform origin)
        {
            int     c        = e.GetPersistentEventCount();
            Vector3 location = origin.position;

            for (int i = 0; i < c; i++)
            {
                var obj = e.GetPersistentTarget(i);
                if (!obj)
                {
                    continue;       //In the case of an unassigned UnityEvent, skip it
                }
                var mb = obj as Component;
                if (mb != null)
                {
                    location = mb.transform.position;
                }
                else
                {
                    //If the object is not a Component, get it as a GameObject
                    var go = obj as GameObject;
                    if (go)
                    {
                        location = go.transform.position;
                    }
                }

                Gizmos.DrawLine(origin.position, location);
            }
        }
Пример #11
0
    public void FocusRight()
    {
        object     obj        = Right.GetPersistentTarget(0);
        GameObject gameObject = obj as GameObject;

        GameEngine.instance.CurrentFocus = gameObject.GetComponent <UIFocus>();
        Debug.Log(gameObject.name);
    }
Пример #12
0
        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
            }
Пример #14
0
 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);
 }
Пример #15
0
 public override void InEditorUpdate(GameObject go)
 {
     for (int i = 0; i < action.GetPersistentEventCount(); i++)
     {
         GameObject ob = action.GetPersistentTarget(i) as GameObject;
         if (go && ob)
         {
             Debug.DrawLine(go.transform.position, ob.transform.position, Color.green);
         }
     }
 }
Пример #16
0
        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();
        }
Пример #17
0
 public void ExecuteEvent()
 {
     if (!isInfinitelyTriggerable)
     {
         hasPlayed = true;
     }
     if (myUnityEvent.GetPersistentTarget(0) != null)
     {
         myUnityEvent.Invoke();
     }
 }
Пример #18
0
    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));
        }
    }
Пример #19
0
        public static void CopyEventData(UnityEvent source, UnityEvent target)
        {
            int listeners = source.GetPersistentEventCount();

            for (int i = 0; i < listeners; i++)
            {
                System.Reflection.MethodInfo info = UnityEventBase.GetValidMethodInfo(source.GetPersistentTarget(i), source.GetPersistentMethodName(i), new Type[] { typeof(float) });
                UnityAction execute = () => info.Invoke(source.GetPersistentTarget(i), new object[] { 180f });
                target.AddListener(execute);
            }
        }
Пример #20
0
    void OnDrawGizmos()
    {
        var color = new Color(0, 0, 1, 0.75f);

        for (int i = 0; i < onInteract.GetPersistentEventCount(); i++)
        {
            if (onInteract.GetPersistentTarget(i) is Transform trans)
            {
                Debug.DrawLine(transform.position, trans.position, color, 0);
            }
            else if (onInteract.GetPersistentTarget(i) is GameObject go)
            {
                Debug.DrawLine(transform.position, go.transform.position, color, 0);
            }
            else if (onInteract.GetPersistentTarget(i) is MonoBehaviour mono)
            {
                Debug.DrawLine(transform.position, mono.transform.position, color, 0);
            }
        }
    }
Пример #21
0
 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);
 }
Пример #22
0
    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;
    }
Пример #23
0
 public static bool EventHasTarget(UnityEvent e)
 {
     for (int i = 0; i < e.GetPersistentEventCount(); i++)
     {
         if (e.GetPersistentTarget(i) != null)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #24
0
    /// <summary>
    /// Same as CheckUnityEventAction but allows to specify passing a param
    /// through the Event.
    /// </summary>
    public static bool CheckUnityEvent <T>(this UnityEvent <T> unityEvent)
    {
        for (int i = 0; i < unityEvent.GetPersistentEventCount(); i++)
        {
            if (unityEvent.GetPersistentTarget(i) != null)
            {
                return(true);
            }
        }

        return(false);
    }
Пример #25
0
 private void OnDrawGizmos()
 {
     if (debugOn)
     {
         Vector2 thisPos = transform.position;
         for (int i = 0; i < OnPressed.GetPersistentEventCount(); i++)
         {
             GameObject targetObject = OnPressed.GetPersistentTarget(i) as GameObject;
             Debug.DrawLine(thisPos, targetObject.transform.position, debugColor_link);
         }
     }
 }
Пример #26
0
 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);
 }
        private void OnDrawGizmos()
        {
            var eventCount = m_Event.GetPersistentEventCount();

            if (eventCount > 0)
            {
                for (int i = 0; i < eventCount; i++)
                {
                    //普通物件限定Component來抓取位置
                    //特殊狀況則是GameEvent
                    if (m_Event.GetPersistentTarget(i) && m_Event.GetPersistentTarget(i) is Component)
                    {
                        DrawGizmosLine(((Component)m_Event.GetPersistentTarget(i)).transform.position);
                    }
                    else if (m_Event.GetPersistentTarget(i) && m_Event.GetPersistentTarget(i) is GameEvent)
                    {
                        //尋找所有GameEventListener
                        foreach (var gl in Resources.FindObjectsOfTypeAll(typeof(GameEventListener)))
                        {
                            //如果是這個Event
                            if (((GameEventListener)gl).m_Event == (GameEvent)m_Event.GetPersistentTarget(i))
                            {
                                DrawGizmosLine(((GameEventListener)gl).transform.position);
                            }
                        }
                    }
                }
            }
        }
Пример #28
0
        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);
        }
Пример #29
0
    private bool hasTargets()
    {
        bool hasPersistentTarget = false;

        for (int i = 0; i < events.GetPersistentEventCount(); i++)
        {
            if (events.GetPersistentTarget(i) != null)
            {
                hasPersistentTarget = true;
            }
        }

        return(hasPersistentTarget);
    }
Пример #30
0
        private void Start()
        {
            UnityEvent onClick = GetComponent <Button>().onClick;

            for (int i = 0; i < onClick.GetPersistentEventCount(); i++)
            {
                if (onClick.GetPersistentTarget(i) == this)
                {
                    onClick.SetPersistentListenerState(i, UnityEventCallState.Off);
                }
            }

            onClick.AddListener(GoBack);
        }