コード例 #1
0
        public virtual void OnEnable()
        {
            datablock = (Datablock)target;

            renameAssetTempName = datablock.name;

            members = Datablock.GetFields(datablock.GetType());
            DatablockManager.EnsureInitilized();
        }
コード例 #2
0
        private void CreateChildDatablock(Datablock datablock)
        {
            var newDatablock = CreateInstance(datablock.GetType()) as Datablock;

            newDatablock.Parent = datablock;
            string pathName = AssetDatabase.GenerateUniqueAssetPath(AssetDatabase.GetAssetPath(datablock));

            AssetDatabase.CreateAsset(newDatablock, pathName);
            Selection.activeObject = newDatablock;
        }
コード例 #3
0
        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
            if (GUILayout.Button("Create new", EditorStyles.toolbarDropDown, GUILayout.Width(90)))
            {
                var menu = new GenericMenu();

                foreach (Type datablockType in datablockTypes)
                {
                    Type   type          = datablockType; // Copy to local value because of closure
                    string datablockName = datablockType.Name;
                    var    category      = type.GetAttribute <DatablockCategoryAttribute>();

                    if (category != null)
                    {
                        datablockName = category.category + "/" + datablockName;
                    }

                    menu.AddItem(new GUIContent(datablockName), false, () => CreateNewDatablock(type));
                }

                menu.ShowAsContext();
            }

            EditorGUIUtility.labelWidth = 50;
            filterText = EditorGUILayout.TextField("Filter:", filterText, EditorStyles.toolbarTextField, new[] { GUILayout.ExpandWidth(true) }) ?? "";

            Texture2D clearTextureToUse = filterText == "" ? null : clearTexture;

            var clearStyle = new GUIStyle(GUIStyle.none);

            clearStyle.padding = new RectOffset(1, 1, 3, 1);


            if (GUILayout.Button(clearTextureToUse, clearStyle, new[] { GUILayout.Width(15) }))
            {
                filterText = "";
                GUI.FocusControl(null);
            }

            var datablockNames = new List <string>();

            foreach (Type datablockType in datablockTypes)
            {
                string datablockName = datablockType.Name;
                datablockNames.Add(datablockName);
            }

            var typeList = new List <string>(datablockNames);

            typeList.Insert(0, "None");

            EditorGUIUtility.labelWidth = 80;

            string filterTypeText = GetParameterValue("T");

            if (filterTypeText != null)
            {
                filterTypeIndex = typeList.IndexOf(filterTypeText);
                if (filterTypeIndex < 0)
                {
                    filterTypeIndex = 0;
                }
            }
            else
            {
                filterTypeIndex = 0;
            }

            int tempFilterTypeIndex = EditorGUILayout.Popup("Type Filter:", filterTypeIndex, typeList.ToArray(), EditorStyles.toolbarPopup, GUILayout.Width(200));

            if (filterTypeIndex != tempFilterTypeIndex)
            {
                filterTypeIndex = tempFilterTypeIndex;
                if (filterTypeIndex != 0)
                {
                    SetFilterParameter("T", typeList[filterTypeIndex]);
                }
                else
                {
                    SetFilterParameter("T", null);
                }
            }

            EditorGUILayout.EndHorizontal();


            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("ID", EditorStyles.boldLabel, GUILayout.Width(60f));
            GUILayout.Label("Name", EditorStyles.boldLabel, new[] { GUILayout.ExpandWidth(true), GUILayout.MinWidth(165) });
            GUILayout.Label("Type", EditorStyles.boldLabel, new[] { GUILayout.MaxWidth(170f), GUILayout.MinWidth(100) });
            GUILayout.Label("Parent", EditorStyles.boldLabel, GUILayout.MinWidth(150));
            GUILayout.Label("Actions", EditorStyles.boldLabel, GUILayout.Width(110));
            EditorGUILayout.EndHorizontal();


            List <Datablock> datablocks = GetFilteredDatablocks().ToList();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);

            var lineStyle = new GUIStyle(EditorStyles.toolbarTextField);

            lineStyle.fixedHeight = 23;
            //lineStyle.margin = new RectOffset(0, 0, 3, 7);
            lineStyle.padding = new RectOffset(0, 0, 3, 3);

            var nameStyle = new GUIStyle(EditorStyles.label);

            nameStyle.normal.textColor = EditorGUIUtility.isProSkin ? new Color(.04f, .5f, 0.85f) : new Color(.02f, .1f, 0.65f);

            var toolbarButtonStyle = new GUIStyle(EditorStyles.toolbarButton);

            toolbarButtonStyle.padding = new RectOffset(2, 2, 2, 2);


            if (datablocks.Count == 0)
            {
                GUILayout.Label("No results");
            }

            bool alternateColor = false;

            for (int i = 0; i < datablocks.Count; i++)
            {
                Datablock datablock = datablocks[i];

                if (!datablock)
                {
                    continue;
                }

                GUI.backgroundColor = alternateColor ? Color.white : new Color(0.8f, 0.8f, 0.85f);
                alternateColor      = !alternateColor;

                EditorGUILayout.BeginHorizontal(lineStyle);

                // ID
                GUILayout.Label(datablock.DatablockId.ToString(), GUILayout.Width(60f));

                // Name
                if (GUILayout.Button(datablock.name, nameStyle, new[] { GUILayout.ExpandWidth(true), GUILayout.MinWidth(165) }))
                {
                    SelectDatablock(datablock);
                }

                // Type
                GUILayout.Label(datablock.GetType().Name, new[] { GUILayout.MaxWidth(170f), GUILayout.MinWidth(100) });

                // Parent
                if (datablock.Parent == null)
                {
                    GUILayout.Label("None", GUILayout.MinWidth(150));
                }
                else
                {
                    if (GUILayout.Button(datablock.Parent.name, nameStyle, GUILayout.MinWidth(150)))
                    {
                        SelectDatablock(datablock.Parent);
                    }
                }

                // Start toolbar buttons
                EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.Width(110f));

                // Select children
                if (GUILayout.Button(new GUIContent(childTexture, "Select children"), toolbarButtonStyle, GUILayout.Width(25)))
                {
                    SetFilterParameter("P", datablock.DatablockId.ToString(), true);
                }

                // Select silblibngs
                if (GUILayout.Button(new GUIContent(siblingTexture, "Select siblings"), toolbarButtonStyle, GUILayout.Width(25)))
                {
                    if (datablock.Parent)
                    {
                        SetFilterParameter("S", datablock.Parent.DatablockId.ToString(), true);
                    }
                    else
                    {
                        SetFilterParameter("S", "0", true);
                    }
                }

                // New Child
                if (GUILayout.Button(new GUIContent(addTexture, "Create child from this datablock"), toolbarButtonStyle, GUILayout.Width(25)))
                {
                    CreateChildDatablock(datablock);
                }

                // Delete
                if (GUILayout.Button(new GUIContent(removeTexture, "Delete datablock"), toolbarButtonStyle, GUILayout.Width(25)))
                {
                    if (EditorUtility.DisplayDialog("Delete datablock", "Are you sure you want to delete " + datablock.name + "?", "Delete", "Cancel"))
                    {
                        string pathToDelete = AssetDatabase.GetAssetPath(datablock);
                        AssetDatabase.DeleteAsset(pathToDelete);
                    }
                }

                EditorGUILayout.EndHorizontal();


                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndScrollView();
        }
