/// <summary>
 /// Returns value indicating whether or not method is a coroutine and is currently invoking (started through InvokeRepeating).
 /// </summary>
 /// <returns> True if method is coroutine that is currently invoking. </returns>
 private bool IsInvoking()
 {
     if (!isCoroutine)
     {
         return(false);
     }
     if (monoBehaviour != null)
     {
         return(monoBehaviour.IsInvoking(memberInfo.MethodInfo.Name));
     }
     return(StaticCoroutine.IsInvoking(memberInfo.MethodInfo.Name));
 }
Example #2
0
        /// <inheritdoc/>
        public override bool DrawPrefix(Rect position)
        {
            if (Event.current.type == EventType.Repaint)
            {
                //hide prefix resizer line behind the field
                DrawGUI.Active.ColorRect(lastDrawPosition, DrawGUI.Active.InspectorBackgroundColor);

                if (backgroundRect.width > 0f)
                {
                    GUI.Label(backgroundRect, "", InspectorPreferences.Styles.MethodBackground);
                }
            }

            bool drawBackgroundBehindFoldoutsWas = DrawGUI.drawBackgroundBehindFoldouts;

            DrawGUI.drawBackgroundBehindFoldouts = false;

            var prefixDrawRect      = PrefixLabelPosition;
            var clipLabelInsideRect = prefixDrawRect;

            clipLabelInsideRect.width += clipLabelInsideRect.x;
            clipLabelInsideRect.x      = 0f;
            prefixDrawRect.y           = 0f;

            //use BeginArea to prevent foldout text clipping past the Button
            GUILayout.BeginArea(clipLabelInsideRect);
            bool dirty = base.DrawPrefix(prefixDrawRect);

            GUILayout.EndArea();

            DrawGUI.drawBackgroundBehindFoldouts = drawBackgroundBehindFoldoutsWas;

            bool selected    = Selected;
            var  guiColorWas = GUI.color;

            bool isInvoking;

            if (isCoroutine && Application.isPlaying)
            {
                if (monoBehaviour != null)
                {
                    isInvoking = monoBehaviour.IsInvoking(memberInfo.MethodInfo.Name);
                }
                else
                {
                    isInvoking = StaticCoroutine.IsInvoking(memberInfo.MethodInfo.Name);
                }
            }
            else
            {
                isInvoking = false;
            }

            if (isInvoking)
            {
                GUI.color = Color.green;
            }
            else if (selected)
            {
                var col = preferences.theme.ButtonSelected;
                GUI.color = col;
            }

            if (GUI.Button(buttonRect, buttonLabel, Style))
            {
                DrawGUI.UseEvent();
                if (Event.current.button == 0)
                {
                    dirty = true;
                    Invoke();
                    Select(ReasonSelectionChanged.ControlClicked);
                }
                else if (Event.current.button == 1)
                {
                    var menu = Menu.Create();

                    menu.Add(Menu.Item("Invoke", () => Invoke(false, false, false, false)));

                    if (hasReturnValue)
                    {
                                                #if !POWER_INSPECTOR_LITE
                        menu.AddSeparator();
                        menu.Add(Menu.Item("Copy Return Value", () => Invoke(false, false, true, false)));
                                                #endif
                        if (UnityObjectExtensions.IsUnityObjectOrUnityObjectCollectionType(Type))
                        {
                            menu.Add(Menu.Item("Ping Return Value", () => Invoke(true, false, false, false)));
                            menu.Add(Menu.Item("Select Return Value", () => Invoke(false, false, false, true)));
                        }
                    }

                    if (isCoroutine)
                    {
                        menu.AddSeparator();

                        var    monoBehaviour = UnityObject as MonoBehaviour;
                        string methodName    = MethodInfo.Name;

                        if (monoBehaviour != null && Application.isPlaying)
                        {
                            menu.Add("Invoke Repeating/Every Second", () => monoBehaviour.InvokeRepeating(MethodInfo.Name, 1f, 1f));
                            menu.Add("Invoke Repeating/Every 5 Seconds", () => monoBehaviour.InvokeRepeating(MethodInfo.Name, 5f, 5f));
                            menu.Add("Invoke Repeating/Every 10 Seconds", () => monoBehaviour.InvokeRepeating(MethodInfo.Name, 10f, 10f));

                            if (monoBehaviour.IsInvoking(methodName))
                            {
                                menu.Add("Stop Coroutine", () => monoBehaviour.StopCoroutine(MethodInfo.Name));
                                menu.Add("Cancel Invoke", () => monoBehaviour.CancelInvoke(MethodInfo.Name));
                            }
                        }
                    }

                    ContextMenuUtility.Open(menu, this);
                }
                else if (Event.current.button == 2)
                {
                    Invoke(true, false, true, false);
                }
            }

            GUI.color = guiColorWas;

            return(dirty);
        }