unity3d_协同进程研究(1)

来源:互联网 发布:天猫 淘宝商城长靴 编辑:程序博客网 时间:2024/06/10 00:10

在开发项目的时候,unity的协同进程是经常用的一个功能。

现在就StartCoroutine()的常用方式列出如下,以便日后学习使用。

方式1:单参传递

  1. void Start(){
  2. StartCoroutine(CoroutineWithMultipleParameters(1.0F));
  3. }
  4. IEnumeratorCoroutineWithMultipleParameters(float aNum){
  5. //stuff
  6. }

方式2:单参传递

  1. void Start(){
  2. StartCoroutine("CoroutineWithMultipleParameters", 1.0F);
  3. }
  4. IEnumeratorCoroutineWithMultipleParameters(float aNum){  
  5. //stuff
  6. }
方式3:直接多参传递

  1. void Start(){
  2. StartCoroutine(CoroutineWithMultipleParameters(1.0F,2.0F,"foo"));
  3. }
  4. IEnumeratorCoroutineWithMultipleParameters(float aNum,float bNum,string aWord){
  5. //stuff
  6. }
方式4:使用参数数组,实现间接传递

  1. void CallCoroutine()
  2. {
  3. float floatParameter= 1.43434f;
  4. string stringParameter ="blablabla";
  5. object[] parms= new object[2]{floatParameter, stringParameter};
  6. StartCoroutine("MyCoroutine", parms);
  7. }
  8.  
  9. voidStopCoroutine()
  10. {
  11. StopCoroutine("MyCoroutine");
  12. }

  13. IEnumeratorMyCoroutine(object[] parms)
  14. {
  15. bool achieved= false;
  16. float floatParameter= (float)parms[0];
  17. string stringParameter= (string)parms[1];
  18. while(achieved==false){
  19. if(---- something-----){
  20. achieved =true;
  21. }
  22. yieldreturnnull;
  23. }
  24. }


稍后会继续讲解一些 协同进程的原理。

enjoy!

原创粉丝点击