Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

Add a system to tag methods so they can only be executed either client-side or server-side #1878

Open Cyantoo opened 2 years ago

Cyantoo commented 2 years ago

In my code (and I would guess that is the case for many other users), I have, for a bunch of my mehtods executed only server-side, the following snippet of code:

if (!NetworkManager.Singleton.IsServer)
        {
            return;
        }

I have the same, with IsClient, for my methods only executed client-side. This system is heavy and error prone: I have to add this snippet manually every time and might forget to do it sometimes.

I believe Mirror has [Server] and [Client] decorators on methods, I'm guessing they're doing what I'm talking about. I think the relevant documentation page is https://mirror-networking.com/docs/api/Mirror.ClientAttribute.html .

I would love to see this feature added to Network for GameObjects, which would make our code both clearer and more stable.

WhippetsAintDogs commented 2 years ago

Me too ! 😍

They seems to use Unity's internal ILPostProcessor to do code weaving.

https://github.dev/vis2k/Mirror/blob/1631402bd7cad3f349f12a4e0d4eb82821c382a9/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorHook.cs#L139

https://forum.unity.com/threads/how-does-unity-do-codegen-and-why-cant-i-do-it-myself.853867/