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
TimeEventPlugin.cs
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=" +m_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:
Http://pan.baidu.com/s/1bnjmKdL
Copyright NOTICE: This article transferred from Http://blog.csdn.net/huutu Reprint please take the http://www.thisisgame.com.cn
To set the Add callback function in Inspector-Unity3d editor extension