Photon
How do I use Photon
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Demo2DJumpAndRun.cs
Copy
6 void OnJoinedRoom()
7 {
8 if( PhotonNetwork.isMasterClient == false )
9 {
10 return;
11 }
12
13 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
14 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
15 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
16
17 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
18 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
19 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
20 }
File name: JumpAndRunMovement.cs
Copy
15 void Awake()
16 {
17 m_Animator = GetComponent
18 m_Body = GetComponent
19 m_PhotonView = GetComponent
20 }
File name: JumpAndRunMovement.cs
Copy
29 void FixedUpdate()
30 {
31 if( m_PhotonView.isMine == false )
32 {
33 return;
34 }
35
36 UpdateMovement();
37 UpdateJumping();
38 }
File name: JumpAndRunMovement.cs
Copy
52 void UpdateJumping()
53 {
54 if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55 {
56 m_Animator.SetTrigger( "IsJumping" );
57 m_Body.AddForce( Vector2.up * JumpForce );
58 m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59 }
60 }
File name: DemoBoxesGui.cs
Copy
8 void OnGUI()
9 {
10 if (HideUI)
11 {
12 return;
13 }
14
15 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
16
17 if (!PhotonNetwork.connected)
18 {
19 if (GUILayout.Button("Connect"))
20 {
21 PhotonNetwork.ConnectUsingSettings(null);
22 }
23 }
24 else
25 {
26 if (GUILayout.Button("Disconnect"))
27 {
28 PhotonNetwork.Disconnect();
29 }
30 }
31
32 }
File name: OnAwakePhysicsSettings.cs
Copy
10 public void Awake()
11 {
12 if (!photonView.isMine)
13 {
14 Rigidbody attachedRigidbody = GetComponent
15 if (attachedRigidbody != null)
16 {
17 attachedRigidbody.isKinematic = true;
18 }
19 else
20 {
21 Rigidbody2D attachedRigidbody2d = GetComponent
22 if (attachedRigidbody2d != null)
23 {
24 attachedRigidbody2d.isKinematic = true;
25 }
26 }
27 }
28 }
File name: ClickAndDrag.cs
Copy
11 void Update ()
12 {
13 if (!photonView.isMine)
14 {
15 return;
16 }
17
18 InputToEvent input = Camera.main.GetComponent
19 if (input == null) return;
20 if (!following)
21 {
22 if (input.Dragging)
23 {
24 camOnPress = this.transform.position;
25 following = true;
26 }
27 else
28 {
29 return;
30 }
31 }
32 else
33 {
34 if (input.Dragging)
35 {
36 Vector3 target = camOnPress - (new Vector3(input.DragVector.x, 0, input.DragVector.y) * factor);
37 this.transform.position = Vector3.Lerp(this.transform.position, target, Time.deltaTime*.5f);
38 }
39 else
40 {
41 camOnPress = Vector3.zero;
42 following = false;
43 }
44 }
45 }
File name: DemoOwnershipGui.cs
Copy
8 public void OnOwnershipRequest(object[] viewAndPlayer)
9 {
10 PhotonView view = viewAndPlayer[0] as PhotonView;
11 PhotonPlayer requestingPlayer = viewAndPlayer[1] as PhotonPlayer;
12
13 Debug.Log("OnOwnershipRequest(): Player " + requestingPlayer + " requests ownership of: " + view + ".");
14 if (this.TransferOwnershipOnRequest)
15 {
16 view.TransferOwnership(requestingPlayer.ID);
17 }
18 }
File name: DemoOwnershipGui.cs
Copy
23 public void OnGUI()
24 {
25 GUI.skin = this.Skin;
26 GUILayout.BeginArea(new Rect(Screen.width - 200, 0, 200, Screen.height));
27 {
28 string label = TransferOwnershipOnRequest ? "passing objects" : "rejecting to pass";
29 if (GUILayout.Button(label))
30 {
31 this.TransferOwnershipOnRequest = !this.TransferOwnershipOnRequest;
32 }
33 }
34 GUILayout.EndArea();
35
36
37
38 if (PhotonNetwork.inRoom)
39 {
40 int playerNr = PhotonNetwork.player.ID;
41 string playerIsMaster = PhotonNetwork.player.isMasterClient ? "(master) " : "";
42 string playerColor = PlayerVariables.GetColorName(PhotonNetwork.player.ID);
43 GUILayout.Label(string.Format("player {0}, {1} {2}(you)", playerNr, playerColor, playerIsMaster));
44
45 foreach (PhotonPlayer otherPlayer in PhotonNetwork.otherPlayers)
46 {
47 playerNr = otherPlayer.ID;
48 playerIsMaster = otherPlayer.isMasterClient ? "(master)" : "";
49 playerColor = PlayerVariables.GetColorName(otherPlayer.ID);
50 GUILayout.Label(string.Format("player {0}, {1} {2}", playerNr, playerColor, playerIsMaster));
51 }
52
53 if (PhotonNetwork.inRoom && PhotonNetwork.otherPlayers.Length == 0)
54 {
55 GUILayout.Label("Join more clients to switch object-control.");
56 }
57 }
58 else
59 {
60 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
61 }
62 }
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 }
Download file with original file name:Photon
Photon 126 lượt xem
Gõ tìm kiếm nhanh...