Unity Extended Property Custom Drawing

Source: Internet
Author: User

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

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.