[Unity] Coroutine에 맡긴 IEnumarator는 재활용할 수 없습니다.

4403 단어 Unity

개요


IEnumerator에는 Reset 메서드가 있습니다.
Coroutine에 맡긴 IEnumarator의 실험을 재활용할 수 있다.
비망록입니다.

컨디션


Unity5.5.2p4

결실


안돼.
뭐, 내가 예상한 대로.

컨텐트


다음 코드는 GameObject와 함께 제공됩니다.
메소드 On Presed Button을 UGUI 버튼 이벤트와 연결합니다.
using UnityEngine;
using System.Collections;

public class Test2Coroutine : MonoBehaviour
{
    IEnumerator _iEnumerator;
    Coroutine _coroutine;

    private void Awake()
    {
        _iEnumerator = Loop();
    }

    IEnumerator Loop()
    {
        Debug.LogFormat("[LOOP] Start()");
        int i = 0;

        while(true)
        {
            i++;
            Debug.LogFormat("[LOOP] i:{0}", i);
            yield return null;
        }
    }

    public void OnPressedButton()
    {
        Debug.LogFormat("Pressed button.");
        _iEnumerator.Reset();

        if (_coroutine != null)
            StopCoroutine(_coroutine);

        _coroutine = StartCoroutine(_iEnumerator);
    }
}
로그 내용
Pressed button.
NotSupportedException: Operation is not supported.
at My.Test2Coroutine+<Loop>c__Iterator0.Reset () [0x00000]
at My.Test2Coroutine.OnPressedButton () [0x00017]
at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00017]
at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00056]
at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x0000e]
at UnityEngine.Events.UnityEvent.Invoke () [0x00008]
at UnityEngine.UI.Button.Press () [0x00022]
at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00012]
at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00008]
at UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) [0x00073]
리스트 등이 소장하고 있는 아이뉴머레이터라면 가능할 것 같다.
예상대로.

좋은 웹페이지 즐겨찾기