I have a problem with implementing generic user interface interaction.
I have different classes that contain data each for particular interface element. So every UserInterfaceElementComponent
has data only useful for him - all data inherit from UserInterfaceElementComponentData
. For example,
UserInterfaceElementRightCorner
only can use data of type UserInterfaceElementComponentDataRightCorner
.
The problem is in generic part - I have class UserInterfaceManager
that has a generic method:
public static void TransferDataToUIController<T>(T userInterfaceElementComponentData) where T: UserInterfaceElementComponentData
{
_userInterfaceController.TransferUIDataToComponent(userInterfaceElementComponentData);
}
So from any class I can send it any data that inherits from UserInterfaceElementComponentData
. And it asks controller to transfer this data to particular UserInterfaceElementComponent
by geting its enum type:
public void TransferUIDataToComponent<T>(T userInterfaceElementComponentData) where T: UserInterfaceElementComponentData
{
this._userInterfaceElements[(int)userInterfaceElementComponentData.ElementComponentType].AcceptUserInterfaceElementComponentData(userInterfaceElementComponentData);
}
So it gets particular UI component from this array this._userInterfaceElements
that corresponds to this data type. Then it calls to accept data that was given to him.
public class UserInterfaceElementRightCorner : UserInterfaceElementComponent {
public override void AcceptUserInterfaceElementComponentData(UserInterfaceElementComponentData userInterfaceElementComponentData)
{
((UserInterfaceElementComponentDataRightCorner)userInterfaceElementComponentData)._planetInfoPanel.gameObject.SetActive(true);
}
}
Though need to cast this object to corresponding type to get this particular data, so I could use it in that class in way I need, but I would like to avoid object cast because it will be called more than 10000 times per second.
I don't mind changin the whole architecture, I just need to pass object as parameter and it should already be the type I need. Though with generics it seems impossible.
Otherwise, I will have to create a new method for every type in 2 classes and compare types. In general I need to pass particular data type to particular client without casting and with 1-2 methods.
as
I can check very very fast and it won't matter, but now It's just a curiosity if this can actually be done. I always search for the best ways.