diff options
author | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
commit | 766cdff5ffa72b65d7f106658d1603f47739b2ba (patch) | |
tree | 34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/NetworkData.cs |
+ init
Diffstat (limited to 'GameCode/NetworkData.cs')
-rw-r--r-- | GameCode/NetworkData.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/GameCode/NetworkData.cs b/GameCode/NetworkData.cs new file mode 100644 index 0000000..293da46 --- /dev/null +++ b/GameCode/NetworkData.cs @@ -0,0 +1,58 @@ +using Photon.Pun; +using UnityEngine; + +public class NetworkData : MonoBehaviour +{ + private PhotonView photonView; + + private bool inited; + + private void Start() + { + photonView = GetComponent<PhotonView>(); + } + + private void Init() + { + if (!inited) + { + inited = true; + if (PhotonNetwork.IsMasterClient) + { + Debug.Log("Why am i the master?"); + } + } + } + + private void Update() + { + if (PhotonNetwork.InRoom) + { + Init(); + } + } + + private void RequestJoin() + { + photonView.RPC("RequestJoinMaster", RpcTarget.MasterClient); + Debug.Log("Request join"); + } + + [PunRPC] + public void RequestJoinMaster() + { + string text = JsonUtility.ToJson(new InitPackage + { + currentMapID = MapManager.instance.currentLevelID + }); + photonView.RPC("RequestJoinResponse", RpcTarget.Others, text); + } + + [PunRPC] + public void RequestJoinResponse(string jsonResponse) + { + InitPackage initPackage = (InitPackage)JsonUtility.FromJson(jsonResponse, typeof(InitPackage)); + MapManager.instance.LoadLevelFromID(initPackage.currentMapID, onlyMaster: false, callInImidetly: true); + Debug.Log("Got response"); + } +} |