Last active
January 1, 2019 06:19
-
-
Save HassakuTb/f4788ec7be63938d024dafbb660afb59 to your computer and use it in GitHub Desktop.
Sync timing all players in room without PhotonView. (PUN2 with Unity)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zLib/libpng License | |
Copyright (c) 2019 Hassaku | |
This software is provided 'as-is', without any express or implied | |
warranty. In no event will the authors be held liable for any damages | |
arising from the use of this software. | |
Permission is granted to anyone to use this software for any purpose, | |
including commercial applications, and to alter it and redistribute it | |
freely, subject to the following restrictions: | |
1. The origin of this software must not be misrepresented; you must not | |
claim that you wrote the original software. If you use this software | |
in a product, an acknowledgment in the product documentation would be | |
appreciated but is not required. | |
2. Altered source versions must be plainly marked as such, and must not be | |
misrepresented as being the original software. | |
3. This notice may not be removed or altered from any source | |
distribution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ExitGames.Client.Photon; | |
using Photon.Pun; | |
using Photon.Realtime; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/// <summary> | |
/// PUN2 ルーム内タイミング合わせ | |
/// </summary> | |
/// <usage> | |
/// コールバックの登録は入室時、解除は退室時 | |
/// public void OnCreateRoom(){ | |
/// sync = new TimingSync(); | |
/// PhotonNetwork.AddCallbackTarget(sync); | |
/// ... | |
/// } | |
/// | |
/// public void OnLeftRoom(){ | |
/// PhotonNetwork.RemoveCallbackTarget(sync); | |
/// } | |
/// | |
/// // タイミング合わせ | |
/// yield return sync.WaitForSync("Hoge", 30f); | |
/// </usage> | |
public class TimingSync : IOnEventCallback { | |
// 環境によって変える | |
private const byte SyncRequestCode = 102; | |
private Dictionary<string, int> counters = new Dictionary<string, int>(); | |
public bool IsTimeout { get; set; } | |
public bool IsDisconnected { get; set; } | |
/// <summary> | |
/// ルーム内プレイヤーのタイミング合わせを待つ | |
/// </summary> | |
/// <param name="identifier">タイミング識別子</param> | |
/// <param name="timeout">タイムアウト時間</param> | |
/// <returns></returns> | |
public IEnumerator WaitForSync(string identifier, float timeout) | |
{ | |
if (!counters.ContainsKey(identifier)) counters.Add(identifier, 0); | |
if (!(PhotonNetwork.IsConnectedAndReady && PhotonNetwork.InRoom)) | |
{ | |
IsDisconnected = true; | |
yield break; | |
} | |
PhotonNetwork.RaiseEvent(SyncRequestCode, identifier, | |
new RaiseEventOptions | |
{ | |
Receivers = ReceiverGroup.All, | |
}, | |
new SendOptions | |
{ | |
DeliveryMode = DeliveryMode.Reliable, | |
Reliability = true, | |
}); | |
float timer = 0f; | |
while (true) | |
{ | |
if (timer > timeout) | |
{ | |
IsTimeout = true; | |
break; | |
} | |
if (!(PhotonNetwork.IsConnectedAndReady && PhotonNetwork.InRoom)) | |
{ | |
IsDisconnected = true; | |
break; | |
} | |
if(counters[identifier] == PhotonNetwork.CurrentRoom.PlayerCount) | |
{ | |
break; | |
} | |
timer += Time.deltaTime; | |
yield return null; | |
} | |
counters[identifier] = 0; | |
} | |
public void OnEvent(EventData photonEvent) | |
{ | |
if(photonEvent.Code == SyncRequestCode) | |
{ | |
string key = (string)photonEvent.CustomData; | |
if (!counters.ContainsKey(key)) counters.Add(key, 0); | |
counters[key]++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment