I am trying create a small lerp helper script that basically allows me to do something like a small tween with a extension or static method (So I can call it from my other scripts easily like you can do with tween libraries). I know how to do a simple lerp for this with an corutin or in Update, but I don't understand how to make it work with extension or static methods because I can't really use corutines or update there.
EDIT:
With the help of answer bellow I ended Up solving this with async await. Which I can confirm worked in WebGl in this example (I think that is because I didn't run it on a new thread) Here is my code:
public static async void LerpPositionXAsync(this Transform target, float endPosition, float duration)
{
float elapsedTime = 0;
float startPossitionX = target.localPosition.x;
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
target.localPosition = new Vector3(Mathf.Lerp(startPossitionX, endPosition, elapsedTime / duration),
target.localPosition.y);
await Task.Yield();
}
//Makes sure possition is correct after animation
target.localPosition = new Vector3(endPosition,
target.localPosition.y);
await Task.Yield();
}
Note that I am lerping a float value here and not Vector3 but it works the same.
UPDATE
Since I posted this question I have learned that Task.Delay() specifically doesn't work with WebGl so you you should probably be fine if you don't use that or you don't start any tasks. However in Unity 2023 (Not sure for the older ones) they added Awaitable class for the async workflow that works similar to the coroutine and it is supported by WebGl so if you are thinking about using async with WebGl I would recommend that.