實用的呼叫函式-Invoke() vs InvokeRepeating()
實用的呼叫函式-Invoke() vs InvokeRepeating()
Unity:
Version: 2020.3.12f1.590.2 Personal
Revision: 2020.3/release b3b2c6512326
Built: Tue, 08 Jun 2021 08:46:44 GMT
1.Invoke:
syntax:
Invoke(string MethodName,float t)
Invoke(string MethodName,float t)
description:
Invoke the method MethodName() after t seconds.
Invoke the method MethodName() after t seconds.
[Note]
(1)Notice that "MethodName" is considered as MethodName() in compiler.
(2)Notice that because of (1), MethodName must be a string, so it must be quotated such as "MethodName".
(2)Notice that because of (1), MethodName must be a string, so it must be quotated such as "MethodName".
(3)Notice that because of (1), the rule of naming of MethodName is almost same as the rule of naming of a variable in C#. i.e. it only can contain a uppercase or lowercase or _ or digits in the string method name. It can not contain a bracket or other special character.
(4)Notice that if t is set to zero, the MethodName() will be called in the beginning of then next Update() cycle.
[Review]
Debug.Log("a") means that a will be output in Unity Console.
more details on the website:
code 1:
void Start()
{
Debug.Log("Start.");
Invoke("MethodName",0f);
Debug.Log("end Start.");
}
}
void Update()
{
Debug.Log("Update.");
Debug.Log("end Update.");
}
private void MethodName()
{
Debug.Log("Mehod Name.");
}
ideal execution process:
Debug.Log("Start.");
Debug.Log("end Start.");
Debug.Log("Mehod Name.");
Debug.Log("Update.");
Debug.Log("end Update.");
Debug.Log("Update.");
Debug.Log("end Update.");
code 2:
void Start()
{
Debug.Log("Start.");
Debug.Log("end Start.");
}
}
void Update()
{
Debug.Log("Update.");
Invoke("MethodName",0f);
Debug.Log("end Update.");
}
ideal execution process:
Debug.Log("Start.");
Debug.Log("end Start.");
Debug.Log("Update.");
Debug.Log("end Update.");
Debug.Log("Mehod Name.");
Debug.Log("Update.");
Debug.Log("end Update.");
Debug.Log("Mehod Name.");
Debug.Log("Update.");
Debug.Log("end Update.");
2.InvokeRepeating:
syntax:
InvokeRepeating(string MethodName,float t,float rate)
description:
Invoke the method MethodName() after t seconds and repeat it infinitely with rate seconds.
InvokeRepeating(string MethodName,float t,float rate)
description:
Invoke the method MethodName() after t seconds and repeat it infinitely with rate seconds.
i.e. after t seconds , the MethodName() will be called. after it was executed , MethodName() will be called in rate seconds.
[Note]
Notice that if time scale is set to zero, the MethodName() will not be called.
Notice that if t is set to zero, the MethodName() will be called at the beginning of next Update() every time.
3. my code and description
e.g.1void Start()
{
}
void Update()
{
Debug.Log("Update!");
Invoke("InstantiateABullet", 0f);
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...etc
e.g.2
void Start()
{
Invoke("InstantiateABullet", 0f);
Debug.Log("end Start!");
}
void Update()
{
Debug.Log("Update!");
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.3
void Start()
{
InvokeRepeating("InstantiateABullet", 1.0f,2f);
Debug.Log("end Start!");
}
void Update()
{
Debug.Log("Update!");
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Instantiate A Bullet!");
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.5
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 2f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Update!");
Debug.Log("end Update!");
Debug.Log("Update!");
Debug.Log("end Update!");
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("end Update!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.3
void Start()
{
InvokeRepeating("InstantiateABullet", 1.0f,2f);
Debug.Log("end Start!");
}
void Update()
{
Debug.Log("Update!");
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Instantiate A Bullet!");
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.5
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 2f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Update!");
Debug.Log("end Update!");
Debug.Log("Update!");
Debug.Log("end Update!");
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.6
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 0f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.7
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 0f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 0f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
e.g.7
void Start()
{
timer = 0f;
Debug.Log("end Start!");
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
}
void FixedUpdate()
{
InvokeRepeating("InstantiateABullet", 1.0f, 0f);
Debug.Log("Update!");
timer = Time.deltaTime;
Debug.Log("Time.deltaTime=" + Time.deltaTime.ToString());
Debug.Log("end Update!");
}
private void InstantiateABullet()
{
Debug.Log("Instantiate A Bullet!");
}
execution process:
Debug.Log("end Start!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
->
Debug.Log("Instantiate A Bullet!");
->
Debug.Log("Update!");
->
Debug.Log("end Update!");
->
...
Comments
Post a Comment