To set the Add callback function in Inspector-Unity3d editor extension

Source: Internet
Author: User

You need to set the add callback function in the editing environment when you do some opening animations or other specified events to trigger a class of functionality.

Initially trying to write directly using delegate, the test found no effect, in Google search to the Unity forum someone mentioned that delegate can not be serialized to inspector use, it is recommended to use Unityevent.

Unityevent, in fact, often used in Ugui, but usually do not know is it.

For example, a button click event is

Directly from the source code of the Ugui Button.

First, provide a class with unityevent


Using unityengine;using system.collections;using unityengine.serialization;using system;using UnityEngine.Events; public class Timeeventplugin:monobehaviour {    [serializefield]public float m_delaytime;[ Serializefield]public Timeeventtrigger m_timeeventtrigger=new Timeeventtrigger ();    private float delaytime;    void Start ()    {        delaytime = M_delaytime;debug. Log ("Timeeventtrigger id=";    }    void Update ()    {        Delaytime-= time.deltatime;if (delaytime <= 0)        {            delaytime = m_delaytime;m_ TimeEventTrigger.m_TimeEvent.Invoke ();}}}    [Serializable]public class Timeeventtrigger{public int id;[ Serializable]public class  Timeevent:unityevent{}[serializefield]public timeevent m_timeevent=new TimeEvent ();}

Then write it corresponding to the editor class Timeeventplugineditor, put in the editor directory

Using unityengine;using system.collections;using unityeditor; [Customeditor (typeof (Timeeventplugin))]public class Timeeventplugineditor:editor{serializedproperty m_ Serializedtimeeventtrigger;    Serializedproperty m_serializedtimeevent; Timeeventtrigger m_timeeventtrigger;void onenable () {M_serializedtimeeventtrigger = SerializedObject.FindProperty (" M_timeeventtrigger ");} public override void Oninspectorgui () {serializedobject.update (); Timeeventplugin plug = target as Timeeventplugin;plug.m_delaytime=editorguilayout.floatfield ("M_delaytime", plug.m_ Delaytime); Editorguilayout.propertyfield (m_serializedtimeeventtrigger,true); Serializedobject.applymodifiedproperties ();}}

Finally write a general script Test.cs, which provides a callback function to register in Timeeventplugin

Using unityengine;using System.collections;public class Test:monobehaviour {    //Use this for initializationvoid Star T ()     {} public    void Timeend ()    {        Debug.Log ("Timeend");    } Update is called once per framevoid update () {}}

Finally, the effect in the editor is rendered as follows:

Example Download:


Copyright NOTICE: This article transferred from Http:// Reprint please take the

To set the Add callback function in Inspector-Unity3d editor extension

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: 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.