mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-23 16:48:23 -05:00
7763f81ede
* 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.
46 lines
1.4 KiB
C#
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>();
|
|
}
|
|
}
|
|
}
|