CSharp · Console
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaitForKey : CustomYieldInstruction
{
private KeyCode _keyToWaitFor;
public WaitForKey(KeyCode keyToWaitFor)
{
_keyToWaitFor = keyToWaitFor;
}
public override bool keepWaiting
{
get
{
if (Input.GetKeyDown(_keyToWaitFor))
{
return false;
}
return true;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineTest : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
while (true)
{
yield return ScaleCoroutine(transform.localScale, new Vector3(3, 3, 3));
yield return new WaitForSeconds(2);
yield return ScaleCoroutine(transform.localScale, new Vector3(1, 1, 1));
yield return new WaitForKey(KeyCode.LeftShift);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StopCoroutine("Start");
}
}
private IEnumerator ScaleCoroutine(Vector3 startScale, Vector3 endScale)
{
float elapsedTime = 0;
float transitionPercentage = 0;
while (transitionPercentage < 1)
{
elapsedTime += Time.deltaTime;
transitionPercentage = elapsedTime / 2f;
transform.localScale = Vector3.Lerp(startScale, endScale, transitionPercentage);
yield return null;
}
}
}