Waiting









How do I use Waiting
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonHandler.cs Copy
232     internal IEnumerator PingAvailableRegionsCoroutine(bool connectToBest)
233     {
234         BestRegionCodeCurrently = CloudRegionCode.none;
235         while (PhotonNetwork.networkingPeer.AvailableRegions == null)
236         {
237             if (PhotonNetwork.connectionStateDetailed != PeerState.ConnectingToNameServer && PhotonNetwork.connectionStateDetailed != PeerState.ConnectedToNameServer)
238             {
239                 Debug.LogError("Call ConnectToNameServer to ping available regions.");
240                 yield break; // break if we don't connect to the nameserver at all
241             }
242
243             Debug.Log("Waiting for AvailableRegions. State: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.Server + " PhotonNetwork.networkingPeer.AvailableRegions " + (PhotonNetwork.networkingPeer.AvailableRegions != null));
244             yield return new WaitForSeconds(0.25f); // wait until pinging finished (offline mode won't ping)
245         }
246
247         if (PhotonNetwork.networkingPeer.AvailableRegions == null || PhotonNetwork.networkingPeer.AvailableRegions.Count == 0)
248         {
249             Debug.LogError("No regions available. Are you sure your appid is valid and setup?");
250             yield break; // break if we don't get regions at all
251         }
252
253         //#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IPHONE)
254         //#pragma warning disable 0162 // the library variant defines if we should use PUN's SocketUdp variant (at all)
255         //if (PhotonPeer.NoSocket)
256         //{
257         // if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
258         // {
259         // Debug.Log("PUN disconnects to re-use native sockets for pining servers and to find the best.");
260         // }
261         // PhotonNetwork.Disconnect();
262         //}
263         //#pragma warning restore 0162
264         //#endif
265
266         PhotonPingManager pingManager = new PhotonPingManager();
267         foreach (Region region in PhotonNetwork.networkingPeer.AvailableRegions)
268         {
269             SP.StartCoroutine(pingManager.PingSocket(region));
270         }
271
272         while (!pingManager.Done)
273         {
274             yield return new WaitForSeconds(0.1f); // wait until pinging finished (offline mode won't ping)
275         }
276
277
278         Region best = pingManager.BestRegion;
279         PhotonHandler.BestRegionCodeCurrently = best.Code;
280         PhotonHandler.BestRegionCodeInPreferences = best.Code;
281
282         Debug.Log("Found best region: " + best.Code + " ping: " + best.Ping + ". Calling ConnectToRegionMaster() is: " + connectToBest);
283
284
285         if (connectToBest)
286         {
287             PhotonNetwork.networkingPeer.ConnectToRegionMaster(best.Code);
288         }
289     }
File name: PickupItemSyncer.cs Copy
24     public void OnJoinedRoom()
25     {
26         Debug.Log("Joined Room. isMasterClient: " + PhotonNetwork.isMasterClient + " id: " + PhotonNetwork.player.ID);
27         // this client joined the room. let's see if there are players and if someone has to inform us about pickups
28         this.IsWaitingForPickupInit = !PhotonNetwork.isMasterClient;
29
30         if (PhotonNetwork.playerList.Length >= 2)
31         {
32             this.Invoke("AskForPickupItemSpawnTimes", 2.0f);
33         }
34     }
File name: PickupItemSyncer.cs Copy
37     public void AskForPickupItemSpawnTimes()
38     {
39         if (this.IsWaitingForPickupInit)
40         {
41             if (PhotonNetwork.playerList.Length < 2)
42             {
43                 Debug.Log("Cant ask anyone else for PickupItem spawn times.");
44                 this.IsWaitingForPickupInit = false;
45                 return;
46             }
47
48
49             // find a another player (than the master, who likely is gone) to ask for the PickupItem spawn times
50             PhotonPlayer nextPlayer = PhotonNetwork.masterClient.GetNext();
51             if (nextPlayer == null || nextPlayer.Equals(PhotonNetwork.player))
52             {
53                 nextPlayer = PhotonNetwork.player.GetNext();
54                 //Debug.Log("This player is the Master's next. Asking this client's 'next' player: " + ((nextPlayer != null) ? nextPlayer.ToStringFull() : ""));
55             }
56
57             if (nextPlayer != null && !nextPlayer.Equals(PhotonNetwork.player))
58             {
59                 this.photonView.RPC("RequestForPickupTimes", nextPlayer);
60
61                 // you could restart this invoke and try to find another player after 4 seconds. but after a while it doesnt make a difference anymore
62                 //this.Invoke("AskForPickupItemSpawnTimes", 2.0f);
63             }
64             else
65             {
66                 Debug.Log("No player left to ask");
67                 this.IsWaitingForPickupInit = false;
68             }
69         }
70     }
File name: PickupItemSyncer.cs Copy
130     public void PickupItemInit(double timeBase, float[] inactivePickupsAndTimes)
131     {
132         this.IsWaitingForPickupInit = false;
133
134         // if there are no inactive pickups, the sender will send a list of 0 items. this is not a problem...
135         for (int i = 0; i < inactivePickupsAndTimes.Length / 2; i++)
136         {
137             int arrayIndex = i*2;
138             int viewIdOfPickup = (int)inactivePickupsAndTimes[arrayIndex];
139             float timeUntilRespawnBasedOnTimeBase = inactivePickupsAndTimes[arrayIndex + 1];
140
141
142             PhotonView view = PhotonView.Find(viewIdOfPickup);
143             PickupItem pi = view.GetComponent();
144
145             if (timeUntilRespawnBasedOnTimeBase <= 0)
146             {
147                 pi.PickedUp(0.0f);
148             }
149             else
150             {
151                 double timeOfRespawn = timeUntilRespawnBasedOnTimeBase + timeBase;
152
153                 Debug.Log(view.viewID + " respawn: " + timeOfRespawn + " timeUntilRespawnBasedOnTimeBase:" + timeUntilRespawnBasedOnTimeBase + " SecondsBeforeRespawn: " + pi.SecondsBeforeRespawn);
154                 double timeBeforeRespawn = timeOfRespawn - PhotonNetwork.time;
155                 if (timeUntilRespawnBasedOnTimeBase <= 0)
156                 {
157                     timeBeforeRespawn = 0.0f;
158                 }
159
160                 pi.PickedUp((float) timeBeforeRespawn);
161             }
162         }
163     }
File name: InputManager.cs Copy
39  void Update() {
40   mouseAxis.x = Input.GetAxis("Mouse X");
41   mouseAxis.y = Input.GetAxis("Mouse Y");
42
43   if (InputEvent == null) return;
44
45   if (!GameManager.Instance.IsReady) return;
46
47   HighlightTile();
48
49   if (Input.GetMouseButtonUp(0)) {
50    if (GameManager.Instance.GameState.IsWaiting) {
51     UnHighlightTile();
52     InputEvent(InputActionType.GRAB_PIECE);
53    } else if (GameManager.Instance.GameState.IsHolding) {
54     InputEvent(InputActionType.PLACE_PIECE);
55    }
56   }
57
58   if (Input.GetMouseButtonUp(1)) {
59    if (GameManager.Instance.GameState.IsHolding) {
60     InputEvent(InputActionType.CANCEL_PIECE);
61    }
62   }
63
64   if (Input.GetAxis("Mouse ScrollWheel") > 0) {
65    InputEvent(InputActionType.ZOOM_IN);
66   }
67
68   if (Input.GetAxis("Mouse ScrollWheel") < 0) {
69    InputEvent(InputActionType.ZOOM_OUT);
70   }
71
72   if (Input.GetMouseButtonDown(2)) {
73    InputEvent(InputActionType.ROTATE);
74   } else if (Input.GetMouseButtonUp(2)) {
75    InputEvent(InputActionType.STOP_ROTATE);
76   }
77  }
File name: InputManager.cs Copy
79  public void HighlightTile() {
80   if (GameManager.Instance.GameState.IsWaiting) {
81    UnHighlightTile();
82    currentNode = Finder.RayHitFromScreen(Input.mousePosition);
83    if (currentNode != null) {
84     Piece piece = currentNode.Piece;
85     if (piece != null) {
86      if (GameManager.Instance.CurrentPlayer.Has(piece)) {
87       currentNode.HighlightMove();
88      } else {
89       currentNode.HighlightEat();
90      }
91     }
92    }
93   }
94  }
File name: GameState.cs Copy
24  public GameState() {
25   state = GameStateType.WAITING;
26  }
File name: GameState.cs Copy
35  public bool IsWaiting {
36   get {return state == GameStateType.WAITING;}
37  }
File name: GameState.cs Copy
55  public void Release() {
56   state = GameStateType.WAITING;
57   GameManager.Instance.SwitchPlayer();
58  }
File name: GameState.cs Copy
60  public void Cancel() {
61   state = GameStateType.WAITING;
62  }

Download file with original file name:Waiting

Waiting 116 lượt xem

Gõ tìm kiếm nhanh...