Layout
How do I use Layout
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: 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: ChatGui.cs
Copy
103 public void OnGUI()
104 {
105 if (!this.IsVisible)
106 {
107 return;
108 }
109
110 GUI.skin.label.wordWrap = true;
111 //GUI.skin.button.richText = true; // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
112 //GUILayout.Button("lala"); // as richText, html tags could be in text
113
114
115 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
116 {
117 if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
118 {
119 // focus on input -> submit it
120 GuiSendsMsg();
121 return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
122 }
123 else
124 {
125 // assign focus to input
126 GUI.FocusControl("ChatInput");
127 }
128 }
129
130 GUI.SetNextControlName("");
131 GUILayout.BeginArea(this.GuiRect);
132
133 GUILayout.FlexibleSpace();
134
135 if (this.chatClient.State != ChatState.ConnectedToFrontEnd)
136 {
137 GUILayout.Label("Not in chat yet.");
138 }
139 else
140 {
141 List
142 int countOfPublicChannels = channels.Count;
143 channels.AddRange(this.chatClient.PrivateChannels.Keys);
144
145 if (channels.Count > 0)
146 {
147 int previouslySelectedChannelIndex = this.selectedChannelIndex;
148 int channelIndex = channels.IndexOf(this.selectedChannelName);
149 this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;
150
151 this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
152 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
153
154 this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
155 this.selectedChannelName = channels[this.selectedChannelIndex];
156
157 if (this.selectedChannelIndex != previouslySelectedChannelIndex)
158 {
159 // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
160 this.scrollPos.y = float.MaxValue;
161 if (this.doingPrivateChat)
162 {
163 string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
164 this.userIdInput = pieces[1];
165 }
166 }
167
168 GUILayout.Label(ChatGui.WelcomeText);
169
170 if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
171 {
172 for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
173 {
174 string sender = this.selectedChannel.Senders[i];
175 object message = this.selectedChannel.Messages[i];
176 GUILayout.Label(string.Format("{0}: {1}", sender, message));
177 }
178 }
179
180 GUILayout.EndScrollView();
181 }
182 }
183
184
185 GUILayout.BeginHorizontal();
186 if (doingPrivateChat)
187 {
188 GUILayout.Label("to:", GUILayout.ExpandWidth(false));
189 GUI.SetNextControlName("WhisperTo");
190 this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
191 string focussed = GUI.GetNameOfFocusedControl();
192 if (focussed.Equals("WhisperTo"))
193 {
194 if (this.userIdInput.Equals("username"))
195 {
196 this.userIdInput = "";
197 }
198 }
199 else if (string.IsNullOrEmpty(this.userIdInput))
200 {
201 this.userIdInput = "username";
202 }
203
204 }
205 GUI.SetNextControlName("ChatInput");
206 inputLine = GUILayout.TextField(inputLine);
207 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
208 {
209 GuiSendsMsg();
210 }
211 GUILayout.EndHorizontal();
212 GUILayout.EndArea();
213 }
File name: NamePickGui.cs
Copy
38 public void OnGUI()
39 {
40 // Enter-Key handling:
41 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
42 {
43 if (!string.IsNullOrEmpty(this.InputLine))
44 {
45 this.StartChat();
46 return;
47 }
48 }
49
50
51 GUI.skin.label.wordWrap = true;
52 GUILayout.BeginArea(guiCenteredRect);
53
54
55 if (this.chatComponent != null && string.IsNullOrEmpty(this.chatComponent.ChatAppId))
56 {
57 GUILayout.Label("To continue, configure your Chat AppId.\nIt's listed in the Chat Dashboard (online).\nStop play-mode and edit:\nScripts/ChatGUI in the Hierarchy.");
58 if (GUILayout.Button("Open Chat Dashboard"))
59 {
60 Application.OpenURL("https://www.exitgames.com/en/Chat/Dashboard");
61 }
62 GUILayout.EndArea();
63 return;
64 }
65
66 GUILayout.Label(this.helpText);
67
68 GUILayout.BeginHorizontal();
69 GUI.SetNextControlName("NameInput");
70 this.InputLine = GUILayout.TextField(this.InputLine);
71 if (GUILayout.Button("Connect", GUILayout.ExpandWidth(false)))
72 {
73 this.StartChat();
74 }
75 GUILayout.EndHorizontal();
76
77 GUILayout.EndArea();
78
79
80 GUI.FocusControl("NameInput");
81 }
File name: GUICustomAuth.cs
Copy
81 void OnGUI()
82 {
83 if (PhotonNetwork.connected)
84 {
85 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
86 return;
87 }
88
89
90 GUILayout.BeginArea(GuiRect);
91 switch (guiState)
92 {
93 case GuiState.AuthFailed:
94 GUILayout.Label("Authentication Failed");
95
96 GUILayout.Space(10);
97
98 GUILayout.Label("Error message:\n'" + this.authDebugMessage + "'");
99
100 GUILayout.Space(10);
101
102 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
103 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
104 GUILayout.Label("The error message comes from that service and can be customized.");
105
106 GUILayout.Space(10);
107
108 GUILayout.BeginHorizontal();
109 if (GUILayout.Button("Back"))
110 {
111 SetStateAuthInput();
112 }
113 if (GUILayout.Button("Help"))
114 {
115 SetStateAuthHelp();
116 }
117 GUILayout.EndHorizontal();
118 break;
119
120 case GuiState.AuthHelp:
121
122 GUILayout.Label("By default, any player can connect to Photon.\n'Custom Authentication' can be enabled to reject players without valid user-account.");
123
124 GUILayout.Label("The actual authentication must be done by a web-service which you host and customize. Example sourcecode for these services is available on the docs page.");
125
126 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
127 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
128
129 GUILayout.Space(10);
130 if (GUILayout.Button("Configure Authentication (Dashboard)"))
131 {
132 Application.OpenURL("https://cloud.exitgames.com/dashboard");
133 }
134 if (GUILayout.Button("Authentication Docs"))
135 {
136 Application.OpenURL("https://doc.exitgames.com/en/pun/current/tutorials/pun-and-facebook-custom-authentication");
137 }
138
139
140 GUILayout.Space(10);
141 if (GUILayout.Button("Back to input"))
142 {
143 SetStateAuthInput();
144 }
145 break;
146
147 case GuiState.AuthInput:
148
149 GUILayout.Label("Authenticate yourself");
150
151 GUILayout.BeginHorizontal();
152 this.authName = GUILayout.TextField(this.authName, GUILayout.Width(Screen.width/4 - 5));
153 GUILayout.FlexibleSpace();
154 this.authToken = GUILayout.TextField(this.authToken, GUILayout.Width(Screen.width/4 - 5));
155 GUILayout.EndHorizontal();
156
157
158 if (GUILayout.Button("Authenticate"))
159 {
160 PhotonNetwork.AuthValues = new AuthenticationValues();
161 PhotonNetwork.AuthValues.SetAuthParameters(this.authName, this.authToken);
162 PhotonNetwork.ConnectUsingSettings("1.0");
163 }
164
165 GUILayout.Space(10);
166
167 if (GUILayout.Button("Help", GUILayout.Width(100)))
168 {
169 SetStateAuthHelp();
170 }
171
172 break;
173 }
174
175 GUILayout.EndArea();
176 }
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: GUIFriendFinding.cs
Copy
52 public void OnGUI()
53 {
54 if (!PhotonNetwork.insideLobby)
55 {
56 // this feature is only available on the Master Client. Check either: insideLobby or
57 // PhotonNetwork.connectionStateDetailed == PeerState.Authenticated or
58 // PhotonNetwork.connectionStateDetailed == PeerState.JoinedLobby
59
60 // for simplicity (and cause we know we WILL join the lobby, we can just check that)
61 return;
62 }
63
64
65 GUILayout.BeginArea(GuiRect);
66
67 GUILayout.Label("Your (random) name: " + PhotonNetwork.playerName);
68 GUILayout.Label("Your friends: " + string.Join(", ",this.friendListOfSomeCommunity));
69
70
71 GUILayout.BeginHorizontal();
72 if (GUILayout.Button("Find Friends"))
73 {
74 PhotonNetwork.FindFriends(this.friendListOfSomeCommunity);
75 }
76 if (GUILayout.Button("Create Room"))
77 {
78 PhotonNetwork.CreateRoom(null); // just a random room
79 }
80 GUILayout.EndHorizontal();
81
82
83 if (PhotonNetwork.Friends != null)
84 {
85 foreach (FriendInfo info in PhotonNetwork.Friends)
86 {
87 GUILayout.BeginHorizontal();
88 GUILayout.Label(info.ToString());
89 if (info.IsInRoom)
90 {
91 if (GUILayout.Button("join"))
92 {
93 PhotonNetwork.JoinRoom(info.Room);
94 }
95 }
96 GUILayout.EndHorizontal();
97 }
98 }
99
100 GUILayout.EndArea();
101 }
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: 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
61 public void OnGUI()
62 {
63 GUI.skin = Skin;
64
65 string[] synchronizeTypeContent = new string[] { "Disabled", "Discrete", "Continuous" };
66
67 GUILayout.BeginArea( new Rect( Screen.width - 200 * m_FoundPlayerSlideIn - 400 * m_SlideIn, 0, 600, Screen.height ), GUI.skin.box );
68 {
69 GUILayout.Label( "Mecanim Demo", GUI.skin.customStyles[ 0 ] );
70
71 GUI.color = Color.white;
72 string label = "Settings";
73
74 if( m_IsOpen == true )
75 {
76 label = "Close";
77 }
78
79 if( GUILayout.Button( label, GUILayout.Width( 110 ) ) )
80 {
81 m_IsOpen = !m_IsOpen;
82 }
83
84 string parameters = "";
85
86 if( m_AnimatorView != null )
87 {
88 parameters += "Send Values:\n";
89
90 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
91 {
92 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
93
94 try
95 {
96 switch( parameter.Type )
97 {
98 case PhotonAnimatorView.ParameterType.Bool:
99 parameters += parameter.Name + " (" + ( m_AnimatorView.GetComponent
100 break;
101 case PhotonAnimatorView.ParameterType.Int:
102 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
103 break;
104 case PhotonAnimatorView.ParameterType.Float:
105 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
106 break;
107 }
108 }
109 catch
110 {
111 Debug.Log( "derrrr for " + parameter.Name );
112 }
113 }
114 }
115
116 if( m_RemoteAnimator != null )
117 {
118 parameters += "\nReceived Values:\n";
119
120 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
121 {
122 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
123
124 try
125 {
126 switch( parameter.Type )
127 {
128 case PhotonAnimatorView.ParameterType.Bool:
129 parameters += parameter.Name + " (" + ( m_RemoteAnimator.GetBool( parameter.Name ) ? "True" : "False" ) + ")\n";
130 break;
131 case PhotonAnimatorView.ParameterType.Int:
132 parameters += parameter.Name + " (" + m_RemoteAnimator.GetInteger( parameter.Name ) + ")\n";
133 break;
134 case PhotonAnimatorView.ParameterType.Float:
135 parameters += parameter.Name + " (" + m_RemoteAnimator.GetFloat( parameter.Name ).ToString( "0.00" ) + ")\n";
136 break;
137 }
138 }
139 catch
140 {
141 Debug.Log( "derrrr for " + parameter.Name );
142 }
143 }
144 }
145
146 GUIStyle style = new GUIStyle( GUI.skin.label );
147 style.alignment = TextAnchor.UpperLeft;
148
149 GUI.color = new Color( 1f, 1f, 1f, 1 - m_SlideIn );
150 GUI.Label( new Rect( 10, 100, 600, Screen.height ), parameters, style );
151
152 if( m_AnimatorView != null )
153 {
154 GUI.color = new Color( 1f, 1f, 1f, m_SlideIn );
155
156 GUILayout.Space( 20 );
157 GUILayout.Label( "Synchronize Parameters" );
158
159 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
160 {
161 GUILayout.BeginHorizontal();
162 {
163 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
164
165 GUILayout.Label( parameter.Name, GUILayout.Width( 100 ), GUILayout.Height( 36 ) );
166
167 int selectedValue = (int)parameter.SynchronizeType;
168 int newValue = GUILayout.Toolbar( selectedValue, synchronizeTypeContent );
169
170 if( newValue != selectedValue )
171 {
172 m_AnimatorView.SetParameterSynchronized( parameter.Name, parameter.Type, (PhotonAnimatorView.SynchronizeType)newValue );
173 }
174 }
175 GUILayout.EndHorizontal();
176 }
177 }
178 }
179 GUILayout.EndArea();
180 }
Download file with original file name:Layout
Layout 141 lượt xem
Gõ tìm kiếm nhanh...