Skip to content

Instantly share code, notes, and snippets.

@mntrskl
Last active February 25, 2021 09:14
Show Gist options
  • Select an option

  • Save mntrskl/dbc2b51b8c26d2accee69919139c930b to your computer and use it in GitHub Desktop.

Select an option

Save mntrskl/dbc2b51b8c26d2accee69919139c930b to your computer and use it in GitHub Desktop.
Unity
static public class UnityEngineExtensions
{
/// <summary>
/// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added.
/// </summary>
/// <typeparam name="T">The type of Component to return.</typeparam>
/// <param name="gameObject">The GameObject this Component is attached to.</param>
/// <returns>Component</returns>
static public T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleCallback : MonoBehaviour
{
void Start()
{
StartCoroutine(SampleRoutine(() =>
{
Debug.Log("onComplete");
}));
}
IEnumerator SampleRoutine(Action onComplete = null)
{
yield return new WaitForSeconds(5.0f);
onComplete?.Invoke();
}
}
using UnityEngine;
/// <summary>
/// Inherit from this base class to create a singleton.
/// e.g. public class MyClassName : Singleton<MyClassName> {}
/// </summary>
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
// Check to see if we're about to be destroyed.
private static bool m_ShuttingDown = false;
private static object m_Lock = new object();
private static T m_Instance;
/// <summary>
/// Access singleton instance through this propriety.
/// </summary>
public static T Instance
{
get
{
if (m_ShuttingDown)
{
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
"' already destroyed. Returning null.");
return null;
}
lock (m_Lock)
{
if (m_Instance == null)
{
// Search for existing instance.
m_Instance = (T)FindObjectOfType(typeof(T));
// Create new instance if one doesn't already exist.
if (m_Instance == null)
{
// Need to create a new GameObject to attach the singleton to.
var singletonObject = new GameObject();
m_Instance = singletonObject.AddComponent<T>();
singletonObject.name = typeof(T).ToString() + " (Singleton)";
// Make instance persistent.
DontDestroyOnLoad(singletonObject);
}
}
return m_Instance;
}
}
}
private void OnApplicationQuit()
{
m_ShuttingDown = true;
}
private void OnDestroy()
{
m_ShuttingDown = true;
}
}
using System;
using UnityEngine;
public class Player : MonoBehaviour
{
public static Action<int> OnDamageReceived();
public int Health;
void Start()
{
Health = 10;
}
void Damage()
{
Health--;
OnDamageReceived?.Invoke(Health);
}
}
using UnityEngine;
public class UIManager : MonoBehaviour {
private void OnEnable() {
Player.OnDamageReceived += UpdateHealth;
}
private void OnEnable() {
Player.OnDamageReceived -= UpdateHealth;
}
private void UpdateHealth(int health) {
Debug.Log("Current Health " + health);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment