EditorWindow edit ScriptableObject
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的更多相关文章
- Unity ScriptableObject的使用
ScriptableObject主要实现对象序列化的保存,因为是Unity自己的序列化,所以比xml,json序列化方便很多,但相对可控性也比较差 1.Editor下写入和读取测试: using Un ...
- ScriptableObject 对象化的运用
http://www.cnblogs.com/oldman/articles/2409554.html using UnityEngine; using UnityEditor; using Syst ...
- python右键Edit with IDLE
在windows下试用python,Py文件的右键菜单有个Edit with IDLE,虽然这个ide不是那么功能强大,但是胜在方便.对于脚本语言的一般使用来说是足够了.但是有时候,这个菜单就消失了, ...
- Unity EditorWindow知识记录
1.创建EditorWindow using UnityEditor; using UnityEngine; public class ZZEditorWindow : EditorWindow { ...
- PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.
PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...
- eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)
在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...
随机推荐
- Mongodb 3.0 创建用户
MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法. 创建第一个用户(该用户需要有grant权限,即:账号管理的授权权限) ...
- build a git repo and clone
First machine: git init --bare gitrepo.git Second machine: git clone user@server:~/gitrepo.git cd gi ...
- Python学习教程(learning Python)--2.3.4Python函数返回值
本节讨论Python函数返回值问题. Python和C语言一样,也可以在函数结束时返回一个值.但在定义自己的Python函数时,是不需要指定返回值数据类型的,这和Python不关心变量的数据类型是一致 ...
- WPF之旅(二)- XAML
什么是XAML XAML(Extensible Application Markup Lanaguage的简写,发音“zammel”)是用于实例化.NET对象的标记语言.尽管XAML是一种可以用于诸多 ...
- 自学Python一 迷茫中的开端!
有心学习一下Python,多门技术多条路啊.经历了找教程,看代码,写demo,这东西入门容易精通难啊!又因为请了两周婚假彻底忘光光.想了想自己还是边复习边写点什么东西吧.很多技术,新东西都是看了n多, ...
- linux 下的使用 ln 创建 软链接 和 硬链接
linux 下的一个指令 ln 作用: 创建软链接或者硬链接 Linux 系统下每创建一个文件,系统都会为此文件生成一个 index node 简称(inode) ,而每一个文件都包含用户数据(use ...
- 【转】Oracle中如何用一条SQL快速生成10万条测试数据
转自http://blog.csdn.net/welken/article/details/4971887 做数据库开发或管理的人经常要创建大量的测试数据,动不动就需要上万条,如果一条一条的录入, ...
- Openstack:ice-house安装过程
#apt-get install ntpdpkg-reconfigure tzdata --> Asia -->Shuanghai #apt-get install python-mysq ...
- XAML特殊字符
此部分只限制在XAML中,代码中不受此类限制. 1.特殊字符转义 XAML 特殊字符转义 特殊字符 转义 小于号 < < 大于号 > > 取址符 & & 引号 ...
- iOS-打包成ipa
第一步:模拟器选择栏,选择"Generic iOS Device ".早期版本需要断开手机连接,才可以找到. 第二步:选择"Product"菜单下的" ...