【Unity】指定したフレーム数を待つカスタムコルーチン

DEVELOP, Unity

using System;
using UnityEngine;
using System.Collections;
 
public class WaitForFrame : CustomYieldInstruction
{
    private readonly int BASE_FPS = 60;
 
    int m_endFrame = 0;
    int m_waitFrame = 0;
 
    public nsWaitForFrame(int _waitFrame)
    {
        float rate = (float)Application.targetFrameRate / (float)BASE_FPS;
        m_waitFrame = (int)(_waitFrame * rate);
        m_endFrame = Time.frameCount;
    }
 
    public override bool keepWaiting
    {
        get
        {
            if(Time.timeScale<= 0.0f)
            {
                return true;
            }
 
            int wait = (int)((float)m_waitFrame / Time.timeScale);
            if (m_endFrame + wait <= Time.frameCount)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

Posted by kazupon