有时需要将繁琐的属性,字段等变量统一管理,这时全部放在一起,显得特别拥挤,修改的欲望都没了,这个时候就需要Editor编辑,修改Inspector界面。
1、 属性类如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Item_Profile : MonoBehaviour {
#region common
// ********* common values ************//
public int item_id = -1; // this id assigned by item database manager
public Texture2D icon;
public string comment;
public enum TYPE
{
None,
Weapon,
Shield,
Armor,
Recipe,
Parts,
Consumables,
Bomb
} ;
public TYPE type;
public int rare = 1000; // the rare level
public Color color = Color.white;
public int buy,sell;
public int limit_stack = 1; // maximun # of stack
public GameObject model;
#endregion
#region weapon
// ********* weapon values ************//
public enum WEAPON_TYPE
{
None,
Single_Sword
} ;
public WEAPON_TYPE weapon_type;
public int damage;
public int damage_r; // randomly range
public int damage_metal;
public int damage_metal_r;
public int damage_wood;
public int damage_wood_r;
public int damage_water;
public int damage_water_r;
public int damage_fire;
public int damage_fire_r;
public int damage_earth;
public int damage_earth_r;
//
#endregion
#region shield & armor
// ********* shield & armor values ************//
// public Material armor_material;
// only for armor
public enum CLAN_TYPE
{
None,
Elven,
Orc
} ;
public CLAN_TYPE clan = CLAN_TYPE.Orc;
// only for armor
public enum ARMOR_TYPE
{
None,
Chest,
Head,
Shoulder,
Belt
} ;
public ARMOR_TYPE armor_type;
public int def;
public int def_metal;
public int def_wood;
public int def_water;
public int def_fire;
public int def_earth;
//
#endregion
#region recipe
// ********* recipe values ************//
[System.Serializable]
public class Recipe_Parts
{
public Item_Profile part;
public int number;
} ;
public Recipe_Parts[] parts;
public Item_Profile final_product ;
//
#endregion
}
2、 编辑类如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
// 这里是需要改变指定的类在Inspector中的布局,这里我们需要改变的类就是Item_Profile。
[CustomEditor(typeof(Item_Profile))]
public class Item_Profile_Editor : Editor
{
void OnEnable ()
{
}
// 这个函数,看名字就觉得很NB,这个就是在编辑状态下,Inspector的GUI的布局。
public override void OnInspectorGUI ()
{
// target是Editor里的一个属性,指的就是我们需要改变的那个类,这里做强转换,不解释。
Item_Profile item = target as Item_Profile;
// 序列化更新,不解释。
serializedObject.Update();
// Add your code here.
// ********* common values **********//
item.item_id = EditorGUILayout.IntField("auto_id", item.item_id);
item.icon = EditorGUILayout.ObjectField ("icon", item.icon, typeof(Texture2D)) as Texture2D;
item.name = EditorGUILayout.TextField ("name", item.name);
item.comment = EditorGUILayout.TextField("comment",item.comment);
GUILayout.Space(5f);
EditorGUILayout.PropertyField( serializedObject.FindProperty("type") );
GUILayout.Space(5f);
item.rare = EditorGUILayout.IntField("rare",item.rare);
EditorGUILayout.PropertyField( serializedObject.FindProperty("color") );
GUILayout.Space(5f);
item.buy = EditorGUILayout.IntField("buy", item.buy);
item.sell = EditorGUILayout.IntField("sell",item.sell);
GUILayout.Space(5f);
item.limit_stack = EditorGUILayout.IntField("limit stack", item.limit_stack);
item.model = EditorGUILayout.ObjectField ("model", item.model, typeof(GameObject)) as GameObject;
// ********* core values **********//
GUILayout.Space(10f);
if (NGUIEditorTools.DrawHeader("Info:"))
{
NGUIEditorTools.BeginContents();
//GUILayout.BeginVertical();
switch (item.type)
{
case Item_Profile.TYPE.Weapon:
Show_Weapon(item);
break;
case Item_Profile.TYPE.Armor:
case Item_Profile.TYPE.Shield:
Show_Armor_Shield(item);
break;
case Item_Profile.TYPE.Recipe:
Show_Recipe(item);
break;
case Item_Profile.TYPE.Parts:
Show_Parts(item);
break;
case Item_Profile.TYPE.Consumables:
break;
case Item_Profile.TYPE.Bomb:
break;
}
//GUILayout.EndVertical();
NGUIEditorTools.EndContents();
}
// ……
// 更新编辑后的数据。
serializedObject.ApplyModifiedProperties();
}
void Show_Recipe(Item_Profile item)
{
EditorGUILayout.PropertyField( serializedObject.FindProperty("parts"), true );
EditorGUILayout.PropertyField( serializedObject.FindProperty("final_product"), true );
}
void Show_Weapon(Item_Profile item)
{
GUILayout.Space(5f);
EditorGUILayout.PropertyField( serializedObject.FindProperty("weapon_type") );
item.damage = EditorGUILayout.IntField("damage", item.damage);
item.damage_r = EditorGUILayout.IntField("damage_r", item.damage_r);
item.damage_metal = EditorGUILayout.IntField("damage_metal", item.damage_metal);
item.damage_metal_r = EditorGUILayout.IntField("damage", item.damage_metal_r);
item.damage_wood = EditorGUILayout.IntField("damage_wood", item.damage_wood);
item.damage_wood_r = EditorGUILayout.IntField("damage_wood_r", item.damage_wood_r);
item.damage_water = EditorGUILayout.IntField("damage_water", item.damage_water);
item.damage_water_r = EditorGUILayout.IntField("damage_water_r", item.damage_water_r);
item.damage_fire = EditorGUILayout.IntField("damage_fire", item.damage_fire);
item.damage_fire_r = EditorGUILayout.IntField("damage_fire_r", item.damage_fire_r);
item.damage_earth = EditorGUILayout.IntField("damage_earth", item.damage_earth);
item.damage_earth_r = EditorGUILayout.IntField("damage_earth_r", item.damage_earth_r);
}
void Show_Armor_Shield(Item_Profile item)
{
GUILayout.Space(5f);
EditorGUILayout.PropertyField( serializedObject.FindProperty("clan") );
GUILayout.Space(5f);
EditorGUILayout.PropertyField( serializedObject.FindProperty("armor_type") );
item.def = EditorGUILayout.IntField("def", item.def);
item.def_metal = EditorGUILayout.IntField("def_metal", item.def_metal);
item.def_wood = EditorGUILayout.IntField("def_wood", item.def_wood);
item.def_water = EditorGUILayout.IntField("def_water", item.def_water);
item.def_fire = EditorGUILayout.IntField("def_fire", item.def_fire);
item.def_earth = EditorGUILayout.IntField("def_earth", item.def_earth);
}
void Show_Parts(Item_Profile item)
{
}
}
其他:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Item_Database_Manager))]
public class Item_Database_Manager_Editor : Editor
{
void OnEnable ()
{
}
public override void OnInspectorGUI ()
{
Item_Database_Manager manager = target as Item_Database_Manager;
serializedObject.Update();
EditorGUILayout.PropertyField( serializedObject.FindProperty("empty_item"));
if(GUILayout.Button("Create a empty item"))
{
manager.Create_Empty_Item();
}
if(GUILayout.Button("Sort"))
{
manager.ReSort();
}
EditorGUILayout.PropertyField( serializedObject.FindProperty("items"),true);
serializedObject.ApplyModifiedProperties();
EditorGUILayout.HelpBox("The Item Database required non-null emelent in the array and each item only appare once.", MessageType.Info);
}
}
【注意】
简单数据类型的处理方法:
item.item_id = EditorGUILayout.IntField("auto_id",item.item_id); //显示并更新item.item_id, “auto_id”为在Inspector中显示的名称。
内置的对象的处理方法:
item.icon =EditorGUILayout.ObjectField ("icon", item.icon, typeof(Texture2D)) asTexture2D;
对枚举类型的处理方法:
EditorGUILayout.PropertyField(serializedObject.FindProperty(“type"));
【参考】http://www.unitymanual.com/thread-25147-1-1.html