How do I sync non-player GameObject properties in UNet/Unity5?

前端 未结 4 893
遇见更好的自我
遇见更好的自我 2020-12-09 22:32

I\'m working on and learning some basics of Unity 5, UNET, and networking. I made a simple 3D game where you go around and change the colors of objects. But I want to make i

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 22:58

    Unity 5.3.2p3 Assign Client Authority to Non Player Objects

    For anyone interested in setting this up this is my approach

    Client Side OnLocalPlayer component -> Call commands to assign and remove object authority by passing through the objects NetworkInstanceId. You can add any UI to call these methods on this component

    Server Side

        [Command]
        void CmdAssignObjectAuthority(NetworkInstanceId netInstanceId)
        {
            // Assign authority of this objects network instance id to the client
            NetworkServer.objects[netInstanceId].AssignClientAuthority(connectionToClient);
        }
    
        [Command]
        void CmdRemoveObjectAuthority(NetworkInstanceId netInstanceId)
        {
            // Removes the  authority of this object network instance id to the client
            NetworkServer.objects[netInstanceId].RemoveClientAuthority(connectionToClient);
        }  
    

    Client Side 3. Object component ->
    OnStartAuthority() - allowed to send commands to server OnStopAuthority() - not allowed to send commands to server

    That's all there is to it!

提交回复
热议问题