Unity Inspector automatically associates references to components (ii)

Source: Internet
Author: User

The reference is actively associated with the declared variable name.

Use this association to refer to two different ways
1. Give you the component inheritance Monoautoquote click on the Component Inspector button to execute
2. Add the [Aautoquote] attribute to your component class by Plateface/setselectgameref execution

[Aautoquote]  Public class monoautoquote:monobehaviour, iautoquote{}

Public interface Iautoquote {}

public class Aautoquote:attribute {}

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingUnityeditor;usingSystem.Reflection;//[Caneditmultipleobjects][Customeditor (typeof(Monoautoquote),true)] Public classautoquoteeditor:editor{ Public Override voidOninspectorgui () {Base.        Oninspectorgui (); if(Guilayout.button ("associating child node references") {Component C= Target asComponent; if(c! =NULL) Autioquotemenu.setref (c, C.gameobject); }    }} Public classautioquotemenu{[MenuItem ("plateface/setselectgameref%&a")]     Public Static voidSetRef () {gameobject o=Selection.activegameobject; if(O! =NULL) {component[] CAry= o.getcomponents<component>(); foreach(varCinchCAry) {System.Type ComponentType=C.gettype (); if((typeof(Monobehaviour). IsAssignableFrom (componenttype)) | |Ishasattribute (ComponentType))                {SetRef (c, O); }            }        }    }     Public Static voidSetRef (Component C, Gameobject o) {System.Type T=C.gettype (); varInfolist = T.getfields (BindingFlags.Public |bindingflags.instance); System.Text.StringBuilder SB=NewSystem.Text.StringBuilder (); stringName =string.        Empty; foreach(varIteminchinfolist) {            varFieldType =item.            FieldType; if((typeof(Monobehaviour). IsAssignableFrom (FieldType))) {if(item. Name.startswith ("m") ) {Name= Item. Name.substring (1); Transform TR=O.transform.find (name); if(tr = =NULL) {debug.logerror (name+"reference not found"); Continue; } Component com=tr.                    Getcomponent (FieldType); Item.                SetValue (c, COM); }            }        }    }     Public Static BOOLIshasattribute (System.Type Type) {system.object[] olist= Type. GetCustomAttributes (typeof(Aautoquote),false); foreach(varItemincholist) {            if(item asAautoquote)! =NULL)                return true; }        return false; }}

Unity Inspector automatically associates references to components (ii)

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.