Skip to content

MonoBehaviour Extension

Andrzej Kebab edited this page Feb 2, 2024 · 1 revision

UtilityLibrary.Unity.Runtime

SetActive

Sets the active state of the GameObject associated with the MonoBehaviour.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.SetActive(true);

Parameters:

  • active: The active state to set.

IsPrefab

Checks if the GameObject associated with the MonoBehaviour is a prefab.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
bool isPrefab = scriptInstance.IsPrefab();

Returns:

  • bool: true if the GameObject is a prefab, false otherwise.

ExecuteWhen

Executes an action when a condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteWhen(() => YourAction(), () => YourCondition());

Parameters:

  • action: The action to execute.
  • condition: The condition to check.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteUntil

Executes an action until a condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteUntil(() => YourAction(), () => YourCondition());

Parameters:

  • action: The action to execute.
  • condition: The condition to check.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteCountUntil

Executes an action a certain number of times or until a condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteCountUntil(() => YourAction(), count, () => YourCondition());

Parameters:

  • action: The action to execute.
  • count: The maximum number of times to execute the action.
  • condition: The condition to check.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteIntervalUntil

Executes an action at a certain interval a certain number of times or until a condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteIntervalUntil(() => YourAction(), interval, count, () => YourCondition());

Parameters:

  • action: The action to execute.
  • interval: The interval in seconds between each execution.
  • count: The maximum number of times to execute the action.
  • condition: The condition to check.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteIntervalUntil

Executes an action at a certain interval until a condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteIntervalUntil(() => YourAction(), interval, () => YourCondition());

Parameters:

  • action: The action to execute.
  • interval: The interval in seconds between each execution.
  • condition: The condition to check.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteIfUntil

Executes an action if a certain condition is met until another condition is met.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteIfUntil(() => YourAction(), () => ExecuteCondition(), () => FinishCondition());

Parameters:

  • action: The action to execute.
  • executeCondition: The condition to check for executing the action.
  • finishCondition: The condition to check for finishing the execution.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteDelay

Executes an action after a certain delay.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteDelay(() => YourAction(), delayInSeconds, true);

Parameters:

  • action: The action to execute.
  • seconds: The delay in seconds before executing the action.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteCount

Executes an action a certain number of times.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteCount(() => YourAction(), count);

Parameters:

  • action: The action to execute.
  • count: The number of times to execute the action.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteInterval

Executes an action at a certain interval indefinitely.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteInterval(() => YourAction(), interval, true);

Parameters:

  • action: The action to execute.
  • interval: The interval in seconds between each execution.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteInterval

Executes an action at a certain interval for a certain duration.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteInterval(() => YourAction(), interval, duration, true);

Parameters:

  • action: The action to execute.
  • interval: The interval in seconds between each execution.
  • duration: The duration in seconds for the execution.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteIntervalCount

Executes an action at a certain interval a certain number of times.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteIntervalCount(() => YourAction(), count, interval, true);

Parameters:

  • action: The action to execute.
  • count: The number of times to execute the action.
  • interval: The interval in seconds between each execution.
  • timeScale: Whether

to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteNextFrame

Executes an action on the next frame.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteNextFrame(() => YourAction());

Parameters:

  • action: The action to execute.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteEndOfFrame

Executes an action at the end of the frame.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteEndOfFrame(() => YourAction());

Parameters:

  • action: The action to execute.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteAfterFrames

Executes an action after a certain number of frames.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteAfterFrames(() => YourAction(), numberOfFrames);

Parameters:

  • action: The action to execute.
  • frames: The number of frames to wait before executing the action.

Returns:

  • Coroutine: A Coroutine running the execution.

ExecuteWhenFixedUpdate

Executes an action when the next FixedUpdate is called.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.ExecuteWhenFixedUpdate(() => YourAction());

Parameters:

  • action: The action to execute.

Returns:

  • Coroutine: A Coroutine running the execution.

RestartCoroutine

Restarts a Coroutine.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.RestartCoroutine("YourCoroutineMethodName");

Parameters:

  • methodName: The name of the Coroutine method.

Returns:

  • Coroutine: A Coroutine running the execution.

StartCoroutineSync

Starts a Coroutine in a synchronized manner.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.StartCoroutineSync(YourCoroutine());

Parameters:

  • enumerator: The IEnumerator to run.

Returns:

  • Coroutine: A Coroutine running the execution.

WaitForSeconds

Waits for a certain amount of time.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.StartCoroutine(WaitForSeconds(seconds, true));

Parameters:

  • second: The amount of time to wait in seconds.
  • timeScale: Whether to use scaled or unscaled time.

Returns:

  • Coroutine: A Coroutine running the execution.

WaitForSeconds

Waits for a certain amount of time using scaled time.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.StartCoroutine(WaitForSeconds(seconds));

Parameters:

  • second: The amount of time to wait in seconds.

Returns:

  • Coroutine: A Coroutine running the execution.

WaitForSecondsRealtime

Waits for a certain amount of time using unscaled time.

MonoBehaviour scriptInstance = // Obtain a MonoBehaviour instance;
scriptInstance.StartCoroutine(WaitForSecondsRealtime(seconds));

Parameters:

  • second: The amount of time to wait in seconds.

Returns:

  • Coroutine: A Coroutine running the execution.

Clone this wiki locally