Last active
December 25, 2024 23:38
-
-
Save Marsgames/a26af659ceb1c17a7e26584672793faf to your computer and use it in GitHub Desktop.
This is my own version of Unity MonoBehavior script Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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