Hii, i need to create a simple loop but without using coroutine we can easily do that outside of unity but as far as i know in unity you must use IEnumerator to do this but that means i need an instance and coroutines does not return values without use of callbacks which i can’t use since i need the whole loop not the callback from the start or end
So in unity i did something like in this example but the loop only runs for one frame and then stop which is weird since when you start an loop it should run until reach the ends or freeze the whole machine if there’s no end so how can i do a loop without using coroutines?
public void Stuff()
{
float value02 = SimpleLoop();
}
public float SimpleLoop()
{
float value = 0f;
while (value < 10f)
{
value++;
return value;
}
return value;
}
I’m trying to have my own Lerp functions running through a Utility class like plugins as DoTween where you just need to call the function and not implement the whole code behind it that’s why i’m trying to do such thing but if there’s another way to do it i would appreciate if someone could guide me.
Cheers.
(…) coroutines does not return values without use of callbacks which i can’t use since i need the whole loop not the callback from the start or end (…)
But you can callback from the middle of the coroutine, right?
void Start ()
{
StartCoroutine(
Interpolate( 0 , 1 , 3f , (t) => Debug.Log($"t: {t:G8}") )
);
}
IEnumerator Interpolate ( float src , float dst , float duration , System.Action<float> onValueChanged )
{
float time = 0;
onValueChanged( src );
while( time < duration )
{
yield return null;
time += Time.deltaTime;
float interpolated = Mathf.Lerp( src , dst , time/duration );
onValueChanged( interpolated );
}
}
_
do you know if it’s possible call coroutines in static classes without instances?
No. But there is a workaround:
Routine.cs
using System.Collections;
using UnityEngine;
public class Routine : MonoBehaviour
{
static Routine _singleton;
public static void Start ( IEnumerator routine ) => _singleton.StartCoroutine( routine );
public static void Stop ( IEnumerator routine ) => _singleton.StopCoroutine( routine );
public static void Stop ( Coroutine routine ) => _singleton.StopCoroutine( routine );
public static void StopAll () => _singleton.StopAllCoroutines();
[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.BeforeSceneLoad )]
static void OnRuntimeMethodLoad ()
=> GameObject.DontDestroyOnLoad( _singleton = new GameObject($"#{nameof(Routine)}").AddComponent<Routine>() );
}
TickTock.cs (tester)
using System.Collections;
using UnityEngine;
public class TickTock : MonoBehaviour
{
IEnumerator Start ()
{
Routine.Start( Ticks("tick") );
yield return new WaitForSeconds(0.5f);
Routine.Start( Ticks(" tock") );
yield return new WaitForSeconds(3f);
}
IEnumerator Ticks ( string message )
{
var sec = new WaitForSeconds(1);
while( true )
{
yield return sec;
Debug.Log(message);
}
}
}
public async void Stuff()
{
float value02 = await SimpleLoop();
}
public async Task<float> SimpleLoop()
{
float value = 0f;
while (value < 10f)
value++;
return value;
}
Thanks everyone for the help i’m trying all the answer but i’m pretty sure that, now i’ve enough knowledge to do what i need :).
Cheers.