不知道如何在Unity3D中使用协同程序

r01*_*128 10 c# unity-game-engine

在Unity3D中,这是我的代码:

void ActivateBuff1(){
    gun.equippedGun.msPerShot /= 2;
    gun.equippedGun.shotsLeftInMag += 10;
    StartCoroutine (WaitRage ());
}

void ActivateBuff2(){
    player.speedModifier *= 1.5f;
    StartCoroutine (WaitSpeed ());
}

IEnumerator WaitRage(){
    yield return new WaitForSeconds(powerUpDuration);
    gun.equippedGun.msPerShot *= 2;
}

IEnumerator WaitSpeed(){
    yield return new WaitForSeconds(powerUpDuration);
    player.speedModifier /= 1.5f;
}
Run Code Online (Sandbox Code Playgroud)

每次玩家进入加电状态时,都会调用其中一个ActivateBuff方法.显然,powerUps效果并不会永远持续下去,所以我常常IEnumerators在等待一定秒数后反转原始方法的效果.但是出于某种原因,IEnumerators永远不会调用代码.请帮助...(并请建议另一种编码方式,因为我知道它不是很干净)

Ser*_*ite 12

在正常情况下,您提供的代码应该可以正常工作.但是,正如评论中所确定的那样,有一个警告 - 如果在延迟WaitForSeconds()完成之前调用协程的Gameobject被禁用/销毁,协程将被停止,其余的代码将根本不被调用.在破坏Gameobject之前你需要等待协程完成,或者让其他一些Gameobject调用协程.

您提到您还在寻找可能简化代码的替代方案 - 您可以考虑Invoke(),这可以让您在指定的延迟后调用方法.(只要你不经常触发这种情况,反射的开销就不会对你的性能产生明显的影响.)所以你的代码可以被重写为更短的代码:

void ActivateBuff1(){
    gun.equippedGun.msPerShot /= 2;
    gun.equippedGun.shotsLeftInMag += 10;
    Invoke("ResetPlayerRage", powerUpDuration);
}

void ActivateBuff2(){
    player.speedModifier *= 1.5f;
    Invoke("ResetPlayerSpeed", powerUpDuration);
}

void ResetPlayerRage(){
    gun.equippedGun.msPerShot *= 2;
}

void ResetPlayerSpeed(){
    player.speedModifier /= 1.5f;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,Invoke()如果Gameobject被破坏也将被取消 - 但与协程不同,如果Gameobject被禁用,它将不会被取消.所以你可以先禁用Gameobject(所以它变得不可见并且不与任何东西交互),然后在运行延迟方法之后将其销毁:

void ActivateBuff1(){
    gun.equippedGun.msPerShot /= 2;
    gun.equippedGun.shotsLeftInMag += 10;
    gameObject.SetActive(false);
    Invoke("ResetPlayerRage", powerUpDuration);
}

void ResetPlayerRage(){
    gun.equippedGun.msPerShot *= 2;
    Destroy(gameObject);
}
Run Code Online (Sandbox Code Playgroud)

以下Invoke()是根据您操作脚本组件或整个Gameobject的方式来停止和协同程序的摘要:

..........................................................................
:                                  :                     :               :
:          Does it stop?           :   InvokeRepeating   :   Coroutine   :
:                                  :                     :               :
:..................................:.....................:...............:
:                                  :                     :               :
:   Disable the script component   :         No          :      No       :
:                                  :                     :               :
:..................................:.....................:...............:
:                                  :                     :               :
:   Destroy the script component   :         Yes         :      Yes      :
:                                  :                     :               :
:..................................:.....................:...............:
:                                  :                     :               :
:   Disable the game object        :         No          :      Yes      :
:                                  :                     :               :
:..................................:.....................:...............:
:                                  :                     :               :
:   Destroy the game object        :         Yes         :      Yes      :
:                                  :                     :               :
:..................................:.....................:...............:

  • 我们已经在网站@serlite dude上为ascii艺术设定了标准!:) (2认同)