Exemple #1
0
        public static object ScriptDrawer(object obj, InvokeWrapper wrapper, GUISkin skin)
        {
            object result           = null;
            var    type             = wrapper.GetContainingType();
            bool   allowSceneObject = type == typeof(GameObject) ||
                                      type.BaseType == typeof(ScriptComponent);
            Object valInField           = wrapper.Get <Object>(obj);
            bool   recursiveEditing     = wrapper.HasAttribute <AllowRecursiveEditing>();
            bool   createNewAssetButton = false;

            if (recursiveEditing)
            {
                var foldoutData = EditorData.Instance.GetData(obj as Object, wrapper.Member.Name);

                GUILayout.BeginHorizontal();
                {
                    var objFieldLabel = MakeLabel(wrapper.Member);
                    var buttonSize    = skin.label.CalcHeight(objFieldLabel, Screen.width);
                    UnityEngine.GUI.enabled = valInField != null;
                    foldoutData.Bool        = GUILayout.Button(GUI.MakeLabel(foldoutData.Bool ? "-" : "+"),
                                                               skin.button,
                                                               new GUILayoutOption[] { GUILayout.Width(20.0f), GUILayout.Height(buttonSize) }) ?
                                              // Button clicked - toggle current value.
                                              !foldoutData.Bool :
                                              // If foldout were enabled but valInField has changed to null - foldout will become disabled.
                                              valInField != null && foldoutData.Bool;
                    UnityEngine.GUI.enabled = true;
                    result = EditorGUILayout.ObjectField(objFieldLabel,
                                                         valInField,
                                                         type,
                                                         allowSceneObject,
                                                         new GUILayoutOption[] { });

                    if (typeof(ScriptAsset).IsAssignableFrom(type))
                    {
                        GUILayout.Space(4);
                        using (new GUI.ColorBlock(Color.Lerp(UnityEngine.GUI.color, Color.green, 0.1f)))
                            createNewAssetButton = GUILayout.Button(GUI.MakeLabel("New", false, "Create new asset"),
                                                                    GUILayout.Width(42),
                                                                    GUILayout.Height(buttonSize));
                    }
                }
                GUILayout.EndHorizontal();

                // Remove editor if object field is set to null or another object.
                if (valInField != (result as Object))
                {
                    ToolManager.ReleaseRecursiveEditor(valInField);
                    foldoutData.Bool = false;
                }

                if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) &&
                    Event.current.type == EventType.MouseDown &&
                    Event.current.button == 0)
                {
                    Event.current.Use();
                    foldoutData.Bool = !foldoutData.Bool;

                    // Unfolding - remove editor.
                    if (!foldoutData.Bool)
                    {
                        ToolManager.ReleaseRecursiveEditor(result as Object);
                    }

                    GUIUtility.ExitGUI();
                }

                if (foldoutData.Bool)
                {
                    using (new GUI.Indent(12)) {
                        GUI.Separator();

                        GUILayout.Space(6);

                        AGXUnity.Utils.GUI.WarningLabel("Changes made to this object will affect all objects referencing this asset.",
                                                        skin);

                        GUILayout.Space(6);

                        Editor editor = ToolManager.TryGetOrCreateRecursiveEditor(result as Object);
                        if (editor != null)
                        {
                            editor.OnInspectorGUI();
                        }

                        GUI.Separator();
                    }
                }
            }
            else
            {
                result = EditorGUILayout.ObjectField(MakeLabel(wrapper.Member),
                                                     valInField,
                                                     type,
                                                     allowSceneObject,
                                                     new GUILayoutOption[] { });
            }

            if (createNewAssetButton)
            {
                var assetName = type.Name.SplitCamelCase().ToLower();
                var path      = EditorUtility.SaveFilePanel("Create new " + assetName, "Assets", "new " + assetName + ".asset", "asset");
                if (path != string.Empty)
                {
                    var info         = new System.IO.FileInfo(path);
                    var relativePath = IO.Utils.MakeRelative(path, Application.dataPath);
                    var newInstance  = ScriptAsset.Create(type);
                    newInstance.name = info.Name;
                    AssetDatabase.CreateAsset(newInstance, relativePath + (info.Extension == ".asset" ? "" : ".asset"));
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();

                    result = newInstance;
                }
            }

            return(result);
        }
Exemple #2
0
        public sealed override void OnInspectorGUI()
        {
            if (Utils.KeyHandler.HandleDetectKeyOnGUI(this.targets, Event.current))
            {
                return;
            }

            if (IsMainEditor && !typeof(ScriptableObject).IsAssignableFrom(target.GetType()))
            {
                var controlRect = EditorGUILayout.GetControlRect(false, 0.0f);
                if (m_icon == null)
                {
                    m_icon = IconManager.GetIcon("algoryx_white_shadow_icon");
                }

                if (m_icon != null)
                {
                    if (m_hideTexture == null)
                    {
#if UNITY_2019_3_OR_NEWER
                        var hideColor = Color.Lerp(InspectorGUI.BackgroundColor, Color.white, 0.03f);
#else
                        var hideColor = InspectorGUI.BackgroundColor;
#endif

                        m_hideTexture = GUI.CreateColoredTexture(1, 1, hideColor);
                    }

                    var hideRect = new Rect(controlRect.x,
#if UNITY_2019_3_OR_NEWER
                                            controlRect.y - 1.25f * EditorGUIUtility.singleLineHeight - 2,
#else
                                            controlRect.y - 1.00f * EditorGUIUtility.singleLineHeight - 1,
#endif
                                            18,
                                            EditorGUIUtility.singleLineHeight + 2);
                    UnityEngine.GUI.DrawTexture(hideRect, m_hideTexture);

                    var iconRect = new Rect(hideRect);
                    iconRect.height = 14.0f;
                    iconRect.width  = 14.0f;
#if UNITY_2019_3_OR_NEWER
                    iconRect.y += 2.5f;
#else
                    iconRect.y += 1.5f;
#endif
                    iconRect.x += 3.0f;
                    UnityEngine.GUI.DrawTexture(iconRect, m_icon);
                }

                InspectorGUI.BrandSeparator();
            }

            GUILayout.BeginVertical();

            ToolManager.OnPreTargetMembers(this.targets);

            DrawMembersGUI(this.targets, null, serializedObject);

            ToolManager.OnPostTargetMembers(this.targets);

            GUILayout.EndVertical();
        }
 private void OnDisable()
 {
     ToolManager.OnTargetEditorDisable(this.targets);
 }