Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Scripts/Utilities.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Assets/Scripts/Utilities/MonoBehaviourExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public static class MonoBehaviourExtensions
{
#region Public API

public static Coroutine Wait(this MonoBehaviour monoBehaviour, float seconds)
{
return monoBehaviour.StartCoroutine(Wait(seconds));
}

public static Coroutine InvokeDelayed(this MonoBehaviour monoBehaviour, UnityAction action, float delayInSeconds)
{
return monoBehaviour.StartCoroutine(InnerInvokeDelayed(action, delayInSeconds));
}

public static Coroutine InvokeRepeated(this MonoBehaviour monoBehaviour, UnityAction action, float delayInSeconds, float repeatRateInSeconds)
{
return monoBehaviour.StartCoroutine(InnerInvokeRepeated(action, delayInSeconds, repeatRateInSeconds));
}

#endregion

#region Private methods
private static IEnumerator Wait(float seconds)
{
yield return new WaitForSeconds(seconds);
}

private static IEnumerator InnerInvokeDelayed(UnityAction action, float delayInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);
action.Invoke();
}

private static IEnumerator InnerInvokeRepeated(UnityAction action, float delayInSeconds, float repeatRateInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);

action.Invoke();

while (true)
{
yield return new WaitForSeconds(repeatRateInSeconds);

action.Invoke();
}
}

#endregion

}
11 changes: 11 additions & 0 deletions Assets/Scripts/Utilities/MonoBehaviourExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions Assets/Scripts/Utilities/MonoBehaviourExtensionsDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public class MonoBehaviourExtensionsDemo : MonoBehaviour
{
public int i = 0;

private IEnumerator Start()
{
yield return (this.Wait(1));
Debug.Log("done waiting");

UnityAction helloWorld1 = () => HelloWorld();
yield return(this.InvokeDelayed(helloWorld1, 2)); //this will halt everything below until it finishes invoking

UnityAction helloWorld2 = () => HelloWorld(2);
yield return(this.InvokeDelayed(helloWorld2, 2)); //this will halt everything below until it finishes invoking

UnityAction helloWorld3= () => HelloWorld(200);
this.InvokeDelayed(helloWorld3, 2); //this will NOT halt everything below until it finishes invoking, it just invokes it with a delay

UnityAction countedHelloWorld = () => CountedHelloWorld();
var repeatedRoutine = this.InvokeRepeated(countedHelloWorld, 0, 0.5f);

yield return (this.Wait(3));
Debug.Log("stopping now");
StopCoroutine(repeatedRoutine);

}

public void HelloWorld()
{
Debug.Log("hello world!");
}

public void HelloWorld(int num)
{
Debug.Log("hello world # " + num);
}

public void CountedHelloWorld()
{
HelloWorld(i);
i++;
}

}
11 changes: 11 additions & 0 deletions Assets/Scripts/Utilities/MonoBehaviourExtensionsDemo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.