Today, inadvertently read the code written by the great God, see the Invoke function, and then generate interest. Later learned that they have to learn a lot of things.
Here's how to use it.
Invoke is a deferred call function that requires the introduction of a namespace using unityengine.events before using the Invoke function;
1.Invoke ("MethodName", 2)
This is relatively simple, written in a C # script, meaning two seconds after the call, MethodName method.
2.InvokeRepeating ("MethodName")
This method calls invoke multiple times, which is understood to call the MethodName method every two seconds after one second.
3.CancelInvoke ("MethodName")
Cancels the invocation of the MethodName method.
Directly on the code to see more intuitive.
1 usingUnityengine;2 usingSystem.Collections;3 usingunityengine.events;4 5 Public classInvoketest:monobehaviour {6 Publicgameobject prefabs;7 PrivateVector3 v3;8 Public inti =5;9 //Use this for initializationTen voidStart () { OneV3 =NewVector3 (0,0,0); AInvoke ("Testins",1); - //invokerepeating ("Testins", 2, 1); Call Invokerepeating when Untie - } the - //Update is called once per frame - voidUpdate () { - if(v3.x = = -) Cancelinvoke ("Testins"); + } - + voidTestins () { A //v3.x + = i; Call Invokerepeating when Untie at instantiate (prefabs,v3,quaternion.identity); - } - -}
The above code, respectively, is the Invoke method, the Invokerepeating method, the use of the Cancelinvoke method.
One other use of Invoke is to activate Unityevent.
Here is an example.
1 usingUnityengine;2 usingSystem.Collections;3 usingunityengine.events;4 5 Public classTestloader:monobehaviour {6 [Serializefield]7 protectedUnityevent onLoad =Newunityevent ();8 [Serializefield]9 protectedUnityevent unLoad =Newunityevent ();Ten //Use this for initialization One voidStart () { A Load (); - UnLoad (); - } the - //Update is called once per frame - voidUpdate () { - + } - +[ContextMenu ("Load")] A Public voidLoad () { at Onload.invoke (); - } - -[ContextMenu ("UnLoad")] - Public voidUnLoad () { - Unload.invoke (); in } -}
Here are two serialized unityevent, possibly looking at the code is not very intuitive, direct.
Does it feel familiar? Yes, that's what we often see as the onclick below the button.
Let's hang up our own test script for this thing.
But this time we want to invoke the method of the test script, this time we use Invoke.
This will automatically invoke the specified method of the script under Unityevent.
The code for the test script is as follows.
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classOnloadscripts1:monobehaviour {5 Public voidSystemloadmessage () {6Debug.Log ("=======whitetaken=======");7 }8 9 Public voidSystemloadmessage (inti) {TenDebug.Log ("=====whitetaken:"+ i +"======"); One } A } - - usingUnityengine; the usingSystem.Collections; - - Public classOnloadscripts2:monobehaviour { - Public voidSystemloadlog () { +Debug.Log ("--------Whitetaken----------"); - } + } A at usingUnityengine; - usingSystem.Collections; - - Public classUnloadscripts:monobehaviour { - Public voidSystemunload (stringname) { -Debug.Log ("===----+ "+name+"Uninstall:-----====="); in } -}
After running we can see the print results.
Other uses of invoke have not yet been used, and you are welcome to advise me. I will modify the error directly.
Continue studying the singleton mode tonight.
(@WhiteTaken) Usage of Invoke in unity