When I was ready to go to bed so late, I went to study for a while. Find a headline curious point to go in. is actually a custom drawing property. In the last few days this problem has puzzled me, did not expect a few minutes to solve the problem. I spent half a day using reflection to solve ... If we want to have properties customize the drawing form, we will first think of the extension inspectoreditor, which is explained in this chapter. Original properties can be drawn independently, bright blind my eyes!
Steps:
1. Define the Drawing attribute description class (to be drawn with data supplied to –> drawing class –>)
2. Defining properties Custom Drawing Class
3. Add your features to a field
1. Drawing Feature Description Classes
usingUnityengine;usingSystem.Collections;usingUnityeditor;/// <summary>///must inherit PropertyAttribute/// </summary> Public classMyrangeattribute:propertyattribute {//draw the data you need Public floatmin; Public floatMax; Public stringlabel; PublicMyrangeattribute (floatMinfloatMaxstringLabel ="") { This. Min =min; This. Max =Max; This. Label =label; } }
2. Attribute Drawing class
usingUnityengine;usingSystem.Collections;usingUnityeditor;/// <summary>///inherit Propertydrawer, must be placed under the editor folder/// </summary>[Custompropertydrawer (typeof(Myrangeattribute))] Public classMyrangeattributedrawer:propertydrawer { Public Override voidOngui (Rect position, Serializedproperty property, Guicontent label) {//Get drawing Description classMyrangeattribute range = This. Attribute asMyrangeattribute; //The judging field is that type, making a different drawing if(Property.propertytype = =serializedpropertytype.float) {Debug.Log ("Float Type"); Editorgui.slider (Position,property,range.min,range.max,label); }Else if(Property.propertytype = =Serializedpropertytype.integer) {Debug.Log ("integer Type"); if(Range.label! =string. Empty) {Label.text=Range.label; } editorgui.intslider (Position, property, (int) Range.min, (int) Range.max, label); } }}
Component:
using Unityengine; using System.Collections; Public class mycompoment:monobehaviour { [Myrangeattribute (0,ten," blood value " )] publicint int1; Public string string1;}
Project Resource map:
Unity Extended Property Custom Drawing