PhotonView
How do I use Photon View
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: 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: MaterialPerOwner.cs
Copy
18 private void Update()
19 {
20 if( this.photonView.ownerId != assignedColorForUserId )
21 {
22 m_Renderer.material = PlayerVariables.GetMaterial( m_Renderer.material, this.photonView.ownerId );
23 this.assignedColorForUserId = this.photonView.ownerId;
24 //Debug.Log("Switched Material to: " + this.assignedColorForUserId + " " + this.renderer.material.GetInstanceID());
25 }
26 }
File name: OnClickRequestOwnership.cs
Copy
8 public void OnClick()
9 {
10 if( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
11 {
12 Vector3 colVector = new Vector3( Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ) );
13 this.photonView.RPC( "ColorRpc", PhotonTargets.AllBufferedViaServer, colVector );
14 }
15 else
16 {
17 if( this.photonView.ownerId == PhotonNetwork.player.ID )
18 {
19 Debug.Log( "Not requesting ownership. Already mine." );
20 return;
21 }
22
23 this.photonView.RequestOwnership();
24 }
25 }
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
File name: DemoMecanimGUI.cs
Copy
42 public void FindRemoteAnimator()
43 {
44 if( m_RemoteAnimator != null )
45 {
46 return;
47 }
48
49 // the prefab has to be tagged as Player
50 GameObject[] gos = GameObject.FindGameObjectsWithTag( "Player" );
51 for( int i = 0; i < gos.Length; ++i )
52 {
53 PhotonView view = gos[ i ].GetComponent
54 if( view != null && view.isMine == false )
55 {
56 m_RemoteAnimator = gos[ i ].GetComponent
57 }
58 }
59 }
Download file with original file name:PhotonView
PhotonView 189 lượt xem
Gõ tìm kiếm nhanh...