EditorWindow edit ScriptableObject

时间:2022-04-12 19:30:49
 using UnityEngine;

 [System.Serializable]
public class Weapon
{
//[SerializeField]
public string weaponName;
//[SerializeField]
public int damage; public Weapon(string name, int dmg)
{
weaponName = name;
damage = dmg;
}
}

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

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq; public class WeaponDatabase : ScriptableObject
{
[SerializeField]
private List<Weapon> database; void OnEnable()
{
if (database == null)
database = new List<Weapon>();
} public void Add(Weapon weapon)
{
database.Add(weapon);
} public void Remove(Weapon weapon)
{
database.Remove(weapon);
} public void RemoveAt(int index)
{
database.RemoveAt(index);
} public int COUNT
{
get { return database.Count; }
} //.ElementAt() requires the System.Linq
public Weapon Weapon(int index)
{
return database.ElementAt(index);
} public void SortAlphabeticallyAtoZ()
{
database.Sort((x, y) => string.Compare(x.weaponName, y.weaponName));
}
}

自定义EditotWindow来操作数据库

 using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System; public class WeaponDatabaseEditor : EditorWindow
{
private enum State
{
BLANK,
EDIT,
ADD
} private State state;
private int selectedWeapon;
private string newWeaponName;
private int newWeaponDamage; private string PROJECT_PATH = Application.dataPath + @"/Database"; private const string DATABASE_PATH = @"Assets/Database/weaponDB.asset"; private WeaponDatabase weapons;
private Vector2 _scrollPos; [MenuItem("BZA/Database/Weapon Database %#w")]
public static void Init()
{
WeaponDatabaseEditor window = EditorWindow.GetWindow<WeaponDatabaseEditor>();
window.minSize = new Vector2(, );
window.Show();
} void OnEnable()
{
if (weapons == null)
LoadDatabase(); state = State.BLANK;
} void OnGUI()
{
EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
DisplayListArea();
DisplayMainArea();
EditorGUILayout.EndHorizontal();
} void LoadDatabase()
{
weapons = (WeaponDatabase)AssetDatabase.LoadAssetAtPath(DATABASE_PATH, typeof(WeaponDatabase)); if (weapons == null)
CreateDatabase();
} void CreateDatabase()
{
if (!Directory.Exists(PROJECT_PATH)) Directory.CreateDirectory(PROJECT_PATH); weapons = ScriptableObject.CreateInstance<WeaponDatabase>();
AssetDatabase.CreateAsset(weapons, DATABASE_PATH);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
} void DisplayListArea()
{
EditorGUILayout.BeginVertical(GUILayout.Width());
EditorGUILayout.Space(); _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, "box", GUILayout.ExpandHeight(true)); for (int cnt = ; cnt < weapons.COUNT; cnt++)
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("-", GUILayout.Width()))
{
weapons.RemoveAt(cnt);
weapons.SortAlphabeticallyAtoZ();
EditorUtility.SetDirty(weapons);
state = State.BLANK;
return;
} if (GUILayout.Button(weapons.Weapon(cnt).weaponName, "box", GUILayout.ExpandWidth(true)))
{
selectedWeapon = cnt;
state = State.EDIT;
} EditorGUILayout.EndHorizontal();
} EditorGUILayout.EndScrollView(); EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
EditorGUILayout.LabelField("Weapons: " + weapons.COUNT, GUILayout.Width()); if (GUILayout.Button("New Weapon"))
state = State.ADD; EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
} void DisplayMainArea()
{
EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
EditorGUILayout.Space(); switch (state)
{
case State.ADD:
DisplayAddMainArea();
break;
case State.EDIT:
DisplayEditMainArea();
break;
default:
DisplayBlankMainArea();
break;
} EditorGUILayout.Space();
EditorGUILayout.EndVertical();
} void DisplayBlankMainArea()
{
EditorGUILayout.LabelField(
"There are 3 things that can be displayed here.\n" +
"1) Weapon info for editing\n" +
"2) Black fields for adding a new weapon\n" +
"3) Blank Area",
GUILayout.ExpandHeight(true));
} void DisplayEditMainArea()
{
weapons.Weapon(selectedWeapon).weaponName = EditorGUILayout.TextField(new GUIContent("Name: "), weapons.Weapon(selectedWeapon).weaponName);
weapons.Weapon(selectedWeapon).damage = int.Parse(EditorGUILayout.TextField(new GUIContent("Damage: "), weapons.Weapon(selectedWeapon).damage.ToString())); EditorGUILayout.Space(); if (GUILayout.Button("Done", GUILayout.Width()))
{
weapons.SortAlphabeticallyAtoZ();
EditorUtility.SetDirty(weapons);
state = State.BLANK;
}
} void DisplayAddMainArea()
{
newWeaponName = EditorGUILayout.TextField(new GUIContent("Name: "), newWeaponName);
newWeaponDamage = Convert.ToInt32(EditorGUILayout.TextField(new GUIContent("Damage: "), newWeaponDamage.ToString())); EditorGUILayout.Space(); if (GUILayout.Button("Done", GUILayout.Width()))
{
weapons.Add(new Weapon(newWeaponName, newWeaponDamage));
weapons.SortAlphabeticallyAtoZ(); newWeaponName = string.Empty;
newWeaponDamage = ;
EditorUtility.SetDirty(weapons);
state = State.BLANK;
}
}
}

Result

EditorWindow edit ScriptableObject