Beispiel #1
0
 public void AddComponent(ComponentInfoBase componentBase)
 {
     this.ComponentInfos.Add(componentBase);
 }
Beispiel #2
0
 public void RemoveComponent(ComponentInfoBase componentBase)
 {
     this.ComponentInfos.Remove(componentBase);
 }
Beispiel #3
0
        private void OnGUI()
        {
            if (this._item != null)
            {
                this._scrollPos = EditorGUILayout.BeginScrollView(this._scrollPos);
                Editor.CreateEditor(this._item).DrawDefaultInspector();

                GUILayout.Space(10);

                if (GUILayout.Button("Rename Asset File"))
                {
                    this.OnRename();
                }

                EditorGUILayout.BeginHorizontal();

                this._componentTypeIndex = EditorGUILayout.Popup(this._componentTypeIndex, this._componentTypeNames);

                Color prevAddComponenButtonColor = GUI.color;
                GUI.color = Color.green;
                if (GUILayout.Button("Add Component"))
                {
                    if (this._componentTypeIndex < 0)
                    {
                        return;
                    }

                    ComponentInfoBase componentBase = CreateInstance(this._componentTypeNames[this._componentTypeIndex]) as ComponentInfoBase;
                    if (componentBase != null)
                    {
                        this._item.AddComponent(componentBase);
                        CustomEditorUtils.AddAssetToAsset(componentBase, this._item);
                    }
                    else
                    {
                        throw new ArgumentException("component is NULL.");
                    }
                }
                GUI.color = prevAddComponenButtonColor;

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginVertical();
                GUILayout.Label("Item Components", EditorStyles.boldLabel);
                int indexToDelete = -1;
                for (int i = 0; i < this._item.ComponentInfos.Count; i++)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    if (this._item.ComponentInfos[i] != null)
                    {
                        Editor.CreateEditor(this._item.ComponentInfos[i]).OnInspectorGUI();
                    }

                    Color prevDeleteComponentButtonColor = GUI.color;
                    GUI.color = Color.red;
                    if (GUILayout.Button("Delete Component"))
                    {
                        indexToDelete = i;
                    }
                    GUI.color = prevDeleteComponentButtonColor;
                    EditorGUILayout.EndVertical();
                }

                if (indexToDelete > -1)
                {
                    bool result = EditorUtility.DisplayDialog("Delete component",
                                                              "Delete component: {0}".F(
                                                                  this._item.ComponentInfos[
                                                                      indexToDelete
                                                                  ].GetType()),
                                                              "OK", "Cancel");

                    if (result)
                    {
                        this._item.RemoveComponent(this._item.ComponentInfos[indexToDelete]);
                        DestroyImmediate(this._item.ComponentInfos[indexToDelete], true);

                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                    }
                }
                EditorGUILayout.EndVertical();

                EditorGUILayout.EndScrollView();
            }
        }