I have a few classes that extend MonoBehaviour, for example; WidgetController and PanelController.
From these classes I extend various other classes; ButtonController : WidgetController and PanelLoginController : PanelController.
The definition of the WidgetController has a public PanelController attribute
public class WidgetController : MonoBehaviour {
public PanelController panelController;
…
}
Inherently all of the different types of widget controllers (buttons, checkboxes etc) have a PanelController panelControoler attribute. In the editor they are referenced to extended classes of PanelControllers, such as a PanelLoginController.
Any attributes or methods in PanelController are avaiable in the extended WidgetController, as expected. However, when I create a specialized method or attribute in a extented PanelContrller, the PanelController panelController attribute does not reflect these.
Example:
public class PanelLoginController : PanelController {
public UIInput inputUsername;
public UIInput inputPassword;
private void AttemptLogin() {
string username = inputUsername.label.text;
string password = inputPassword.label.text;
}
}
public class PanelLoginButtonLogin : ButtonController {
void OnClick() {
Debug.Log(panelController.GetType()); // returns PanelLoginController
panelController.AttemptLogin(); // error
panelController.inputUsername = ""; // error
panelController.Invoke("AttemptLogin", 0f); // this works, but I cannot pass parameters and it seems like a work around
}
}
I am trying to wrap my head around Generics, but havn’t quite figured them out yet. I believe they may be the required course of action, but I am not entirely sure.
One work around would be to define a new attribute of the extented panel controller in each extented widget controller- on construct cast the PanelController panelController to that attribute. This does not seem like the proper approach, and would require more code maintenance.
Any suggestions or solutions are greatly appreciated.
Cheers!