コード例 #4
0
        public override void OnInspectorGUI()
        {
            EditorGUI.BeginChangeCheck();

            EditorGUILayout.BeginHorizontal();
            GUI.SetNextControlName("NewNameField");
            renameAssetTempName = EditorGUILayout.TextField("Name", renameAssetTempName);

            bool doChange = false;

            if (Event.current.isKey)
            {
                switch (Event.current.keyCode)
                {
                case KeyCode.Return:
                case KeyCode.KeypadEnter:
                    if (GUI.GetNameOfFocusedControl() == "NewNameField")
                    {
                        doChange = true;
                    }
                    break;
                }
            }

            if (GUILayout.Button("Change", GUILayout.MaxWidth(55)) || doChange)
            {
                string path = AssetDatabase.GetAssetPath(datablock);
                AssetDatabase.RenameAsset(path, renameAssetTempName);
                AssetDatabase.Refresh();
            }
            EditorGUILayout.EndHorizontal();

            var newParent = EditorGUILayout.ObjectField("Parent Datablock", datablock.Parent, datablock.GetType(), false) as Datablock;

            if (newParent != datablock.Parent)
            {
                if (newParent != null && newParent.GetType() != datablock.GetType())
                {
                    Debug.LogError("Parent is not the same type.");
                }
                else if (newParent != null && !datablock.IsParentValid(newParent))
                {
                    Debug.LogError("Parent would create a circular loop.");
                }
                else
                {
                    datablock.Parent = newParent;
                }
            }
            EditorGUILayout.Space();
            DrawFields();


            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Create new child"))
            {
                var newDatablock = CreateInstance(datablock.GetType()) as Datablock;
                newDatablock.Parent = datablock;
                string name = AssetDatabase.GenerateUniqueAssetPath(AssetDatabase.GetAssetPath(datablock));
                AssetDatabase.CreateAsset(newDatablock, name);
                Selection.activeObject = newDatablock;
                return;
            }
            if (GUILayout.Button("Create copy"))
            {
                Datablock newDatablock = CreateInstance(datablock.GetType()) as Datablock;
                newDatablock.Parent = datablock.Parent;
                string name = AssetDatabase.GenerateUniqueAssetPath(AssetDatabase.GetAssetPath(datablock));
                AssetDatabase.CreateAsset(newDatablock, name);
                Selection.activeObject = newDatablock;
                return;
            }
            EditorGUILayout.EndHorizontal();

            if (datablock.Parent != null)
            {
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Reset to parent"))
                {
                    datablock.ClearParentOverrides();
                }

                EditorGUILayout.EndHorizontal();


                showHierarchy = EditorGUILayout.Foldout(showHierarchy, "Hierarchy");
                if (showHierarchy)
                {
                    EditorGUILayout.BeginVertical();

                    Datablock parent = datablock.Parent;
                    while (parent != null)
                    {
                        if (GUILayout.Button(parent.name, EditorStyles.miniButton))
                        {
                            Selection.activeObject = parent;
                        }
                        parent = parent.Parent;
                    }

                    EditorGUILayout.EndVertical();
                }
            }

            if (!Application.isPlaying)
            {
                // Run some extra logic on changes
                if (EditorGUI.EndChangeCheck())
                {
                    EditorUtility.SetDirty(datablock);
                }
            }
        }