unity-atoms/Source/Extensions/MonoBehaviourExtensions.cs
Jeff Campbell 7763f81ede Unified line endings
* Unified all line endings in project to align with .editorconfig; all end-of-line characters have been set to LF and new-lines placed at the end of every file if not present.
2019-04-07 11:56:54 +02:00

46 lines
1.4 KiB
C#

using System;
using System.Collections;
using UnityEngine;
namespace UnityAtoms.Extensions
{
public static class MonoBehaviourExtensions
{
public static void ClearInterval(this MonoBehaviour mb, Coroutine coroutine)
{
mb.StopCoroutine(coroutine);
}
public static Coroutine SetTimeout(this MonoBehaviour mb, Action function, float delay)
{
return mb.StartCoroutine(SetTimeout(function, delay));
}
static IEnumerator SetTimeout(Action function, float delay)
{
yield return new WaitForSeconds(delay);
function();
}
public static Coroutine SetInterval(this MonoBehaviour mb, Action function, float delay)
{
return mb.StartCoroutine(SetInterval(function, delay));
}
static IEnumerator SetInterval(Action function, float delay)
{
while (true && function != null)
{
yield return new WaitForSeconds(delay);
function();
}
}
// Tries to get a component on the the MonoBehaviour's GameObject. If the component doesn't exists it adds it and return the newly added component.
public static T GetOrAddComponent<T>(this MonoBehaviour mb) where T : Component
{
return mb.gameObject.GetOrAddComponent<T>();
}
}
}