Unity customization, exposing attributes to the Inspector panel

Source: Internet
Author: User

Sun Guangdong 2015, 7, 12

Many of Unity's editor features are implemented through feature attribute.

Then we need to expand inspector ourselves and write attribute ourselves.


Why do you have to do that?

Encapsulation features are more elegant in order to write object-oriented programs. The following script enables the contents of a property (that is, members with Getter/setter) to be displayed on Unity's inspector.
This makes it possible to keep the fields of the class confidential and restrict all external access, only through property access,


Look at the code!

Using System; [AttributeUsage (Attributetargets.property)] public class Exposepropertyattribute:attribute {}

Scripts in "Assets/editor"

Using unityeditor;using unityengine;using system;using system.collections;using system.collections.generic;using System.Reflection; public static class Exposeproperties{public static void Expose (Propertyfield[] properties) {var emptyoptions = new Guilayo UTOPTION[0]; Editorguilayout.beginvertical (emptyoptions); foreach (Propertyfield field in properties) { Editorguilayout.beginhorizontal (emptyoptions); if (field. Type = = Serializedpropertytype.integer) {var oldValue = (int) field. GetValue (); var newvalue = Editorguilayout.intfield (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = serializedpropertytype.float) {var oldValue = (Float) field. GetValue (); var newvalue = Editorguilayout.floatfield (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = Serializedpropertytype.boolean) {var oldValue = (bool) field. GetValue (); var newvalue = editorguilayout.toggle (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = serializedpropertytype.string) {var oldValue = (String) field. GetValue (); var newvalue = Editorguilayout.textfield (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = Serializedpropertytype.vector2) {var oldValue = (Vector2) field. GetValue (); var newvalue = Editorguilayout.vector2field (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = Serializedpropertytype.vector3) {var oldValue = (Vector3) field. GetValue (); var newvalue = Editorguilayout.vector3field (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = serializedpropertytype.enum) {var oldValue = (Enum) field. GetValue (); var newvalue = editorguilayout.enumpopup (field. Name, OldValue, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} else if (field. Type = = Serializedpropertytype.objectreference) {Unityengine.object OldValue = (unityengine.object) field. GetValue (); Unityengine.object newvalue = Editorguilayout.objectfield (field. Name, OldValue, Field.Info.PropertyType, emptyoptions); if (oldValue! = newvalue) field. SetValue (newvalue);} Editorguilayout.endhorizontal ();} Editorguilayout.endvertical ();} public static propertyfield[] GetProperties (object obj) {var fields = new list<propertyfield> (); propertyinfo[] Infos = obj. GetType (). GetProperties (BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in infos) {if (!) ( Info. CanRead && Info. CanWrite)) continue; object[] Attributes = info. GetCustomAttributes (TRUE); BOOL isexposed = False;foreach (object o in Attributes) if (o.gettype () = = typeof (Exposepropertyattribute)) {isexposed = Tru E;break;} if (!isexposed) continue; var type = serializedpropertytype.integer;if (Propertyfield.getpropertytype (info, out type)) {var field = new Propertyfield (obj, info, type); ADD (field);}} return fields. ToArray ();}}public class Propertyfield{object obj; PropertyInfo info; Serializedpropertytype type; MethodInfo getter; MethodInfo setter; Public PropertyInfo Info{get {return Info,}} public Serializedpropertytype Type{get {return Type;}} public String Name {get {return Objectnames.nicifyvariablename (info). Name); }} public Propertyfield (Object obj, PropertyInfo info, Serializedpropertytype type) {this.obj = Obj;this.info = Info;this. type = type; Getter = This.info.GetGetMethod (); setter = This.info.GetSetMethod ();} public Object GetValue () {return getter. Invoke (obj, null); }public void SetValue (object value) {Setter. Invoke (obj, new[] {value}); public static bool Getpropertytype (PropertyInfo info, out Serializedpropertytype propertyType) {Type type = info. Propertytype;propertytype = serializedpropertytype.generic;if (type = = typeof (int)) PropertyType = Serializedpropertytype.integer;else if (type = = typeof (float)) PropertyType = Serializedpropertytype.float;else if ( Type = = typeof (bool)) PropertyType = Serializedpropertytype.boolean;else if (type = = typeof (String)) PropertyType = Serializedpropertytype.string;else if ( Type = = typeof (Vector2)) PropertyType = Serializedpropertytype.vector2;else if (type = = typeof (Vector3)) PropertyType = Serializedpropertytype.vector3;else if (type. Isenum) PropertyType = Serializedpropertytype.enum;else if (typeof (Monobehaviour). IsAssignableFrom (type)) PropertyType = Serializedpropertytype.objectreference;return PropertyType! = Serializedpropertytype.generic;}}


Using Unityengine; public class Exposablemonobehaviour:monobehaviour {}

Scripts under the "Assets/editor" folder

Using unityeditor;using unityengine;using System.Collections; [Customeditor (typeof (Exposablemonobehaviour), true)] public class exposablemonobehavioureditor:editor{ Exposablemonobehaviour m_instance; Propertyfield[] M_fields; public virtual void onenable () {m_instance = target as Exposablemonobehaviour;m_fields = Exposeproperties.getproperties ( m_instance);} public override void Oninspectorgui () {if (m_instance = = null) return;this. Drawdefaultinspector (); Exposeproperties.expose (M_fields);}}

Note
This will allow classes to inherit from Exposablemonobehaviour, which will enhance the exposure setter and getter methods. There are no noticeable performance issues.
You may have seen the above code, only the following types are currently exposed (although it is easy to implement other types):

-Integer-float-boolean-string-vector2-vector3-enum-object Reference (Monobehavior)

"Use:"
Now create a class extension Exposablemonobehaviour and use the properties. property must have getter and setter accessors and [Exposeproperty] attribute settings, otherwise the property will not be displayed.
To save the property value when you play, you must add [Serializefield] to the field of the attribute. Unfortunately, this field is exposed to the editor, so you must explicitly hide it, using [Hideininspector].

Example: Hanging on an object at random

Using Unityengine; public class Mytype:exposablemonobehaviour{[hideininspector, Serializefield] int m_someint;[ Hideininspector, Serializefield] float m_somefloat; [Hideininspector, Serializefield] bool M_somebool; [Hideininspector, Serializefield] string m_etc;        [Hideininspector, Serializefield] Monobehaviour m_obj; [Exposeproperty] public int Someint{get {return m_someint;} set {M_someint = value;}} [Exposeproperty] public float Somefloat{get {return m_somefloat;} set {m_somefloat = value;}} [Exposeproperty] public bool Somebool{get {return m_somebool;} set {M_somebool = value;}} [Exposeproperty] public string Somestring{get {return m_etc;} set {m_etc = value;}}         [Exposeproperty] public monobehaviour somescript{get {return m_obj;} set {m_obj = value;}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unity customization, exposing attributes to the Inspector panel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.