Tutorial: "NetSync Objects" for Unity

Tobias admin
edited October 2011 in Any Topic & Chat
NOGAP's Caspar Strandbygaard was kind enough to create a small tutorial for Photon and Unity. We updated it to Photon v2.0.5.1 and streamlined its integration with the SDK (read the readme.txt) and with this post, it becomes available for everyone for the first time.

- Tutorial.pdf and readme.txt (setup)
- Synchronization of objects (type, position and rotation) inside a Photon Lite room
- Extended "Lite" logic
- to handle NetSyncObjects with custom operations
- update players joining the room with respective events
- Complete client and server code
- Unity 2 project with debug GUI for Photon

The tutorial is provided as is and might not be updated each SDK release. However, maybe we can encourage you to give feedback, extend and modify it so we can create updates of this demo, depending on your needs and further Unity development.

http://www.exitgames.com/Download/Photon "Stable.Netsync-Demo-for-Photon-v2-0-5.zip"

In any case:
Thanks, Caspar!


  • Hi there mate

    I am getting the following error when running the tutorial... Server is running fine.

    NullReferenceException: Object reference not set to an instance of an object
    PhotonManager.OperationResult (Byte opCode, Int32 returnCode, System.Collections.Hashtable returnValues, Int16 invocID) (at Assets\Scripts\Photon\PhotonManager.cs:444)
    ExitGames.Client.Photon.NConnect.deserializeNeutron (System.Byte[] inBuff) [0x00000]
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands ()
    ExitGames.Client.Photon.EnetPeer.Service ()
    ExitGames.Client.Photon.PhotonPeer.Service ()
    PhotonManager.Update () (at Assets\Scripts\Photon\PhotonManager.cs:317)

    The players prefab never gets created.

    I am running ExitGames-Photon-Server-SDK_v2-0-5-1 and Netsync-Demo-for-Photon-v2-0-5 and Unity3D 2.6.1f3 and I followed the readme file for the config. I was really looking forward to getting it setup, please help. btw is there anyway to debug the server side in visual studio pro by attaching to the process or something ?
  • PhotonManager.cs:444
    numUsersToCreate = (int)returnValues[(byte)ParameterKey.NumObjects];

    NumObjects is the return value of the new operation "Join" (subclass of the original "JoinOperation")
    [ResponseParameter(Code = (short)ParameterKey.NumObjects)]
    public int NumUsers { get; set; }

    If this value is not part of the response I believe that you are connecting to the original Lite and not the NetSyncObject server.
    That happens easily since Lite is configured as default server if the application id is unknown.
    Please make sure that you really connect to the NetSyncObject server.
  • nickdep7 wrote:
    btw is there anyway to debug the server side in visual studio pro by attaching to the process or something ?
    yes, attach the debugger to the photon.socketserver process.
  • Yes!!!! thnx very much the client was pointing to the Lite server application, the tut missed this. Simply switched

    private string appID = "Lite";
    private string appID = "NetSync";

    thnx for the quick response too....
  • Somehow this slipped through...
    The new Demo zip is fixed.
  • Just a quick note for anyone who runs into the same issue I had. This tutorial seems to hang in Unity 3.0 for me if you are connected and joined to a Photon Server and you quit without leaving the game. It seems to be caused by the way it tries to disconnect in PhotonManager.cs
    private void OnApplicationQuit()
            if (peer != null &&            
                currentPhotonState != PhotonState.Connecting &&
                currentPhotonState != PhotonState.Connected &&            
                currentPhotonState != PhotonState.Disconnecting &&
                currentPhotonState != PhotonState.Disconnected &&
                currentPhotonState != PhotonState.Exception &&
                currentPhotonState != PhotonState.TimeoutDisconnect)
            if (peer != null &&
                currentPhotonState != PhotonState.Disconnected &&
                currentPhotonState != PhotonState.Disconnecting &&
                currentPhotonState != PhotonState.Exception &&
                currentPhotonState != PhotonState.TimeoutDisconnect)
                Debug.Log("Disconnecting now..");
                while (peer.PeerState != 0)

    As you can see it runs a while loop processing the peer thread waiting for the state to reset; however the application quits around it and this seems to hang on my machine. Commenting out the while loop is a work around I'm using but maybe someone else has a better solution?
  • OnApplicationQuit should never loop as it has at best a few ms until its killed. in a standalone you can use CancelQuit to solve that but for webplayers you must be lucky if ANY code can run in there if the tab is closed (not the case for most browsers)
  • Yeah that's kind of what I felt, I'm thinking just peer.StopThread() is the only option because if a web player or the editor stops the application, it's over there is nothing left to loop. The server will just have to treat it as a forced close.
  • True, when the application is being closed, you don't have to wait for acknowledgement of your disconnect. It's nice to send it (if it goes through immediately, it saves the timeout delay before others know we're gone), but we don't have to wait for the result. This should be fine, too:

  • Download the latest Photon 2 SDK here: http://www.exitgames.com/Download/Photon
    NetSync objects are not included with Photon 3.
  • The spiritual successor to "Netsync Objects" is the Photon Unity Networking package for Unity (also only implemented for that engine yet).

    It's much simpler, complete and useful!
    The upcoming update will not even require you to setup a server yourself.