coroutine

4/3/2024 Unity3d

coroutine

https://docs.unity3d.com/ScriptReference/Coroutine.html (opens new window)

https://docs.unity3d.com/ScriptReference/WaitForSeconds.html (opens new window)

functions

functions parameter describe
StartCoroutine(funtion,...) 1.lua function

2.lua function parameters

return:cs_coroutine,instance
function is parameterless

Create a coroutine and return the coroutine controller and coroutine instance
coroutine.yield(time) UnityEngine.WaitForSeconds
cs_coroutine.stop(instance) 1.instance stop the coroutine

Note:

When the script is destroyed, please stop using the coroutine and call it during OnDestroy. Otherwise, some errors will occur.

--Please refer to the example:
function OnDestroy()
    if testCo~=nil and cs_coroutine~=nil then
     cs_coroutine.stop(testCo)
    end
     log('OnDestroy')
end
1
2
3
4
5
6
7

Wait

--Wait 1 second, wait 1 frame
 coroutine.yield(UnityEngine.WaitForSeconds(1))
 coroutine.yield(UnityEngine.WaitForEndOfFrame())
1
2
3

example

Every time wait for 1s, the 5th second, stop the coroutine

function Start()
    --cs_coroutine: Unity 协程
    --testCo 协程启动后返回值
    cs_coroutine, testCo = StartCoroutine(test)
end

function test()
    log('coroutine a started')
    count =0
    while true do
        coroutine.yield(UnityEngine.WaitForSeconds(1))
        count=count+1
        log('i am coroutine '..tostring(count))       
        if count ==5 then
            break
        end
    end
    
    cs_coroutine.stop(testCo)
    testCo = nil
    log('coroutine a stoped')

end

function OnDestroy()
    if testCo~=nil and cs_coroutine~=nil then
     cs_coroutine.stop(testCo)
    end
     log('OnDestroy')
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Coroutine passing parameters

local test1=function(x,f)
    print('coroutine a started',x,f) --coroutine a started    12    hello
    
coroutine.yield(UnityEngine.WaitForSeconds(1))
    print('coroutine b finish')
    
coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
    print('i am coroutine a')
end

 function Start()
  cs_coroutine, testCo = StartCoroutine(test1,12,"hello")
 end
 
 function OnDestroy()
    if testCo~=nil and cs_coroutine~=nil then
     cs_coroutine.stop(testCo)
    end
     log('OnDestroy')
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Resource loading

function Start()
    local url ="https://mainbackend.woogiu.com/woogi/0.1/upload/googlespeech/elevenlabs/4/5/45723029cceaf8a3f7742040c9eaf2ef.mp3"
   cs_coroutine, testCo = StartCoroutine(test,url,onFinished)
end

function test(url,callback)
	local w_req = UnityEngine.Networking.UnityWebRequest(url, UnityEngine.Networking.UnityWebRequest.kHttpVerbGET)
 --Method 1
 --[[
    w_req:SendWebRequest()
    while w_req.isDone==false do
        coroutine.yield(UnityEngine.WaitForSeconds(1))
    end
 ]]
  --Method 2
 coroutine.yield(w_req:SendWebRequest()) 
 
	log("error---"..tostring(w_req.error))
 log("bytes---"..tostring(w_req.downloadedBytes))
  
 if callback~=nil then
     callback(true)
 end
  
	log('coroutine a stoped')
end

function onFinished(isOk)
    log('Download Complete '..tostring(isOk))
end 

function OnDestroy()
    if testCo~=nil and cs_coroutine~=nil then
     cs_coroutine.stop(testCo)
    end
     log('OnDestroy')
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Last Updated: Wed, 18 Dec 2024 07:47:17 GMT