Level









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

Featured Snippets


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 = "Demo Boxes\n\nUses ConnectAndJoinRandom script.\n(joins a random room or creates one)\n\nInstantiates simple prefab.\nSynchronizes positions without smoothing.";
41             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42         }
43         if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44         {
45             demoDescription = "Demo Worker\n\nJoins the default lobby and shows existing rooms.\nLets you create or join a room.\nInstantiates an animated character.\nSynchronizes position and animation state of character with smoothing.\nImplements simple in-room Chat via RPC calls.";
46             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47         }
48         if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49         {
50             demoDescription = "Movement Smoothing\n\nUses ConnectAndJoinRandom script.\nShows several basic ways to update positions of remote objects.";
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 = "Ownership Transfer\n\nShows how to transfer the ownership of a PhotonView.\nThe owner will send position updates of the GameObject.\nTransfer can be edited per PhotonView and set to Fixed (no transfer), Request (owner has to agree) or Takeover (owner can't object).";
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 = "Pickup, Teams, Scores\n\nUses ConnectAndJoinRandom script.\nImplements item pickup with RPCs.\nUses Custom Properties for Teams.\nCounts score per player and team.\nUses PhotonPlayer extension methods for easy Custom Property access.";
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 = "Chat\n\nUses the Chat API (now part of PUN).\nSimple UI.\nYou can enter any User ID.\nAutomatically subscribes some channels.\nAllows simple commands via text.\n\nRequires configuration of Chat App ID in scene.";
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 = "RPG Movement\n\nDemonstrates how to use the PhotonTransformView component to synchronize position updates smoothly using inter- and extrapolation.\n\nThis demo also shows how to setup a Mecanim Animator to update animations automatically based on received position updates (without sending explicit animation updates).";
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 = "Mecanim Animations\n\nThis demo shows how to use the PhotonAnimatorView component to easily synchronize Mecanim animations.\n\nIt also demonstrates another feature of the PhotonTransformView component which gives you more control how position updates are inter-/extrapolated by telling the component how fast the object moves and turns using SetSynchronizedValues().";
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 = "2D Game Demo\n\nSynchronizes animations, positions and physics in a 2D scene.";
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 = "Friends & Authentication\n\nShows connect with or without (server-side) authentication.\n\nAuthentication requires minor server-side setup (in Dashboard).\n\nOnce connected, you can find (made up) friends.\nJoin a room just to see how that gets visible in friends list.";
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 = "Marco Polo Tutorial\n\nFinal result you could get when you do the Marco Polo Tutorial.\nSlightly modified to be more compatible with this package.";
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: ToHubButton.cs Copy
21     public void OnGUI()
22     {
23         if (Application.loadedLevel != 0)
24         {
25             int w = ButtonTexture.width + 4;
26             int h = ButtonTexture.height + 4;
27
28             ButtonRect = new Rect(Screen.width - w, Screen.height - h, w, h);
29             if (GUI.Button(ButtonRect, ButtonTexture, GUIStyle.none))
30             {
31                 PhotonNetwork.Disconnect();
32                 Application.LoadLevel(0);
33             }
34         }
35     }
File name: PickupCamera.cs Copy
94     void Apply( Transform dummyTarget, Vector3 dummyCenter )
95     {
96         // Early out if we don't have a target
97         if( !controller )
98             return;
99
100         Vector3 targetCenter = _target.position + centerOffset;
101         Vector3 targetHead = _target.position + headOffset;
102
103         // DebugDrawStuff();
104
105         // Calculate the current & target rotation angles
106         float originalTargetAngle = _target.eulerAngles.y;
107         float currentAngle = cameraTransform.eulerAngles.y;
108
109         // Adjust real target angle when camera is locked
110         float targetAngle = originalTargetAngle;
111
112         // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
113         // It will stop snapping when it reaches the target
114         if( Input.GetButton( "Fire2" ) )
115             snap = true;
116
117         if( snap )
118         {
119             // We are close to the target, so we can stop snapping now!
120             if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
121                 snap = false;
122
123             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
124         }
125         // Normal camera motion
126         else
127         {
128             if( controller.GetLockCameraTimer() < lockCameraTimeout )
129             {
130                 targetAngle = currentAngle;
131             }
132
133             // Lock the camera when moving backwards!
134             // * It is really confusing to do 180 degree spins when turning around.
135             if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
136                 targetAngle += 180;
137
138             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
139         }
140
141
142         // When jumping don't move camera upwards but only down!
143         if( controller.IsJumping() )
144         {
145             // We'd be moving the camera upwards, do that only if it's really high
146             float newTargetHeight = targetCenter.y + height;
147             if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
148                 targetHeight = targetCenter.y + height;
149         }
150         // When walking always update the target height
151         else
152         {
153             targetHeight = targetCenter.y + height;
154         }
155
156         // Damp the height
157         float currentHeight = cameraTransform.position.y;
158         currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
159
160         // Convert the angle into a rotation, by which we then reposition the camera
161         Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
162
163         // Set the position of the camera on the x-z plane to:
164         // distance meters behind the target
165         cameraTransform.position = targetCenter;
166         cameraTransform.position += currentRotation * Vector3.back * distance;
167
168         // Set the height of the camera
169         cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
170
171         // Always look at the target
172         SetUpRotation( targetCenter, targetHead );
173     }
File name: IELdemo.cs Copy
38     // This is one of the callback/event methods called by PUN (read more in PhotonNetworkingMessage enumeration)
39     public void OnCreatedRoom()
40     {
41         Application.LoadLevel(Application.loadedLevel);
42     }
File name: ThirdPersonCamera.cs Copy
86     void Apply( Transform dummyTarget, Vector3 dummyCenter )
87     {
88         // Early out if we don't have a target
89         if( !controller )
90             return;
91
92         Vector3 targetCenter = _target.position + centerOffset;
93         Vector3 targetHead = _target.position + headOffset;
94
95         // DebugDrawStuff();
96
97         // Calculate the current & target rotation angles
98         float originalTargetAngle = _target.eulerAngles.y;
99         float currentAngle = cameraTransform.eulerAngles.y;
100
101         // Adjust real target angle when camera is locked
102         float targetAngle = originalTargetAngle;
103
104         // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
105         // It will stop snapping when it reaches the target
106         if( Input.GetButton( "Fire2" ) )
107             snap = true;
108
109         if( snap )
110         {
111             // We are close to the target, so we can stop snapping now!
112             if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
113                 snap = false;
114
115             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
116         }
117         // Normal camera motion
118         else
119         {
120             if( controller.GetLockCameraTimer() < lockCameraTimeout )
121             {
122                 targetAngle = currentAngle;
123             }
124
125             // Lock the camera when moving backwards!
126             // * It is really confusing to do 180 degree spins when turning around.
127             if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
128                 targetAngle += 180;
129
130             currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
131         }
132
133
134         // When jumping don't move camera upwards but only down!
135         if( controller.IsJumping() )
136         {
137             // We'd be moving the camera upwards, do that only if it's really high
138             float newTargetHeight = targetCenter.y + height;
139             if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
140                 targetHeight = targetCenter.y + height;
141         }
142         // When walking always update the target height
143         else
144         {
145             targetHeight = targetCenter.y + height;
146         }
147
148         // Damp the height
149         float currentHeight = cameraTransform.position.y;
150         currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
151
152         // Convert the angle into a rotation, by which we then reposition the camera
153         Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
154
155         // Set the position of the camera on the x-z plane to:
156         // distance meters behind the target
157         cameraTransform.position = targetCenter;
158         cameraTransform.position += currentRotation * Vector3.back * distance;
159
160         // Set the height of the camera
161         cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
162
163         // Always look at the target
164         SetUpRotation( targetCenter, targetHead );
165     }
File name: WorkerInGame.cs Copy
13     public void Awake()
14     {
15         // in case we started this demo with the wrong scene being active, simply load the menu scene
16         if (!PhotonNetwork.connected)
17         {
18             Application.LoadLevel(WorkerMenu.SceneNameMenu);
19             return;
20         }
21
22         // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
23         PhotonNetwork.Instantiate(this.playerPrefab.name, transform.position, Quaternion.identity, 0);
24     }
File name: WorkerInGame.cs Copy
26     public void OnGUI()
27     {
28         if (GUILayout.Button("Return to Lobby"))
29         {
30             PhotonNetwork.LeaveRoom(); // we will load the menu level when we successfully left the room
31         }
32     }
File name: WorkerInGame.cs Copy
58     public void OnLeftRoom()
59     {
60         Debug.Log("OnLeftRoom (local)");
61
62         // back to main menu
63         Application.LoadLevel(WorkerMenu.SceneNameMenu);
64     }
File name: WorkerInGame.cs Copy
66     public void OnDisconnectedFromPhoton()
67     {
68         Debug.Log("OnDisconnectedFromPhoton");
69
70         // back to main menu
71         Application.LoadLevel(WorkerMenu.SceneNameMenu);
72     }
File name: WorkerInGame.cs Copy
89     public void OnFailedToConnectToPhoton()
90     {
91         Debug.Log("OnFailedToConnectToPhoton");
92
93         // back to main menu
94         Application.LoadLevel(WorkerMenu.SceneNameMenu);
95     }

Download file with original file name:Level

Level 198 lượt xem

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