So I am working on an indie game in Unity and I've recently decided to move all of my UIs (user interfaces) into Canvas game objects, which is fine and dandy, but it does require me to re-write some code. Specifically, I have the following architecture that relies on the interface game objects having a GUITexture component:
public class InterfaceHandler : MonoBehaviour
{
/// <summary>
/// Hides the GUI Texture.
/// </summary>
public void Hide ()
{
GetComponent<GUITexture> ().enabled = false;
}
/// <summary>
/// Shows the GUI Texture.
/// </summary>
public virtual void Show ()
{
GetComponent<GUITexture> ().enabled = true;
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
public class InterfaceHandlerA : InterfaceHandler
{
public Texture2D[] Images;
private int _imageIndex = 0;
public int ImageIndex
{
get
{
return _imageIndex;
}
set
{
_imageIndex = value;
}
}
// Use this for initialization
void Start ()
{
Hide ();
}
// Update is called once per frame
void Update ()
{
GetComponent<GUITexture>().texture = Images[_imageIndex];
}
}
The problem is that with Canvas, there are no GUITextures. Instead I have to use RawImage or Image. So now I have to either re-write the code above (there are many more classes that inherit from InterfaceHandler, InterfaceHandlerA is just one of them) or write all new code that does the exact same thing, except with RawImage or Image instead of GUITexture.
This seems like a good opportunity to stop, take a step back, and really think about how to properly engineer this thing. I want it to be re-usable and maintainable, so that if a year from now Unity decides to replace Canvas with something else and there are no more RawImages, my code would still work. I am thinking maybe some kind of Dependency Injection might work here, or perhaps Generics, i.e. something like this:
public class InterfaceHandler<T> : MonoBehaviour
I tried the approach above, but it tells me that type T doesn't have a property called 'enabled'.
Any other ideas? What would be the best way to architecture this?