Explains how to implement multi-selection effects like camera level masks in Unity's Inspector panel

Source: Internet
Author: User

First, what we need now is this, I've defined a script with an enumeration and a public enum type variable, and now we can control what the enumeration variable chooses by hand in the Inspector panel in Unity's project. Just like the kind of camera we have in unity that has the option of masking levels, but now the problem is that we can only choose one, and we can't select more, and now all we have to do is write a piece of the enumeration variable that allows it to choose more code.

As follows:

The variable name here I changed to a custom Chinese, in fact, in the code you can change according to their own needs

As in the previous article, let me tell you something. There are 3 scripts in this project, namely:

(1) enumflagsexample. cs


(2) enumflagsattribute. cs

(3) enumflagsattributedrawer. cs

——————————————————————————————————————————————————————————————————————————

Scripts (1)enumflagsexample. cs We're right, it's attached to the main camera.

(2) Enumflagsattribute. CS (3)Enumflagsattributedrawer. cs Where there are no mounts, both of them are on [ Enumflagsattribute(" like to eat fruit ") function writing

——————————————————————————————————————————————————————————————————————————

(1) enumflagsexample. cs

Using unityengine;using system.collections;///<summary>///script location: Main camera///Scripting feature: Achieve multi-selection effects like camera level masks// Created: 2015.07.26///</summary>public class Enumflagsexample:monobehaviour {//define an enum public enum fruit {apple, Pineapple,cherry}//Use one of our own custom features [Enumflagsattribute ("Fruit of your Liking")]//This allows you to manipulate public fruit by selecting the way in the panel  likefruit ;}

(2)Enumflagsattribute. CS

Using unityengine;using system.collections;///<summary>///script location: required to be placed in the Editor folder, in fact, can also run/// Scripting features: Implement a slider bar in the Inspector panel to control the size of the variable//create event: 2015.07.26///</summary>public class Enumflagsattribute: PropertyAttribute {//when script (1) uses [Enumflagsattribute ("Favorite Fruit")] when//"Like fruit" is this stringpublic string heatfoods;// A parameter of type [Enumflagsattribute ("string")]public Enumflagsattribute (String label) {heatfoods = label;} If you need a type with no parameters, you can use the overloaded public Enumflagsattribute () {}}

(3)Enumflagsattributedrawer, C1

Using unityengine;using system.collections;//introduces the editor namespace using Unityeditor; Using the renderer, if you use this custom attribute of [Enumflagsattribute]//execute the following code to supplement Enumflagsattribute [Custompropertydrawer (typeof ( Enumflagsattribute))]public class Enumflagsattributedrawer:propertydrawer {public override void Ongui (Rect _position, Serializedproperty _property, Guicontent _lable) {//attribute is an attribute in the PropertyAttribute class// All properties in Enumflagsattribute can be called enumflagsattribute flags = attribute as enumflagsattribute;//The value of the enumeration is finally a number, If you want to get the value that it represents or contains, you must draw a drop-down menu by bitwise operation, enumeration type _property.intvalue = Editorgui.maskfield (_position, Flags.heatfoods, _ Property.intvalue, _property.enumdisplaynames);}}




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

Explains how to implement multi-selection effects like camera level masks in Unity's 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.