Skip to content

Instantly share code, notes, and snippets.

@dskjal
Last active September 29, 2016 01:45
Show Gist options
  • Save dskjal/2647d3f75187ddd41da4f68a2f1f7c48 to your computer and use it in GitHub Desktop.
Save dskjal/2647d3f75187ddd41da4f68a2f1f7c48 to your computer and use it in GitHub Desktop.
Unity でヒープにできるごみを減らすコルーチン
using UnityEngine;
using System.Collections;
public class Coroutine : MonoBehaviour {
[SerializeField]float interval = 1;
void Start () {
StartCoroutine(coroutine());
}
/* 一般的な実装
IEnumerator coroutine(){
while(true){
// 実行するコード
yield return new WaitForSeconds(interval);
}
}
*/
IEnumerator coroutine() {
var oldInterval = interval;
var wfs = new WaitForSeconds(interval);
while (true) {
// 実行するコード
if (oldInterval != interval) {
wfs = new WaitForSeconds(interval);
oldInterval = interval;
}
yield return wfs;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment