Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Marsgames/a26af659ceb1c17a7e26584672793faf to your computer and use it in GitHub Desktop.

Select an option

Save Marsgames/a26af659ceb1c17a7e26584672793faf to your computer and use it in GitHub Desktop.
This is my own version of Unity MonoBehavior script Template
#region Author
/////////////////////////////////////////
// RAPHAEL DAUMAS --> #SCRIPTNAME#
// https://headcrab.fr
// https://github.com/Marsgames
/////////////////////////////////////////
#endregion
using UnityEngine;
public sealed class #SCRIPTNAME# : BaseEntity
{
#region Variables
#endregion Variables
///////////////////////////////////////////////////////////
#region Unity's functions
/// <summary>
/// Start is called before the first frame update
/// </summary>
private void Start()
{
CheckIfSetUp();
}
/// <summary>
/// Update is called once per frame
/// </summary>
private void Update()
{
}
#endregion Unity's functions
///////////////////////////////////////////////////////////
#region Functions
#endregion Functions
///////////////////////////////////////////////////////////
#region Accessors
#endregion Accessors
///////////////////////////////////////////////////////////
#region Utils
/// <summary>
/// Checks if all variables are set up correctly, otherwise close Editor
/// </summary>
protected virtual void CheckIfSetUp()
{
#if UNITY_EDITOR
bool isSetUp = true;
if (randomVariable == null)
{
Debug.LogError($"<b>Random Variable</b> cannot be null in <color=#00f> {name} </color>", gameObject);
isSetUp = false;
}
UnityEditor.EditorApplication.isPlaying = isSetUp;
#endif
}
#endregion Utils
}
#region Author
/////////////////////////////////////////
// RAPHAEL DAUMAS --> BaseEntity
// https://headcrab.fr
// https://github.com/Marsgames
/////////////////////////////////////////
#endregion
using UnityEngine;
public class BaseEntity : MonoBehaviour
{
#region Variables
#endregion Variables
///////////////////////////////////////////////////////////
#region Unity's functions
#endregion Unity's functions
///////////////////////////////////////////////////////////
#region Functions
#endregion Functions
}
#region Author
/////////////////////////////////////////
// RAPHAEL DAUMAS --> Singleton
// https://headcrab.fr
// https://github.com/Marsgames
/////////////////////////////////////////
#endregion
/// <summary>
/// This class is a Singleton, it means that there can be only one instance of it.
/// But it won't destroy the new instance if you try to create a new one, it will just return the existing one.
/// </summary>
/// <typeparam name="T">The type of the class that will be a Singleton</typeparam>
public abstract class StaticInstance<T> : BaseEntity where T : BaseEntity
{
#region Variables
public static T Instance { get; private set; }
#endregion Variables
///////////////////////////////////////////////////////////
#region Unity's functions
protected virtual void Awake()
{
Instance = this as T;
}
protected virtual void OnApplicationQuit()
{
Instance = null;
Destroy(gameObject);
}
#endregion Unity's functions
}
/// <summary>
/// This class is a Singleton, it means that there can be only one instance of it.
/// </summary>
/// <typeparam name="T">The type of the class that will be a Singleton</typeparam>
public abstract class Singleton<T> : StaticInstance<T> where T : BaseEntity
{
#region Unity's functions
protected override void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(Instance.gameObject);
}
base.Awake();
}
#endregion Unity's functions
}
/// <summary>
/// This class is a Singleton, it means that there can be only one instance of it.
/// But it won't be destroyed when you change the scene.
/// </summary>
/// <typeparam name="T">The type of the class that will be a Singleton</typeparam>
public abstract class PersistentSingleton<T> : Singleton<T> where T : BaseEntity
{
#region Unity's functions
protected override void Awake()
{
base.Awake();
DontDestroyOnLoad(gameObject);
}
#endregion Unity's functions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment