Exemplo n.º 1
0
        /// <summary>
        /// A method that allows us to draw our modified NGUITransformInspector, if the user wants to.
        /// </summary>
        void DrawNGUI()
        {
            //Get the type via reflection
            var NGUI = TransformInspectorUtility.GetType("NGUITransformInspector");

            //Get the DrawInspector method
            MethodInfo NGUI_DrawInspector = NGUI.GetMethod("DrawInspector");

            //Get the OnEnable method
            MethodInfo NGUI_OnEnable = NGUI.GetMethod("OnEnable");

            //Create an instance of the NGUITransformInspector class
            var NGUIObject = Activator.CreateInstance(NGUI);

            //If we found the method (not null)...
            if (NGUI_OnEnable != null)
            {
                NGUI_OnEnable.Invoke(NGUIObject, new object[] { this.serializedObject });
            }
            else             //Otherwise, something horrible has gone wrong, so put an error in the console.
            {
                Debug.LogError("Catastrophic reflection failure!  Contact U2DEX support!");
            }

            //If we found the method (not null)...
            if (NGUI_DrawInspector != null)
            {
                NGUI_DrawInspector.Invoke(NGUIObject, null);
            }
            else             //Otherwise, something horrible has gone wrong, so put an error in the console.
            {
                Debug.LogError("Catastrophic reflection failure!  Contact U2DEX support!");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// A method that returns whether the user has NGUI installed.
        /// </summary>
        /// <returns></returns>
        bool FoundNGUI()
        {
            var NGUI = TransformInspectorUtility.GetType("NGUITransformInspector");

            if (NGUI != null)
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// The OnGUI function for this Editor Window.  Handles all the GUI drawing.
        /// </summary>
        private static void OnGUI(bool DrawTitles)
        {
            labelStyle              = new GUIStyle(GUI.skin.label);
            labelStyle.padding.top  = 0;
            labelStyle.padding.left = 0;

            if (DrawTitles)
            {
                GUILayout.Label("Grid Preferences", EditorStyles.boldLabel, new GUILayoutOption[0]);
            }

            DrawGridOptions(340);

            if (DrawTitles)
            {
                GUILayout.Label("Snapping Preferences", EditorStyles.boldLabel, new GUILayoutOption[0]);
            }

            DrawSnapOptions(340);

            GUILayout.Space(10);

            if (DrawTitles)
            {
                GUILayout.Label("2D Transform Inspector", EditorStyles.boldLabel, new GUILayoutOption[0]);
            }

            DisplayPluginToggle();
            DrawApplicableClassesOptions(345);

            //Only draw this if we've found NGUI, and it's our modified NGUITransformInspector
            if (TransformInspectorUtility.GetType("NGUITransformInspector") != null &&
                TransformInspectorUtility.GetType("NGUITransformInspector").GetMethod("DrawInspector") != null)
            {
                GUILayout.Space(10);

                if (DrawTitles)
                {
                    GUILayout.Label("Third-party Compatibility and Support", EditorStyles.boldLabel, new GUILayoutOption[0]);
                }

                DisplayThirdPartySupport();
            }
            else if (TransformInspectorUtility.GetType("NGUITransformInspector") != null)
            {
                //Otherwise, if we found an unaltered NGUITransformInspector, let the user know about it via a warning.
                GUILayout.Space(10);

                EditorGUILayout.HelpBox("NGUI has been detected.\nIf you want to use U2DEX's Transform Inspector" +
                                        " instead of NGUI's, please import the NGUISupport.unitypackage found" +
                                        " in the root U2DEX folder." +
                                        "\n\nYou can then toggle between the two inspectors here.", MessageType.Warning);
            }

            //GUILayout.Space(10); //We don't need a space here -- it's already 'added' by the bottom of the ScrollView

            if (DrawTitles)
            {
                GUILayout.Label("Help and Updates", EditorStyles.boldLabel, new GUILayoutOption[0]);
            }

            DisplayHelpAndUpdates(340);

            GUILayout.Space(10);
        }
        private static void DrawApplicableClassesOptions(int Width)
        {
            // Use a Horizontal space or the toolbar will extend to the left no matter what
            EditorGUILayout.BeginHorizontal(GUILayout.Width(Width));

            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
            if (GlobalSnappingData.ApplicableClasses.Count > 0)
            {
                GUIContent content;
                GUIStyle   iconStyle;

                iconStyle = new GUIStyle(EditorStyles.toolbarButton);

                //use a different font size for each icon, since they're not the same size...
                if (hide)
                {
                    iconStyle.fontSize = 15;
                }
                else
                {
                    iconStyle.fontSize = 10;
                }

                var icon    = (hide == true) ? "\u25B6" : "\u25BC";
                var tooltip = (hide == true) ? "Click to show all" : "Click to hide all";
                content = new GUIContent(icon, tooltip);

                //If the button if pressed, toggle the hide boolean.
                if (GUILayout.Button(content, iconStyle, GUILayout.Width(25)))
                {
                    //Hide equals the opposite of Hide...
                    hide = !hide;
                }
            }

            EditorGUILayout.LabelField("Applicable Classes", labelStyle);

            //Add the + button...
            EditorGUILayout.BeginHorizontal(GUILayout.MaxWidth(75));

            GUIStyle style = new GUIStyle(EditorStyles.toolbarButton);

            style.fontSize       = 16;
            style.padding.bottom = 2;

            //Add button
            if (GUILayout.Button(new GUIContent("+", "Click to add a class"), style))
            {
                GlobalSnappingData.ApplicableClasses.Add("Give me a class name!");

                //if we're hiding the entries, unhide them since we just added one.
                if (hide)
                {
                    hide = false;
                }
            }

            //switch the font size and padding for this button...
            style.padding.bottom = 1;
            style.fontSize       = 10;

            //Help button
            if (GUILayout.Button(new GUIContent("?", "Click to view help"), style, GUILayout.Width(23)))
            {
                //Open a new window instance, or focus on the old one, if we have one open.
                var help_Window = (AdvancedSnapping_HelpUI)EditorWindow.GetWindow(typeof(AdvancedSnapping_HelpUI),
                                                                                  true, u2dexInfo.FullName + ": Snap Tool Help");

                /*Let's take a minute to reflect, and get the Preferences window...*/
                //Get the assembly...
                var assembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));

                //Get the type...
                var type = assembly.GetType("UnityEditor.PreferencesWindow");

                //Finally, get the preference window from the newly-reflected private type.
                var preference_Window = EditorWindow.GetWindow(type);

                //Now, use the preference window's position.
                Rect position = preference_Window.position;

                //Position the help window on top of the current window, in a cascading, slightly-offset manner...
                help_Window.position = new Rect(position.xMin + 200, position.yMin + 100,
                                                position.width, position.height);
                help_Window.Init();
                help_Window.minSize = new Vector2(500, 300);
                help_Window.maxSize = new Vector2(500, 300);

                //Put the help window in focus...
                help_Window.Focus();
            }

            //switch it back for any buttons after this...
            style.padding.bottom = 2;
            style.fontSize       = 16;

            GUILayout.Space(1);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndHorizontal();

            //Keep these outside the !hide if, so the toggle button at the bottom stays at the bottom.
            EditorGUILayout.BeginVertical(GUILayout.Width(Width));

            //Start the ScrollView, but also cap it's height at between 75 and 100 (otherwise it takes up all the space it can...)
            //We also use fancy single-line evaluation statements to check if we're supposed to hide the list.  If we are
            //hiding the list, we remove the height settings (set to zero) so we don't take up a ton of empty room.
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition,
                                                             GUILayout.MinHeight(hide ? 0 : 75), GUILayout.MaxHeight(hide ? 0 : 100));

            if (!hide)
            {
                //center the remove buttons...
                GUIStyle removeStyle = new GUIStyle(style);
                removeStyle.margin.top = 1;
                removeStyle.padding    = new RectOffset(2, 2, 0, 2);

                for (int i = 0; i < GlobalSnappingData.ApplicableClasses.Count; i++)
                {
                    //Cache whether or not the current class is disabled.
                    var currentClassDisabled = GlobalSnappingData.DisabledClasses.Contains(GlobalSnappingData.ApplicableClasses[i]);

                    //If the current class is disabled, disable our GUI for our first few controls.
                    if (currentClassDisabled)
                    {
                        GUI.enabled = false;
                    }

                    EditorGUILayout.BeginHorizontal();
                    {
                        Type objectType = null;
                        if (GlobalSnappingData.ApplicableClasses[i] != string.Empty)
                        {
                            objectType = TransformInspectorUtility.GetType(GlobalSnappingData.ApplicableClasses[i]);
                            //Debug.Log(objectType); //BEWARE!  Massive performance hit!  Only use for debugging.
                        }

                        GUIContent verifyContent;
                        if (objectType != null)
                        {
                            verifyContent = new GUIContent(UnfinityGUIUtil.CheckMark, "This class has been found");
                            EditorGUILayout.LabelField(verifyContent,
                                                       verifiedTextStyle, GUILayout.Width(25));
                        }
                        else
                        {
                            verifyContent = new GUIContent(UnfinityGUIUtil.XMark, "This class cannot be found");
                            EditorGUILayout.LabelField(verifyContent,
                                                       unverifiedTextStyle, GUILayout.Width(25));
                        }

                        EditorGUILayout.LabelField(GetClassStringFromIndex(i),
                                                   GUILayout.Width(100));

                        //We only need to check if our class is a supported class if our current class is NOT already disabled.
                        if (!currentClassDisabled)
                        {
                            //Set the enabled field of the GUI to the opposite of whether the class is
                            //officially supported or not.  Essentially, if it IS supported, disable the GUI, etc.
                            GUI.enabled = !IsSupportedClass(GlobalSnappingData.ApplicableClasses[i]);
                        }

                        GlobalSnappingData.ApplicableClasses[i] =
                            EditorGUILayout.TextField(GlobalSnappingData.ApplicableClasses[i]);

                        //Add the toggle and x (remove) button...
                        EditorGUILayout.BeginHorizontal(GUILayout.MaxWidth(20));
                        {
                            //We have to make sure that GUI is enabled for our toggles, so we'll cache the current value
                            //so we can reset it when we're done.
                            var currentGUIEnabled = GUI.enabled;
                            GUI.enabled = true;

                            //Create a variable to house whether or not the current class is enabled.
                            var classEnabled = true;

                            //If the current class is disabled, we'll set our classEnabled flag to false.
                            if (currentClassDisabled)
                            {
                                classEnabled = false;
                            }

                            //Create a variable to house our previous enabled flag, as the toggle may change it.
                            var previousToggleValue = classEnabled;

                            //Create a GUIContent to house our toggle tooltip (that changes depending on the status of classEnabled).
                            GUIContent classToggleContent = new GUIContent("", (classEnabled) ? "Click to disable" : "Click to enable");

                            //Render our toggle.
                            classEnabled = GUILayout.Toggle(classEnabled, classToggleContent, GUILayout.Width(10));

                            //If the value changed...
                            if (previousToggleValue != classEnabled)
                            {
                                if (classEnabled)
                                {
                                    //If our current class was disabled (but isn't anymore), remove it from our DisabledClasses list.
                                    if (currentClassDisabled)
                                    {
                                        GlobalSnappingData.DisabledClasses.Remove(GlobalSnappingData.ApplicableClasses[i]);
                                    }
                                }
                                else
                                {
                                    //If our current class wasn't disabled (but it is now), add it to our DisabledClasses list.
                                    if (!currentClassDisabled)
                                    {
                                        GlobalSnappingData.DisabledClasses.Add(GlobalSnappingData.ApplicableClasses[i]);
                                    }
                                }
                            }

                            GUILayout.Space(3);

                            //Reset our GUI.enabled flag for the rest of our GUI.
                            GUI.enabled = currentGUIEnabled;

                            //Add button
                            if (GUILayout.Button(new GUIContent("×", "Click to remove this class"),
                                                 removeStyle))
                            {
                                GlobalSnappingData.ApplicableClasses.Remove(GlobalSnappingData.ApplicableClasses[i]);
                            }

                            GUILayout.Space(3);
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                    EditorGUILayout.EndHorizontal();

                    //if the GUI was disabled, enable it here.
                    if (!GUI.enabled)
                    {
                        GUI.enabled = true;
                    }
                }
            }
            EditorGUILayout.EndScrollView();
            EditorGUILayout.EndVertical();
        }
        public void DrawInspector(Transform target)
        {
            Transform t = (Transform)target;

            //if we're only editing 1 object, otherwise we need to display an error message since multi-object
            //editing isn't currently supported by this extension.
            if (Selection.transforms.Length < 2)
            {
                // Replicate the standard transform inspector gui if the component isn't part of 2DToolkit
                if (t.gameObject.GetComponent("tk2dBaseSprite"))
                {
                    var tk2dSpriteType = TransformInspectorUtility.GetType("tk2dBaseSprite");
                    var tk2dSprite     = t.gameObject.GetComponent("tk2dBaseSprite");

                    //Get the sprite definition, so we can make sure it isn't null before preceding.
                    MethodInfo tk2d_GetCurrentSpriteDef = tk2dSpriteType.GetMethod("GetCurrentSpriteDef", Type.EmptyTypes);
                    object     SpriteDef = tk2d_GetCurrentSpriteDef.Invoke(tk2dSprite, tk2d_GetCurrentSpriteDef.GetParameters());

                    //Try to check if the 2DToolkit sprite object is valid
                    if (tk2dSprite != null && SpriteDef != null)
                    {
                        DrawSnappingFoldout(t);

                        UnfinityGUIUtil.Unity4Space();

                        //We only need 2 vectors (X and Y) for 2DToolkit.  No need to show the Z value.
                        Vector3 position = EditorGUILayout.Vector2Field("Position", new Vector2(t.localPosition.x, t.localPosition.y));

                        UnfinityGUIUtil.Unity4Space();

                        //Again, we only need X and Y for scale.
                        Vector2 scale = EditorGUILayout.Vector2Field("Size",
                                                                     new Vector2(TransformInspectorUtility.GetScaleFromClassName("tk2dBaseSprite", "scale", t).x,
                                                                                 TransformInspectorUtility.GetScaleFromClassName("tk2dBaseSprite", "scale", t).y));

                        UnfinityGUIUtil.Unity4Space();

                        DrawRotationControls(t);

                        //Leave some vertical space between areas!
                        UnfinityGUIUtil.Unity4Space();

                        DrawLayerAndDepthControls(t);

                        //allow the Z Depth to be set
                        position.z = (float)EditorGUILayout.IntField("Z Depth", (int)t.localPosition.z);

                        EditorGUI.indentLevel = 0;

                        //Leave some vertical space between areas!
                        EditorGUILayout.Space();

                        if (GUI.changed)
                        {
                            Undo.RecordObject(t, "Transform Change");
                            t.localPosition    = this.FixIfNaN(position);
                            t.localEulerAngles = this.FixIfNaN(EulerAngles);

                            //Check if the scale is NaN
                            var tk2DScale = new Vector3(scale.x, scale.y, 1);
                            tk2DScale = this.FixIfNaN(tk2DScale);

                            //Then copy it back
                            //tk2dSprite.scale = new Vector2(tk2DScale.x, tk2DScale.y);
                            TransformInspectorUtility.SetScaleFromClassName("tk2dBaseSprite", "scale", t, tk2DScale);

                            //Retrieve the layer by name, and then set it.
                            tk2dSprite.gameObject.layer = LayerMask.NameToLayer(GetSortedLayer());

                            //copy our changed sprite back to our target.
                            EditorUtility.SetDirty(tk2dSprite);
                        }
                    }
                }
            }
            else
            {
                EditorGUILayout.LabelField("Multi-object editing is not supported at this time.");
            }
        }
Exemplo n.º 6
0
        public override void OnInspectorGUI()
        {
            Transform t = (Transform)this.target;

            ObjectType = TransformInspectorUtility.GetObjectType(t);

            if (!FoundNGUI() || !GlobalSnappingData.UseNGUIInspector)
            {
                //if we don't get any supported 2D object types, or we're disabled (either globally, or this particular class type is disabled)...
                if (ObjectType == null || !GlobalSnappingData.TransformInspectorEnabled ||
                    (GlobalSnappingData.DisabledClasses.Contains(ObjectType.Name) || GlobalSnappingData.DisabledClasses.Contains(ObjectType.FullName)))
                {
                    //This is not an applicable object.
                    GlobalSnappingData.ApplicableObjectSelected = false;

                    //Draw the default inspector
                    DefaultTransformInspector.Draw(t);
                }
                else                 //we got at least one supported 2D object type, find out which one it is.
                {
                    //This *is* an applicable object.
                    GlobalSnappingData.ApplicableObjectSelected = true;

                    if (ObjectType == TransformInspectorUtility.GetType("tk2dBaseSprite"))
                    {
                        //We got 2D Toolkit, show its inspector.
                        tk2dInspector.DrawInspector(t);
                    }
                    else
                    {
                        //We got Orthello, show its inspector
                        if (ObjectType == TransformInspectorUtility.GetType("OTSprite"))
                        {
                            orthelloInspector.DrawInspector(t);
                        }
                        else
                        {
                            //We got a Unity sprite, show its inspector
                            if (ObjectType == TransformInspectorUtility.GetType("SpriteRenderer"))
                            {
                                unitySpriteInspector.DrawInspector(t);
                            }
                            else
                            {
                                //add more supported 2D stuff here...

                                //The foreach should be in the LAST else bracket, since we want to check for officially supported
                                //types BEFORE types the user entered manually...
                                foreach (string Name in GlobalSnappingData.ApplicableClasses)
                                {
                                    //Debug.Log(ObjectType.Name + " == " + Name);
                                    //Debug.Log(ObjectType.FullName + " == " + Name);

                                    //Make sure that our ObjectType's Name or FullName match our provided name before proceeding.
                                    if (ObjectType.Name == Name || ObjectType.FullName == Name)
                                    {
                                        genericInspector.DrawInspector(t);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                DrawNGUI();
            }
        }