public static void OnReceive(MsgSvrCli_SyncPosition_Ntf ntf) { Client.Main client = Client.Main.Instance; foreach (ObjectTransform objTrans in ntf.transforms) { Common.Sphere sphere = null; if (false == client.spheres.TryGetValue(objTrans.id, out sphere)) { return; } if (true == client.syncPosition) { sphere.transform.localPosition = objTrans.localPosition; } if (true == client.syncRotation) { sphere.transform.rotation = objTrans.rotation; } if (true == client.syncVelocity) { sphere.rigidBody.velocity = objTrans.velocity; } } }
private void Start() { btnConnect.onClick.AddListener(() => { if (null != session) { session.Close(); } session = new Gamnet.Client.Session(); session.OnConnectEvent += () => { session.RegisterHandler <MsgSvrCli_CreateRoom_Ans>(MsgSvrCli_CreateRoom_Ans.MSG_ID, CreateRoom.OnReceive); session.RegisterHandler <MsgSvrCli_CreateSphere_Ntf>(MsgSvrCli_CreateSphere_Ntf.MSG_ID, CreateSphere.OnReceive); session.RegisterHandler <MsgSvrCli_SyncPosition_Ntf>(MsgSvrCli_SyncPosition_Ntf.MSG_ID, SuncPosition.OnReceive); MsgCliSvr_CreateRoom_Req req = new MsgCliSvr_CreateRoom_Req(); Send(req); }; session.OnErrorEvent += (System.Exception e) => { Debug.Log(e.Message + "\n" + e.StackTrace.ToString()); }; session.OnCloseEvent += () => { foreach (var itr in spheres) { Common.Sphere sphere = itr.Value; sphere.transform.SetParent(null); GameObject.Destroy(sphere.gameObject); } spheres.Clear(); session.UnregisterHandler(MsgSvrCli_CreateRoom_Ans.MSG_ID); session.UnregisterHandler(MsgSvrCli_CreateSphere_Ntf.MSG_ID); session.UnregisterHandler(MsgSvrCli_SyncPosition_Ntf.MSG_ID); session.OnConnectEvent = null; session.OnErrorEvent = null; session.OnCloseEvent = null; }; session.AsyncConnect("127.0.0.1", 4000); }); btnClose.onClick.AddListener(() => { session.Close(); }); }