For some reason, classes that derive from NetworkBehaviour cannot have generic parameters. Why is that?
This is a showstopper for us at the moment as our MVC implementation makes use of generic parameters and we therefor cannot implement a View that is capable of using unet.
Create an empty project, create a new C# script, then paste this to get the error:
using UnityEngine.Networking;
public class NetworkTest<T> : NetworkBehaviour {
}
[edit]
This is happening on 5.3.4f1 but is marked as fixed in the issue tracker:
We thought we might get a bit more info on the error then.
Turned out, using the code from the link, we can derive from NetworkBehaviour2 using generic parameters just fine. But then again, that version of NetworkBehaviour is from 2015-12-11 (5.3.0f4) so it might be outdated (?).
Any hints? We are not very keen on using a custom NetworkBehaviour, but can live with it for the moment.
Nevermind, the above “solution” will not work as the UNet Weaver does not acknowledge a custom NetworkBehaviour as NetworkBehaviour and therefor [Command] [SyncVar] [ClientRcp] etc. will not work.
Still looking for a solution. Any help appreciated
Im confused as to what you are trying to do. Are you trying to have a generic monobehaviour?
I dont think unity can handle generic components, but a possible workaround might be to inherit from your generic component and make it non generic.
For example
Public class NetworkTestInt : NetworkTest<int>
{
}
Now it isnt generic and should work fine (not tested), although its not as nice to use.
I think the reason for this is that Unity has trouble serializing generics.
Also here is a post of someone asking about generic networkbehaviour, and unity replied with there being no support for this.
In fact, he was the one that issued that bug report you posted above. It seems he was only fixing an error related to generics, but not making generics work or anything.
We are trying to derive from NetworkBehaviour, but the derived class should have a generic parameter, which is not possible for UNet NetworkBehaviours. So your example also won’t work, as NetworkTest needs to derive from NetworkBehaviour (which won’t work)
Thanks for pointing out that post, at least now we know that this is not a bug but a “feature” of some sort
We’ll create a permanent workaround then for our MVC implementation when it comes to networked Views.