Option









How do I use Option
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: GameManager.cs Copy
27  void OnGUI() {
28   GUI.skin = layout;
29   GUI.Label (new Rect (Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1);
30   GUI.Label (new Rect (Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2);
31
32   if (GUI.Button (new Rect (Screen.width / 2 - 60, 35, 120, 53), "RESTART")) {
33    PlayerScore1 = 0;
34    PlayerScore2 = 0;
35    theBall.SendMessage ("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
36   }
37
38   if (PlayerScore1 == 10) {
39    GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
40    theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
41   } else if (PlayerScore2 == 10) {
42    GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
43    theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
44   }
45  }
File name: SideWalls.cs Copy
7  void OnTriggerEnter2D(Collider2D hitInfo) {
8   if (hitInfo.name == "Ball")
9   {
10    string wallName = transform.name;
11    GameManager.Score (wallName);
12    hitInfo.gameObject.SendMessage ("RestartGame", 1, SendMessageOptions.RequireReceiver);
13   }
14  }
File name: PunStartup.cs Copy
80     public static void SetPunDemoBuildSettings()
81     {
82         // find path of pun guide
83         string[] tempPaths = Directory.GetDirectories(Application.dataPath + "/Photon Unity Networking", "Demos", SearchOption.AllDirectories);
84         if (tempPaths == null || tempPaths.Length != 1)
85         {
86             return;
87         }
88
89         // find scenes of guide
90         string guidePath = tempPaths[0];
91         tempPaths = Directory.GetFiles(guidePath, "*.unity", SearchOption.AllDirectories);
92
93         if (tempPaths == null || tempPaths.Length == 0)
94         {
95             return;
96         }
97
98         // add found guide scenes to build settings
99         List sceneAr = new List();
100         for (int i = 0; i < tempPaths.Length; i++)
101         {
102             //Debug.Log(tempPaths[i]);
103             string path = tempPaths[i].Substring(Application.dataPath.Length - "Assets".Length);
104             path = path.Replace('\\', '/');
105             //Debug.Log(path);
106
107             if (path.Contains("PUNGuide_M2H"))
108             {
109                 continue;
110             }
111
112             if (path.Contains("Hub"))
113             {
114                 sceneAr.Insert(0, new EditorBuildSettingsScene(path, true));
115                 continue;
116             }
117
118             sceneAr.Add(new EditorBuildSettingsScene(path, true));
119         }
120
121         EditorBuildSettings.scenes = sceneAr.ToArray();
122         EditorApplication.OpenScene(sceneAr[0].path);
123     }
File name: PickupController.cs Copy
73     // Are we jumping? (Initiated with jump button and not grounded yet)
77     // Are we moving backwards (This locks the camera to not do a 180 degree spin)
81     // When did the user start walking (Used for going into trot after a while)
87     // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
101     void Awake()
102     {
103         // PUN: automatically determine isControllable, if this GO has a PhotonView
104         PhotonView pv = this.gameObject.GetComponent();
105         if (pv != null)
106         {
107             isControllable = pv.isMine;
108
109             // The pickup demo assigns this GameObject as the PhotonPlayer.TagObject. This way, we can access this character (controller, position, etc) easily
110             if (this.AssignAsTagObject)
111             {
112                 pv.owner.TagObject = this.gameObject;
113             }
114
115             // please note: we change this setting on ANY PickupController if "DoRotate" is off. not only locally when it's "our" GameObject!
116             if (pv.observed is Transform && !DoRotate)
117             {
118                 pv.onSerializeTransformOption = OnSerializeTransform.OnlyPosition;
119             }
120         }
121
122
123         moveDirection = transform.TransformDirection(Vector3.forward);
124
125         _animation = GetComponent();
126         if (!_animation)
127             Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
128
129         if (!idleAnimation)
130         {
131             _animation = null;
132             Debug.Log("No idle animation found. Turning off animations.");
133         }
134         if (!walkAnimation)
135         {
136             _animation = null;
137             Debug.Log("No walk animation found. Turning off animations.");
138         }
139         if (!runAnimation)
140         {
141             _animation = null;
142             Debug.Log("No run animation found. Turning off animations.");
143         }
144         if (!jumpPoseAnimation && canJump)
145         {
146             _animation = null;
147             Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
148         }
149     }
File name: PickupController.cs Copy
151     void Update()
152     {
153         if (isControllable)
154         {
155             if (Input.GetButtonDown("Jump"))
156             {
157                 lastJumpButtonTime = Time.time;
158             }
159
160             UpdateSmoothedMovementDirection();
161
162             // Apply gravity
163             // - extra power jump modifies gravity
164             // - controlledDescent mode modifies gravity
165             ApplyGravity();
166
167             // Apply jumping logic
168             ApplyJumping();
169
170
171             // Calculate actual motion
172             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
173             movement *= Time.deltaTime;
174
175             //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));
176
177             // Move the controller
178             CharacterController controller = GetComponent();
179             collisionFlags = controller.Move(movement);
180
181         }
182
183         // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
184         if (this.remotePosition != Vector3.zero)
185         {
186             transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
187         }
188
189         velocity = (transform.position - lastPos)*25;
190
191         // ANIMATION sector
192         if (_animation)
193         {
194             if (_characterState == PickupCharacterState.Jumping)
195             {
196                 if (!jumpingReachedApex)
197                 {
198                     _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
199                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
200                     _animation.CrossFade(jumpPoseAnimation.name);
201                 }
202                 else
203                 {
204                     _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
205                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
206                     _animation.CrossFade(jumpPoseAnimation.name);
207                 }
208             }
209             else
210             {
211                 if (_characterState == PickupCharacterState.Idle)
212                 {
213                     _animation.CrossFade(idleAnimation.name);
214                 }
215                 else if (_characterState == PickupCharacterState.Running)
216                 {
217                     _animation[runAnimation.name].speed = runMaxAnimationSpeed;
218                     if (this.isControllable)
219                     {
220                         _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
221                     }
222                     _animation.CrossFade(runAnimation.name);
223                 }
224                 else if (_characterState == PickupCharacterState.Trotting)
225                 {
226                     _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
227                     if (this.isControllable)
228                     {
229                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
230                     }
231                     _animation.CrossFade(walkAnimation.name);
232                 }
233                 else if (_characterState == PickupCharacterState.Walking)
234                 {
235                     _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
236                     if (this.isControllable)
237                     {
238                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
239                     }
240                     _animation.CrossFade(walkAnimation.name);
241                 }
242
243                 if (_characterState != PickupCharacterState.Running)
244                 {
245                     _animation[runAnimation.name].time = 0.0f;
246                 }
247             }
248         }
249         // ANIMATION sector
250
251         // Set rotation to the move direction
252         if (IsGrounded())
253         {
254             // a specialty of this controller: you can disable rotation!
255             if (DoRotate)
256             {
257                 transform.rotation = Quaternion.LookRotation(moveDirection);
258             }
259         }
260         else
261         {
262             /* This causes choppy behaviour when colliding with SIDES
263              * Vector3 xzMove = velocity;
264             xzMove.y = 0;
265             if (xzMove.sqrMagnitude > 0.001f)
266             {
267                 transform.rotation = Quaternion.LookRotation(xzMove);
268             }*/
269         }
270
271         // We are in jump mode but just became grounded
272         if (IsGrounded())
273         {
274             lastGroundedTime = Time.time;
275             inAirVelocity = Vector3.zero;
276             if (jumping)
277             {
278                 jumping = false;
279                 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
280             }
281         }
282
283         lastPos = transform.position;
284     }
File name: IELdemo.cs Copy
27     // This is one of the callback/event methods called by PUN (read more in PhotonNetworkingMessage enumeration)
28     public void OnPhotonRandomJoinFailed()
29     {
30         PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);
31     }
File name: ThirdPersonController.cs Copy
292     void Update()
293     {
294         if (isControllable)
295         {
296             if (Input.GetButtonDown("Jump"))
297             {
298                 lastJumpButtonTime = Time.time;
299             }
300
301             UpdateSmoothedMovementDirection();
302
303             // Apply gravity
304             // - extra power jump modifies gravity
305             // - controlledDescent mode modifies gravity
306             ApplyGravity();
307
308             // Apply jumping logic
309             ApplyJumping();
310
311
312             // Calculate actual motion
313             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
314             movement *= Time.deltaTime;
315
316             // Move the controller
317             CharacterController controller = GetComponent();
318             collisionFlags = controller.Move(movement);
319         }
320         velocity = (transform.position - lastPos)*25;
321
322         // ANIMATION sector
323         if (_animation)
324         {
325             if (_characterState == CharacterState.Jumping)
326             {
327                 if (!jumpingReachedApex)
328                 {
329                     _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
330                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
331                     _animation.CrossFade(jumpPoseAnimation.name);
332                 }
333                 else
334                 {
335                     _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
336                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
337                     _animation.CrossFade(jumpPoseAnimation.name);
338                 }
339             }
340             else
341             {
342                 if (this.isControllable && velocity.sqrMagnitude < 0.001f)
343                 {
344                     _characterState = CharacterState.Idle;
345                     _animation.CrossFade(idleAnimation.name);
346                 }
347                 else
348                 {
349                     if (_characterState == CharacterState.Idle)
350                     {
351                         _animation.CrossFade(idleAnimation.name);
352                     }
353                     else if (_characterState == CharacterState.Running)
354                     {
355                         _animation[runAnimation.name].speed = runMaxAnimationSpeed;
356                         if (this.isControllable)
357                         {
358                             _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
359                         }
360                         _animation.CrossFade(runAnimation.name);
361                     }
362                     else if (_characterState == CharacterState.Trotting)
363                     {
364                         _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
365                         if (this.isControllable)
366                         {
367                             _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
368                         }
369                         _animation.CrossFade(walkAnimation.name);
370                     }
371                     else if (_characterState == CharacterState.Walking)
372                     {
373                         _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
374                         if (this.isControllable)
375                         {
376                             _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
377                         }
378                         _animation.CrossFade(walkAnimation.name);
379                     }
380
381                 }
382             }
383         }
384         // ANIMATION sector
385
386         // Set rotation to the move direction
387         if (IsGrounded())
388         {
389
390             transform.rotation = Quaternion.LookRotation(moveDirection);
391
392         }
393         else
394         {
395             /* This causes choppy behaviour when colliding with SIDES
396              * Vector3 xzMove = velocity;
397             xzMove.y = 0;
398             if (xzMove.sqrMagnitude > 0.001f)
399             {
400                 transform.rotation = Quaternion.LookRotation(xzMove);
401             }*/
402         }
403
404         // We are in jump mode but just became grounded
405         if (IsGrounded())
406         {
407             lastGroundedTime = Time.time;
408             inAirVelocity = Vector3.zero;
409             if (jumping)
410             {
411                 jumping = false;
412                 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
413             }
414         }
415
416         lastPos = transform.position;
417     }
File name: WorkerMenu.cs Copy
65     public void OnGUI()
66     {
67         if (this.Skin != null)
68         {
69             GUI.skin = this.Skin;
70         }
71
72         if (!PhotonNetwork.connected)
73         {
74             if (PhotonNetwork.connecting)
75             {
76                 GUILayout.Label("Connecting to: " + PhotonNetwork.ServerAddress);
77             }
78             else
79             {
80                 GUILayout.Label("Not connected. Check console output. Detailed connection state: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.ServerAddress);
81             }
82
83             if (this.connectFailed)
84             {
85                 GUILayout.Label("Connection failed. Check setup and use Setup Wizard to fix configuration.");
86                 GUILayout.Label(String.Format("Server: {0}", new object[] {PhotonNetwork.ServerAddress}));
87                 GUILayout.Label("AppId: " + PhotonNetwork.PhotonServerSettings.AppID);
88
89                 if (GUILayout.Button("Try Again", GUILayout.Width(100)))
90                 {
91                     this.connectFailed = false;
92                     PhotonNetwork.ConnectUsingSettings("0.9");
93                 }
94             }
95
96             return;
97         }
98
99         Rect content = new Rect((Screen.width - WidthAndHeight.x)/2, (Screen.height - WidthAndHeight.y)/2, WidthAndHeight.x, WidthAndHeight.y);
100         GUI.Box(content,"Join or Create Room");
101         GUILayout.BeginArea(content);
102
103         GUILayout.Space(40);
104
105         // Player name
106         GUILayout.BeginHorizontal();
107         GUILayout.Label("Player name:", GUILayout.Width(150));
108         PhotonNetwork.playerName = GUILayout.TextField(PhotonNetwork.playerName);
109         GUILayout.Space(158);
110         if (GUI.changed)
111         {
112             // Save name
113             PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
114         }
115         GUILayout.EndHorizontal();
116
117         GUILayout.Space(15);
118
119         // Join room by title
120         GUILayout.BeginHorizontal();
121         GUILayout.Label("Roomname:", GUILayout.Width(150));
122         this.roomName = GUILayout.TextField(this.roomName);
123
124         if (GUILayout.Button("Create Room", GUILayout.Width(150)))
125         {
126             PhotonNetwork.CreateRoom(this.roomName, new RoomOptions() { maxPlayers = 10 }, null);
127         }
128
129         GUILayout.EndHorizontal();
130
131         // Create a room (fails if exist!)
132         GUILayout.BeginHorizontal();
133         GUILayout.FlexibleSpace();
134         //this.roomName = GUILayout.TextField(this.roomName);
135         if (GUILayout.Button("Join Room", GUILayout.Width(150)))
136         {
137             PhotonNetwork.JoinRoom(this.roomName);
138         }
139
140         GUILayout.EndHorizontal();
141
142
143         if (!string.IsNullOrEmpty(this.ErrorDialog))
144         {
145             GUILayout.Label(this.ErrorDialog);
146
147             if (timeToClearDialog < Time.time)
148             {
149                 timeToClearDialog = 0;
150                 this.ErrorDialog = "";
151             }
152         }
153
154         GUILayout.Space(15);
155
156         // Join random room
157         GUILayout.BeginHorizontal();
158
159         GUILayout.Label(PhotonNetwork.countOfPlayers + " users are online in " + PhotonNetwork.countOfRooms + " rooms.");
160         GUILayout.FlexibleSpace();
161         if (GUILayout.Button("Join Random", GUILayout.Width(150)))
162         {
163             PhotonNetwork.JoinRandomRoom();
164         }
165
166
167         GUILayout.EndHorizontal();
168
169         GUILayout.Space(15);
170         if (PhotonNetwork.GetRoomList().Length == 0)
171         {
172             GUILayout.Label("Currently no games are available.");
173             GUILayout.Label("Rooms will be listed here, when they become available.");
174         }
175         else
176         {
177             GUILayout.Label(PhotonNetwork.GetRoomList().Length + " rooms available:");
178
179             // Room listing: simply call GetRoomList: no need to fetch/poll whatever!
180             this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
181             foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList())
182             {
183                 GUILayout.BeginHorizontal();
184                 GUILayout.Label(roomInfo.name + " " + roomInfo.playerCount + "/" + roomInfo.maxPlayers);
185                 if (GUILayout.Button("Join", GUILayout.Width(150)))
186                 {
187                     PhotonNetwork.JoinRoom(roomInfo.name);
188                 }
189
190                 GUILayout.EndHorizontal();
191             }
192
193             GUILayout.EndScrollView();
194         }
195
196         GUILayout.EndArea();
197     }
File name: WorkerMenu.cs Copy
217     public void OnPhotonRandomJoinFailed()
218     {
219         this.ErrorDialog = "Error: Can't join random room (none found).";
220         Debug.Log("OnPhotonRandomJoinFailed got called. Happens if no room is available (or all full or invisible or closed). JoinrRandom filter-options can limit available rooms.");
221     }
File name: PhotonRigidbody2DViewEditor.cs Copy
7     public override void OnInspectorGUI()
8     {
9         PhotonGUI.ContainerHeader("Options");
10
11         Rect containerRect = PhotonGUI.ContainerBody(EditorGUIUtility.singleLineHeight*2 + 10);
12
13         Rect propertyRect = new Rect(containerRect.xMin + 5, containerRect.yMin + 5, containerRect.width, EditorGUIUtility.singleLineHeight);
14         EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeVelocity"), new GUIContent("Synchronize Velocity"));
15
16         propertyRect.y += EditorGUIUtility.singleLineHeight;
17         EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeAngularVelocity"), new GUIContent("Synchronize Angular Velocity"));
18     }

Download file with original file name:Option

Option 124 lượt xem

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