EditorWindow edit ScriptableObject

 1 using UnityEngine;
 2 
 3 [System.Serializable]
 4 public class Weapon
 5 {
 6     //[SerializeField]
 7     public string weaponName;
 8     //[SerializeField]
 9     public int damage;
10 
11     public Weapon(string name, int dmg)
12     {
13         weaponName = name;
14         damage = dmg;
15     }
16 }

 

我们使用 ScriptableObject 来当做数据库 存储武器信息

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 
 6 public class WeaponDatabase : ScriptableObject
 7 {
 8     [SerializeField]
 9     private List<Weapon> database;
10 
11     void OnEnable()
12     {
13         if (database == null)
14             database = new List<Weapon>();
15     }
16 
17     public void Add(Weapon weapon)
18     {
19         database.Add(weapon);
20     }
21 
22     public void Remove(Weapon weapon)
23     {
24         database.Remove(weapon);
25     }
26 
27     public void RemoveAt(int index)
28     {
29         database.RemoveAt(index);
30     }
31 
32     public int COUNT
33     {
34         get { return database.Count; }
35     }
36 
37     //.ElementAt() requires the System.Linq
38     public Weapon Weapon(int index)
39     {
40         return database.ElementAt(index);
41     }
42 
43     public void SortAlphabeticallyAtoZ()
44     {
45         database.Sort((x, y) => string.Compare(x.weaponName, y.weaponName));
46     }
47 }

 

 自定义EditotWindow来操作数据库

  1 using UnityEngine;
  2 using UnityEditor;
  3 using System.IO;
  4 using System.Collections;
  5 using System;
  6 
  7 public class WeaponDatabaseEditor : EditorWindow
  8 {
  9     private enum State
 10     {
 11         BLANK,
 12         EDIT,
 13         ADD
 14     }
 15 
 16     private State state;
 17     private int selectedWeapon;
 18     private string newWeaponName;
 19     private int newWeaponDamage;
 20 
 21     private string PROJECT_PATH = Application.dataPath +  @"/Database";
 22 
 23     private const string DATABASE_PATH = @"Assets/Database/weaponDB.asset";
 24 
 25     private WeaponDatabase weapons;
 26     private Vector2 _scrollPos;
 27 
 28     [MenuItem("BZA/Database/Weapon Database %#w")]
 29     public static void Init()
 30     {
 31         WeaponDatabaseEditor window = EditorWindow.GetWindow<WeaponDatabaseEditor>();
 32         window.minSize = new Vector2(800, 400);
 33         window.Show();
 34     }
 35 
 36     void OnEnable()
 37     {
 38         if (weapons == null)
 39             LoadDatabase();
 40 
 41         state = State.BLANK;
 42     }
 43 
 44     void OnGUI()
 45     {
 46         EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 47         DisplayListArea();
 48         DisplayMainArea();
 49         EditorGUILayout.EndHorizontal();
 50     }
 51 
 52     void LoadDatabase()
 53     {
 54         weapons = (WeaponDatabase)AssetDatabase.LoadAssetAtPath(DATABASE_PATH, typeof(WeaponDatabase));
 55 
 56         if (weapons == null)
 57             CreateDatabase();
 58     }
 59 
 60     void CreateDatabase()
 61     {
 62         if (!Directory.Exists(PROJECT_PATH)) Directory.CreateDirectory(PROJECT_PATH);
 63 
 64         weapons = ScriptableObject.CreateInstance<WeaponDatabase>();
 65         AssetDatabase.CreateAsset(weapons, DATABASE_PATH);
 66         AssetDatabase.SaveAssets();
 67         AssetDatabase.Refresh();
 68     }
 69 
 70     void DisplayListArea()
 71     {
 72         EditorGUILayout.BeginVertical(GUILayout.Width(250));
 73         EditorGUILayout.Space();
 74 
 75         _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, "box", GUILayout.ExpandHeight(true));
 76 
 77         for (int cnt = 0; cnt < weapons.COUNT; cnt++)
 78         {
 79             EditorGUILayout.BeginHorizontal();
 80             if (GUILayout.Button("-", GUILayout.Width(25)))
 81             {
 82                 weapons.RemoveAt(cnt);
 83                 weapons.SortAlphabeticallyAtoZ();
 84                 EditorUtility.SetDirty(weapons);
 85                 state = State.BLANK;
 86                 return;
 87             }
 88 
 89             if (GUILayout.Button(weapons.Weapon(cnt).weaponName, "box", GUILayout.ExpandWidth(true)))
 90             {
 91                 selectedWeapon = cnt;
 92                 state = State.EDIT;
 93             }
 94 
 95             EditorGUILayout.EndHorizontal();
 96         }
 97 
 98         EditorGUILayout.EndScrollView();
 99 
100         EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
101         EditorGUILayout.LabelField("Weapons: " + weapons.COUNT, GUILayout.Width(100));
102 
103         if (GUILayout.Button("New Weapon"))
104             state = State.ADD;
105 
106         EditorGUILayout.EndHorizontal();
107         EditorGUILayout.Space();
108         EditorGUILayout.EndVertical();
109     }
110 
111     void DisplayMainArea()
112     {
113         EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
114         EditorGUILayout.Space();
115 
116         switch (state)
117         {
118             case State.ADD:
119                 DisplayAddMainArea();
120                 break;
121             case State.EDIT:
122                 DisplayEditMainArea();
123                 break;
124             default:
125                 DisplayBlankMainArea();
126                 break;
127         }
128 
129         EditorGUILayout.Space();
130         EditorGUILayout.EndVertical();
131     }
132 
133     void DisplayBlankMainArea()
134     {
135         EditorGUILayout.LabelField(
136             "There are 3 things that can be displayed here.\n" +
137             "1) Weapon info for editing\n" +
138             "2) Black fields for adding a new weapon\n" +
139             "3) Blank Area",
140             GUILayout.ExpandHeight(true));
141     }
142 
143     void DisplayEditMainArea()
144     {
145         weapons.Weapon(selectedWeapon).weaponName = EditorGUILayout.TextField(new GUIContent("Name: "), weapons.Weapon(selectedWeapon).weaponName);
146         weapons.Weapon(selectedWeapon).damage = int.Parse(EditorGUILayout.TextField(new GUIContent("Damage: "), weapons.Weapon(selectedWeapon).damage.ToString()));
147 
148         EditorGUILayout.Space();
149 
150         if (GUILayout.Button("Done", GUILayout.Width(100)))
151         {
152             weapons.SortAlphabeticallyAtoZ();
153             EditorUtility.SetDirty(weapons);
154             state = State.BLANK;
155         }
156     }
157 
158     void DisplayAddMainArea()
159     {
160         newWeaponName = EditorGUILayout.TextField(new GUIContent("Name: "), newWeaponName);
161         newWeaponDamage = Convert.ToInt32(EditorGUILayout.TextField(new GUIContent("Damage: "), newWeaponDamage.ToString()));
162 
163         EditorGUILayout.Space();
164 
165         if (GUILayout.Button("Done", GUILayout.Width(100)))
166         {
167             weapons.Add(new Weapon(newWeaponName, newWeaponDamage));
168             weapons.SortAlphabeticallyAtoZ();
169 
170             newWeaponName = string.Empty;
171             newWeaponDamage = 0;
172             EditorUtility.SetDirty(weapons);
173             state = State.BLANK;
174         }
175     }
176 }

 

Result

posted @ 2015-04-14 20:23  灵魂重新  阅读(1156)  评论(0编辑  收藏  举报