I have made myself an gesuture recognition class with C# (similar to Input). The thing is, it is a children of MonoBehaviour (so it cannot be made static) because it needs Update, but I want it to be lazy initialized, so I don´t ever forget to include it in my projects.
I know I could use a singleton to solve this, but I don´t want to write GestureRecognition.Instance every time I need to check something, I want it to be clean and similar to Unity´s Input. Is this possible?
No need for a second class. You can hide the singleton inside the class itself:
public class GestureRecognition : MonoBehaviour
{
private static GestureRecognition m_Instance = null;
private static GestureRecognition Instance
{
get
{
if (m_Instance == null)
{
m_Instance = (GestureRecognition) FindObjectOfType(typeof(GestureRecognition ));
if (m_Instance == null)
{
m_Instance = (new GamaObject("GestureRecognition")).AddComponent<GestureRecognition>();
}
DontDestroyOnLoad(m_Instance.gameObject);
}
return m_Instance;
}
}
public static void RecognizeGesture()
{
Instance._RecognizeGesture();
}
private void _RecognizeGesture()
{
// do something on the instance
}
private void Update()
{
// ...
}
}
whenever you access one of the static methods of your class it automatically creates an instance. You can’t even access Instance from outside since it’s only used in other static methods.
That said, to answer the question, it’s generally bad design to implement such things the lazy way. That way your class is tightly bound to other classes. For example: If you create your class using Input.touches. You add more and more stuff and everything’s working fine. Now you want it to work on PC and web as well… No way without rewriting the whole thing with mouse input.
There are so many great software design patterns out there, but most people pick the worst one (the singleton pattern).
If it’s that important to you not to use the .Instance part, you could create a static class that will have its own instance and expose all the functionality that your GestureRecognition class has:
// this is a new class, not the one you created
public class GestureRecognition {
public static GestureRecognitionInternal instance;
public static void RecognizeGesture() {
instance.RecognizeGesture();
}
}
// this is your original class
public class GestureRecognitionInternal : MonoBehaviour {
public void Awake() {
GestureRecognition.instance = this;
}
public void RecognizeGesture() {
// your code here
}
}
I had an idea. Maybe instead of having the GestureRecognition as child of MonoBehaviour, you put it as a static class and then have a GestureRecognitionUpdater class that controls the update of the GestureRecognition class.
Example:
public class GestureRecognitionUpdater : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void Update()
{
GestureRecognition.Update();
}
}
And then in the GestureRecognition you could do something like this:
public class GestureRecognition
{
private static bool initialized;
public static void Initialize()
{
if (!initialized)
{
new GameObject("GestureRecognitionUpdate").AddComponent<GestureRecognitionUpdater>();
initialized = true;
}
}
public static void Update()
{
//Do the update stuff
}
public static void GetGesture()
{
Initialize();
//Do the get gesture stuff
}
}
This way you get the static access to all the functions of GestureRecognition class and it also gets Updated each frame and you won’t have to double up on writing wrappers for each function.
You would have to either call the Initialize function on all the other functions to check for if the Updater has been initialized, or you have to call the Initialize function on the start of your game.