Snippets

ömer faruk sayılır CustomYield : WaitForProgress

Created by Omer F. Sayilir
using UnityEngine;
using System.Collections;
using System;

public class WaitForProgress : CustomYieldInstruction {

	private float nextTime;
	private float waitTime;
	Action<float,bool> OnUpdate;
	private bool useCallback;
	private bool nextUse = true;
	public bool isDone {
		get {
			return nextTime <= Time.realtimeSinceStartup;
		}
	}

	private float m_progress;
	public float progress {
		get {
			m_progress = Mathf.Clamp01(1-(nextTime-Time.realtimeSinceStartup)/waitTime);
			return m_progress;
		}
	}

	public override bool keepWaiting {
		get {
			if (nextUse) {
				nextUse = false;
				this.nextTime = Time.realtimeSinceStartup + waitTime;
			}
			if (useCallback) {
				if (isDone)
				OnUpdate(1.0f,isDone);
				else
				OnUpdate(progress,isDone);
			}
			if (isDone) {
				nextUse = true;
			}
			return !isDone;
		}
	}
	public WaitForProgress (float time)
	{
		waitTime = time;
		useCallback = false;
		OnUpdate = null;
		nextUse = true;
	}

	public WaitForProgress (float time,Action<float,bool> onUpdate)
	{
		waitTime = time;
		OnUpdate = onUpdate;
		nextUse = true;
		if (onUpdate == null) {
			Debug.LogWarning("Null callback has been set. callback won't be used");
			useCallback = false;
		} else
		useCallback = true;
	}
	
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.