Hi Guys,
I had trouble properly wording the title of this post so if I messed it up I do apologize.
My problem is that when I expose a field with type of some Interface the Editor doesn’t display it in the properties list of an object to which this script is assigned.
So, this works just fine :
...
public List<object> Lights;
...
but this doesn’t, I cannot see the field in the editor:
...
// Where ILight is an Interface defined elsewhere in the code
public List<ILight> Lights;
...
It’s a disadvantage that the second method doesn’t work. In order to get the desired effect I use a CLight (class) in place of ILight but that makes implementation a bit dirty. Since I may have an object that implements ILight and ISwitch at the same time I don’t want to have CLightWithSwitch inherit from CLight just so that we could assign it to another object’s properties in the editor.
If this is a limitation of some sort then let me tell you what I’m after and maybe you could recommend a better approach to solving this issue altogether.
I have a simple switch class CSwitch (which would have been an Interface if not for the problem above) that lets me toggle an objects state to On or Off. It has a SetState() and GetState() methods exposed. I also have a class CLightWithSwitch that inherits CLight (this actually controls the light) which in turn inherits CSwitch. I attach CLightWithSwitch to a prefab that has a Light (unity light that is) as one of its children.
So, when the player clicks on the object in game I simply call the CLightWithSwitch.SetState(somestate) and the prefab reacts accordingly (i.e turns the light on or off).
Now this works fine if I have just the one prefab I want to affect. But there are cases when there is a light switch in the room that should set states of 10 light prefabs or more. So for this case I made a CSwitchRelay which inherits CSwitch but adds a collection property (as per the problem described above) List Switches. Its SetState() method is also modified to iterate the list and set the state of all items to the desired state.
CSwitchRelay is then assigned to the actual light switch object in the scene. I then click on the Switches property in the editor and select what lights I want to affect with this SwitchRelay. So when an object representing a light switch is clicked in game all of the lights assigned to the Lights property go On or Off.
So, my original intention was to use ISwitch interfaces so that any object that needs to be turned on or off in the game could just implement it and become listed in the selection box of any CSwitchRelay’s Switches property. But like I said above, I then can’t get the editor to show me the List property.
I don’t like the current implementation because its very cumbersome for no reason. Not being able to expose fields of Interface type makes the implementation ugly and requires me to resort to inheritance when I shouldn’t have to.
I hope this makes sense. And if anyone has any insights into this issue I would really appreciate some pointers.