WaitForSeconds









How do I use Wait For Seconds
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: PingCloudRegions.cs Copy
118     public IEnumerator PingSocket(Region region)
119     {
120         region.Ping = Attempts*MaxMilliseconsPerPing;
121
122         this.PingsRunning++; // TODO: Add try-catch to make sure the PingsRunning are reduced at the end and that the lib does not crash the app
123         PhotonPing ping;
124         //Debug.Log("PhotonHandler.PingImplementation " + PhotonHandler.PingImplementation);
125         if (PhotonHandler.PingImplementation == typeof(PingNativeDynamic))
126         {
127             Debug.Log("Using constructor for new PingNativeDynamic()"); // it seems on android, the Activator can't find the default Constructor
128             ping = new PingNativeDynamic();
129         }
130         else
131         {
132             ping = (PhotonPing)Activator.CreateInstance(PhotonHandler.PingImplementation);
133         }
134
135         //Debug.Log("Ping is: " + ping + " type " + ping.GetType());
136
137         float rttSum = 0.0f;
138         int replyCount = 0;
139
140
141         // PhotonPing.StartPing() requires a plain IP address without port (on all but Windows 8 platforms).
142         // So: remove port and do the DNS-resolving if needed
143         string cleanIpOfRegion = region.HostAndPort;
144         int indexOfColon = cleanIpOfRegion.LastIndexOf(':');
145         if (indexOfColon > 1)
146         {
147             cleanIpOfRegion = cleanIpOfRegion.Substring(0, indexOfColon);
148         }
149         cleanIpOfRegion = ResolveHost(cleanIpOfRegion);
150         //Debug.Log("Resolved and port-less IP is: " + cleanIpOfRegion);
151
152
153         for (int i = 0; i < Attempts; i++)
154         {
155             bool overtime = false;
156             Stopwatch sw = new Stopwatch();
157             sw.Start();
158
159             try
160             {
161                 ping.StartPing(cleanIpOfRegion);
162             }
163             catch (Exception e)
164             {
165                 Debug.Log("catched: " + e);
166                 this.PingsRunning--;
167                 break;
168             }
169
170
171             while (!ping.Done())
172             {
173                 if (sw.ElapsedMilliseconds >= MaxMilliseconsPerPing)
174                 {
175                     overtime = true;
176                     break;
177                 }
178                 yield return 0; // keep this loop tight, to avoid adding local lag to rtt.
179             }
180             int rtt = (int)sw.ElapsedMilliseconds;
181
182
183             if (IgnoreInitialAttempt && i == 0)
184             {
185                 // do nothing.
186             }
187             else if (ping.Successful && !overtime)
188             {
189                 rttSum += rtt;
190                 replyCount++;
191                 region.Ping = (int)((rttSum) / replyCount);
192                 //Debug.Log("region " + region.Code + " RTT " + region.Ping + " success: " + ping.Successful + " over: " + overtime);
193             }
194
195             yield return new WaitForSeconds(0.1f);
196         }
197
198         this.PingsRunning--;
199
200         //Debug.Log("this.PingsRunning: " + this.PingsRunning + " this debug: " + ping.DebugString);
201         yield return null;
202     }
File name: Scalable.cs Copy
77  public IEnumerator IEScaleBy(float startAfter, float speed, float scaleBy) {
78   yield return new WaitForSeconds(startAfter);
79
80   float t = Time.deltaTime * speed;
81   float scale = 1f;
82   float diffScale = scaleBy - scale;
83   Vector3 origScale = gameObject.transform.localScale;
84   while(t < Mathf.PI / 2) {
85    t += Time.deltaTime * speed;
86    gameObject.transform.localScale = origScale * (scale + diffScale * Mathf.Sin(t));
87    yield return null;
88   }
89   gameObject.transform.localScale = origScale * scaleBy;
90  }
File name: Scalable.cs Copy
92  public IEnumerator IEScaleIn(float startAfter, float speed, float endScale) {
93   yield return new WaitForSeconds(startAfter);
94   ready = false;
95   float t = Time.deltaTime * speed;
96   float scale = 1f;
97   while(t < Mathf.PI / 2) {
98    t += Time.deltaTime * speed;
99    scale = endScale * Mathf.Sin(t);
100    transform.localScale = new Vector3(scale,scale,scale);
101    yield return null;
102   }
103   transform.localScale = new Vector3(endScale, endScale, endScale);
104   ready = true;
105  }
File name: Scalable.cs Copy
107  public IEnumerator IEScaleIn(float startAfter, float speed, Vector3 endScales) {
108   yield return new WaitForSeconds(startAfter);
109   ready = false;
110   float t = Time.deltaTime * speed;
111   float scale = 1f;
112   while(t < Mathf.PI / 2) {
113    t += Time.deltaTime * speed;
114    scale = 1f * Mathf.Sin(t);
115    transform.localScale = endScales * scale;
116    yield return null;
117   }
118   transform.localScale = endScales * scale;
119   ready = true;
120  }
File name: Scalable.cs Copy
133  IEnumerator IEScaleOutBase(float startAfter, float speed) {
134   yield return new WaitForSeconds(startAfter);
135
136   float t = Time.deltaTime * speed;
137   float scale = 1f;
138   Vector3 origScale = gameObject.transform.localScale;
139   t = Mathf.PI / 2f;
140   while(t < Mathf.PI) {
141    t += Time.deltaTime * speed;
142    scale = 1 * Mathf.Sin(t);
143    gameObject.transform.localScale = origScale * scale;
144    yield return null;
145   }
146  }
File name: Scaler.cs Copy
7  public IEnumerator IEScaleBy(float startAfter, GameObject go, float speed, float scaleBy) {
8   yield return new WaitForSeconds(startAfter);
9
10   float t = Time.deltaTime * speed;
11   float scale = 1f;
12   float diffScale = scaleBy - scale;
13   Vector3 origScale = go.transform.localScale;
14   while(t < Mathf.PI / 2) {
15    t += Time.deltaTime * speed;
16    go.transform.localScale = origScale * (scale + diffScale * Mathf.Sin(t));
17    yield return null;
18   }
19   go.transform.localScale = origScale * scaleBy;
20  }
File name: Scaler.cs Copy
22  public IEnumerator IEScaleIn(float startAfter, GameObject go, float speed, float endScale) {
23  yield return new WaitForSeconds(startAfter);
24   go.tag = "Busy";
25   float t = Time.deltaTime * speed;
26   float scale = 1f;
27   Vector3 origScale = go.transform.localScale;
28   while(t < Mathf.PI / 2) {
29    t += Time.deltaTime * speed;
30    scale = endScale * Mathf.Sin(t);
31    go.transform.localScale = new Vector3(scale,scale,scale);
32    yield return null;
33   }
34   go.transform.localScale = new Vector3(endScale, endScale, endScale);
35   go.tag = "Ready";
36  }
File name: Scaler.cs Copy
49  IEnumerator IEScaleOutBase(float startAfter, GameObject go, float speed) {
50   yield return new WaitForSeconds(startAfter);
51
52   float t = Time.deltaTime * speed;
53   float scale = 1f;
54   Vector3 origScale = go.transform.localScale;
55   t = Mathf.PI / 2f;
56   while(t < Mathf.PI) {
57    t += Time.deltaTime * speed;
58    scale = 1 * Mathf.Sin(t);
59    go.transform.localScale = origScale * scale;
60    yield return null;
61   }
62  }
File name: GameManagerScript.cs Copy
48  public IEnumerator RestartGameCoroutine() {
49   player.gameObject.SetActive (false);
50   yield return new WaitForSeconds (0.5f);
51   player.transform.position = playerStartPoint;
52   groundGenerator.position = groundStartPoint;
53   player.gameObject.SetActive (true);
54  }

WaitForSeconds 152 lượt xem

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