Peer
How do I use Peer
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: InstantiateCube.cs
Copy
12 void OnClick()
13 {
14 if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
15 {
16 // only use PhotonNetwork.Instantiate while in a room.
17 return;
18 }
19
20 switch (InstantiateType)
21 {
22 case 0:
23 PhotonNetwork.Instantiate(Prefab.name, this.transform.position + 3*Vector3.up, Quaternion.identity, 0);
24 break;
25 case 1:
26 PhotonNetwork.InstantiateSceneObject(Prefab.name, InputToEvent.inputHitPos + new Vector3(0, 5f, 0), Quaternion.identity, 0, null);
27 break;
28 }
29 }
File name: GUIFriendsInRoom.cs
Copy
15 public void OnGUI()
16 {
17 if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
18 {
19 return;
20 }
21
22 GUILayout.BeginArea(GuiRect);
23
24 GUILayout.Label("In-Game");
25 GUILayout.Label("For simplicity, this demo just shows the players in this room. The list will expand when more join.");
26 GUILayout.Label("Your (random) name: " + PhotonNetwork.playerName);
27 GUILayout.Label(PhotonNetwork.playerList.Length + " players in this room.");
28 GUILayout.Label("The others are:");
29 foreach (PhotonPlayer player in PhotonNetwork.otherPlayers)
30 {
31 GUILayout.Label(player.ToString());
32 }
33
34 if (GUILayout.Button("Leave"))
35 {
36 PhotonNetwork.LeaveRoom();
37 }
38 GUILayout.EndArea();
39 }
File name: WorkerMenu.cs
Copy
43 public void Awake()
44 {
45 // this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
46 PhotonNetwork.automaticallySyncScene = true;
47
48 // the following line checks if this client was just created (and not yet online). if so, we connect
49 if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated)
50 {
51 // Connect to the photon master-server. We use the settings saved in PhotonServerSettings (a .asset file in this project)
52 PhotonNetwork.ConnectUsingSettings("0.9");
53 }
54
55 // generate a name for this player, if none is assigned yet
56 if (String.IsNullOrEmpty(PhotonNetwork.playerName))
57 {
58 PhotonNetwork.playerName = "Guest" + Random.Range(1, 9999);
59 }
60
61 // if you wanted more debug out, turn this on:
62 // PhotonNetwork.logLevel = NetworkLogLevel.Full;
63 }
File name: WorkerMenu.cs
Copy
234 public void OnFailedToConnectToPhoton(object parameters)
235 {
236 this.connectFailed = true;
237 Debug.Log("OnFailedToConnectToPhoton. StatusCode: " + parameters + " ServerAddress: " + PhotonNetwork.networkingPeer.ServerAddress);
238 }
File name: RandomMatchmaker.cs
Copy
31 void OnGUI()
32 {
33 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
34
35 if (PhotonNetwork.connectionStateDetailed == PeerState.Joined)
36 {
37 bool shoutMarco = GameLogic.playerWhoIsIt == PhotonNetwork.player.ID;
38
39 if (shoutMarco && GUILayout.Button("Marco!"))
40 {
41 myPhotonView.RPC("Marco", PhotonTargets.All);
42 }
43 if (!shoutMarco && GUILayout.Button("Polo!"))
44 {
45 myPhotonView.RPC("Polo", PhotonTargets.All);
46 }
47 }
48 }
File name: PhotonConverter.cs
Copy
189 static void ConvertToPhotonAPI(string file)
190 {
191 string text = File.ReadAllText(file);
192
193 bool isJS = file.Contains(".js");
194
195 file = file.Replace("\\", "/"); // Get Class name for JS
196 string className = file.Substring(file.LastIndexOf("/")+1);
197 className = className.Substring(0, className.IndexOf("."));
198
199
200 //REGEXP STUFF
201 //Valid are: Space { } , /n /r
202 //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203 string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205 //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208 //NetworkView
209 {
210 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212 text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214 //.RPC
215 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217 }
218
219 //NetworkMessageInfo: 100%
220 {
221 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223 }
224
225 //NetworkViewID:
226 {
227 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228 }
229
230 //NetworkPlayer
231 {
232 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233 }
234
235 //Network
236 {
237 //Monobehaviour callbacks
238 {
239 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245 //Not completely the same meaning
246 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251 }
252
253 //Variables
254 {
255
256 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266 /*DROPPED:
267 minimumAllocatableViewIDs
268 natFacilitatorIP is dropped
269 natFacilitatorPort is dropped
270 connectionTesterIP
271 connectionTesterPort
272 proxyIP
273 proxyPort
274 useProxy
275 proxyPassword
276 */
277 }
278
279 //Methods
280 {
281 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285 /*DROPPED:
286 TestConnection
287 TestConnectionNAT
288 HavePublicAddress
289 */
290 }
291
292 //Overall
293 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296 //Changed methods
297 string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302 }
303
304 //General
305 {
306 if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307 {
308 if (isJS)//JS
309 {
310 if (text.Contains("extends MonoBehaviour"))
311 text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312 else
313 text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314 }
315 else //C#
316 text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317 }
318 }
319
320 File.WriteAllText(file, text);
321 }
File name: CustomTypes.cs
Copy
32 internal static void Register()
33 {
34 PhotonPeer.RegisterType(typeof(Vector2), (byte)'W', SerializeVector2, DeserializeVector2);
35 PhotonPeer.RegisterType(typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
36 PhotonPeer.RegisterType(typeof(Quaternion), (byte)'Q', SerializeQuaternion, DeserializeQuaternion);
37 PhotonPeer.RegisterType(typeof(PhotonPlayer), (byte)'P', SerializePhotonPlayer, DeserializePhotonPlayer);
38 }
File name: CustomTypes.cs
Copy
160 private static object DeserializePhotonPlayer(MemoryStream inStream, short length)
161 {
162 int ID;
163 lock (memPlayer)
164 {
165 inStream.Read(memPlayer, 0, length);
166 int off = 0;
167 Protocol.Deserialize(out ID, memPlayer, ref off);
168 }
169
170 if (PhotonNetwork.networkingPeer.mActors.ContainsKey(ID))
171 {
172 return PhotonNetwork.networkingPeer.mActors[ID];
173 }
174 else
175 {
176 return null;
177 }
178 }
File name: LoadbalancingPeer.cs
Copy
29 private readonly Dictionary31 public LoadbalancingPeer(IPhotonPeerListener listener, ConnectionProtocol protocolType) : base(listener, protocolType)
32 {
33 }
Download file with original file name:Peer
Peer 123 lượt xem
Gõ tìm kiếm nhanh...