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 ...
随机推荐
- 蒙牛乳业六厂—第一家MES工厂
在上海西门子工业自动化(SIAS)与蒙牛液态奶事业部以及蒙牛集团信息中心的共同努力下,经过项目组成员1年半时间的具体实施,中国乳品行业第一个真正意义上的生产执行系统MES,于2008年6月在蒙牛乳业集 ...
- 没有Where条件下group by走索引
C:\Users\Administrator>sqlplus /nolog SQL :: Copyright (c) , , Oracle. All rights reserved. SQL&g ...
- 关于datagridview的一些操作
1.绑定datatable时,会显示出不需要显示的列可以加datagridview.AutoGenerateColumns = false; 2.如果datagridview的某列是数值型的,有小数, ...
- [整理归档]30 common tasks you perform using the GUI that you can do faster in Windows PowerShell
主要内容来自于 http://channel9.msdn.com/Events/TechEd/Australia/2014/DCI316 可以下载PPT以及视频,个人只是整理一下平时常用的 NetWo ...
- Nunit单元测试的使用
先建立一个需要测试的项目 安装nunit 通过nuget安装Install-Package Nunit 类前加[TestFixture] 要测试的方法前加[Test] using System; u ...
- EMVTag系列12《卡片内部风险管理数据》
Ø 9F53 连续脱机交易限制数(国际-货币) L: 1 -C(有条件):如果执行国际-货币频度检查 PBOC专有数据元. 不使用指定应用货币的连续脱机交易次数最大数,超过后交易请求联机 模板 ...
- DB2行转列(多维度)
多维度下进行行列转换,下面的行列转换时根据客户,所属银行机构进行的行列转换. -----------------建表 CREATE TABLE CUST_BANK_INFO ( CUST_ID ), ...
- oracle11g 新特性 - rman自动备份控制文件延迟
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...
- Android greenDao的简单配置和使用
最近自学做东西的时候用到了一个收藏的功能,然后我想把东西存放到SQLite当中,然而自己传值的时候都是用到的实体类,所以存起来也比较麻烦,所以从网上找到一个greenDao的开源框架非常火,不仅效率高 ...
- 代码编译方式 ant +ivy
Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.由Apache软件基金会所提供. 没用过ant,了解一下,无非就这些功能, 编 ...