Skip to content

Instantly share code, notes, and snippets.

@ando-takahiro
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save ando-takahiro/877cc2ab0490330ab158 to your computer and use it in GitHub Desktop.

Select an option

Save ando-takahiro/877cc2ab0490330ab158 to your computer and use it in GitHub Desktop.
unityのcoroutineは同じGameObject中であっても、MonoBehaviour毎に管理される。StopAllCoroutines()はマニュアル通り、MonoBehaviour毎に消される。
using UnityEngine;
using System.Collections;
public class Coro1 : MonoBehaviour {
IEnumerator Task() {
while (true) {
Debug.Log("Coro1");
yield return new WaitForSeconds(1);
}
}
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 100, 100), "Start1"))
StartCoroutine(Task());
if (GUI.Button(new Rect(10, 110, 100, 100), "Stop1"))
StopAllCoroutines();
}
}
using UnityEngine;
using System.Collections;
public class Coro2 : MonoBehaviour {
IEnumerator Task() {
while (true) {
Debug.Log("Coro2");
yield return new WaitForSeconds(1);
}
}
void OnGUI() {
if (GUI.Button(new Rect(110, 10, 100, 100), "Start2"))
StartCoroutine(Task());
if (GUI.Button(new Rect(110, 110, 100, 100), "Stop2"))
StopAllCoroutines();
}
}
# push "Start1"
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Coro1:OnGUI() (at Assets/Coro1.cs:25)
(Filename: Assets/Coro1.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
# push "Start2"
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Coro2:OnGUI() (at Assets/Coro2.cs:25)
(Filename: Assets/Coro2.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro1
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator0:MoveNext() (at Assets/Coro1.cs:18)
(Filename: Assets/Coro1.cs Line: 18)
# push "Stop1"
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Coro2
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
<Task>c__Iterator1:MoveNext() (at Assets/Coro2.cs:18)
(Filename: Assets/Coro2.cs Line: 18)
Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 0.000366 seconds (Nothing changed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment