Utility
The Essential Systems comes with some utility functions to help you with various tasks.
Attributes
Optional
- add to a property to let you enable/disable a property in the inspector
public class Test : MonoBehaviour
{
public Optional<float> aVariableYouCanEnable; // the float can be of ANY type
private void Start()
{
if (aVariableYouCanEnable.Enabled) // check if the variable is enabled
{
Debug.Log(aVariableYouCanEnable.Value); // get the actual value of the property & log it
}
}
}
ShowIf
- add as an attribute to a property to show the property if some condition is true
public class Test : MonoBehaviour
{
public enum Setting
{
Foo,
Bar
}
public Setting setting;
[ShowIf("setting", Setting.Foo)]
public float aVariableYouCanHide; // the float can be of ANY type
public bool show;
[ShowIf("show")] // doesn't need to be an enum to check, can be a bool
public float anotherVariableYouCanHide;
private void Start()
{
Debug.Log(aVariableYouCanEnable); // log it
Debug.Log(anotherVariableYouCanHide); // log it
}
}
Singletons
Singleton
- creates a singleton for any MonoBehaviour
public class Test : Singleton<Test> // provide the class as a type
{
public void TestMethod()
{
Debug.Log("Test");
}
}
public class AnotherTestClass : MonoBehaviour
{
private void Start()
{
Test.Instance.TestMethod(); // call the test method by getting the Instance
}
}
SingleSingleton
- creates a singleton for any MonoBehaviour, but this time it gets placed in the DontDestroyOnLoad
scene.
public class Test : SingleSingleton<Test> // provide the class as a type
{
public void TestMethod()
{
Debug.Log("Test");
}
}
public class AnotherTestClass : MonoBehaviour
{
private void Start()
{
Test.Instance.TestMethod(); // call the test method by getting the Instance
}
}
SingletonScriptableObject
- creates a singleton for a ScriptableObject
[CreateAssetMenu(fileName = "Test", menuName = "Essentials/Test")]
public class Test : SingletonScriptableObject<Test> // provide the class as a type
{
public void TestMethod()
{
Debug.Log("Test");
}
}
public class AnotherTestClass : MonoBehaviour
{
private void Start()
{
Test.Instance.TestMethod(); // call the test method by getting the Instance
}
}
Counter
Counter
- manages a timer where you can Reset it, Get the value, See when it's done
public class Test : MonoBehaviour
{
private Counter counter;
private void Awake()
{
counter = new Counter(5); // set the counter to be 5 seconds
}
private void Update()
{
counter.Step(); // steps the time by Time.deltaTime
if (counter.Done()) // checks if the timer is done AND resets it
{
Debug.Log("Counter is done");
}
}
}
Events
Event Manager
- lets you add/remove events easily + you can fetch the events using its name instead of direct reference
Make sure to add the Event Manager
script to any game object in the scene for events to work.
public class Test : MonoBehaviour
{
private void OnEnable()
{
EventManager.Subscribe("someevent", Test);
}
private void OnDisable()
{
EventManager.Unsubscribe("someevent", Test);
}
private void Test()
{
Debug.Log("Event was called");
}
private void Start()
{
EventManager.Trigger("someevent");
}
}
AObservable<T>
- lets you observe any object
public class TestObservable : AObservable<TestObservable>
{
protected override void NotifyObserver(IObserver<Testing1> observer)
{
observer.Notify(this);
}
private void Update()
{
NotifyObservers();
}
public void Test()
{
Debug.Log("Notified");
}
}
public class TestObserver : MonoBehaviour, IObserver<Testing1>
{
public TestObservable observable;
public void Notify(TestObservable other)
{
other.Test();
}
Observable<T>
- lets you observe any property
public class Test : MonoBehaviour
{
public Observable<float> observable; // float can be ANY type
private void Start()
{
observable.V = 1;
}
private void OnEnable()
{
observable.onValueChanged += Log;
}
private void OnDisable()
{
observable.onValueChanged -= Log;
}
private void Log(float v)
{
Debug.Log("variable has changed");
}
}
UI
UI Manager
- use this when you want to show and hide a specific UI panel/parent (for example: the command console)
public class TestUIManager : UIManager<TestUIManager>
{
public override void Show()
{
Debug.Log("It shows the UI");
}
public override void Hide()
{
Debug.Log("It hides the UI");
}
}
Sub UI State Manager
- use this when you want to have a UI element that has states and separate panels for each state (for example: a main menu screen with multiple tabs)
public enum EMainUI
{
Asleep,
Main,
Options
}
public class MainUIManager : UIStateManager<MainUIManager, EMainUI>
{
protected override EMainUI SleepState => EState.ASLEEP;
protected override EMainUI InitState => EState.MAIN;
protected override void OnStateChanged()
{
base.OnStateChanged(); // Enables correct panel.
switch (State)
{
case EMainUI.Asleep:
// do something
break;
case EMainUI.Main:
// do something
break;
case EMainUI.Options:
// do something
break;
}
}
}
UI State Manager
- use this when you want to have a UI element that has states (for example: a pause screen with multiple tabs)
public enum EPauseUI
{
Asleep,
Main,
Options
}
public class PauseUIManager : UIStateManager<PauseUIManager, EPauseUI>
{
protected override EPauseUI SleepState => EPauseUI.Asleep;
protected override EPauseUI InitState => EPauseUI.Main;
protected override void OnStateChanged()
{
switch (State)
{
case EPauseUI.Asleep:
// do something
break;
case EPauseUI.Main:
// do something
break;
case EPauseUI.Options:
// do something
break;
}
}
public override void Show()
{
Pause(true;
base.Show();
}
public override void Hide()
{
Unpause();
base.Hide();
}
}
Last updated