Unity extension Custom Event Send component

Source: Internet
Author: User

In writing the project, I created a method that requires an int parameter . I remember it was uievent. Trigger cannot pass a number directly, it can only pass a Gameobject attribute past (=.= that value does not want to be defined on the component)

The UIButton message component cannot pass parameters.

Uievent Trigger Component passes gameobject properties of a component past

It feels like these two are not exactly right. Write a SendMessage component

:

Component classes:

usingUnityengine;usingSystem.Collections;namespaceplateface{/// <summary>    ///Message Send component/// </summary>     Public classSendmessageto:monobehaviour { Public enumMestype {@default, @int, @string, @object} PublicGameobject Tager;  Public stringfunctionname;  PublicMestype MessageType =[email protected];  Public int@int;  Public string@string;  PublicGameobject @object; voidOnClick () {if(Tager! =NULL&& functionname! ="")            {                Switch(MessageType) { Case[email protected]: Tager.                        SendMessage (functionname);  Break;  Case[email protected]: Tager.                        SendMessage (functionname, @int);  Break;  Case[email protected]: Tager.                        SendMessage (functionname, @string);  Break;  Case[email protected]: Tager.                        SendMessage (functionname, @object);  Break; default:                         Break; }            }        }            }}

Inspectoriedtor extension

usingUnityengine;usingUnityeditor;usingSystem.Collections;namespaceplateface{[Customeditor (typeof(Sendmessageto))]  Public classSendmessagetoeditor:editor { Public Override voidOninspectorgui () {Sendmessageto Item= Target asSendmessageto;  Serializedobject.update (); //Serialization UpdatesItem.tager= Editorguilayout.objectfield ("Target:", Item.tager,typeof(Gameobject)) asGameobject; Item.functionname= Editorguilayout.textfield ("Method Name:", Item.functionname).            ToString (); Editorguilayout.propertyfield (Serializedobject.findproperty ("MessageType")); Switch(item. MessageType) { Case[email protected]: Break;  Case[email protected]: [email protected]= Editorguilayout.intfield ("parameter (int)", [email protected]);  Break;  Case[email protected]: [email protected]= Editorguilayout.textfield ("parameter (String)", [email protected]).                    ToString ();  Break;  Case[email protected]: [email protected]= Editorguilayout.objectfield ("parameter (object)", [email protected],typeof(Gameobject)) asGameobject;  Break; }            //updates the edited data. serializedobject.applymodifiedproperties (); }      }}

Three script preview graphs:

Unity extension Custom Event Send component

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.