KeyCode
How do I use Key Code
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: 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: 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: RPGMovement.cs
Copy
106 void UpdateForwardMovement()
107 {
108 if( Input.GetKey( KeyCode.W ) == true )
109 {
110 m_CurrentMovement = transform.forward * ForwardSpeed;
111 }
112 }
File name: RPGMovement.cs
Copy
114 void UpdateBackwardMovement()
115 {
116 if( Input.GetKey( KeyCode.S ) == true )
117 {
118 m_CurrentMovement = -transform.forward * BackwardSpeed;
119 }
120 }
File name: RPGMovement.cs
Copy
122 void UpdateStrafeMovement()
123 {
124 if( Input.GetKey( KeyCode.Q ) == true )
125 {
126 m_CurrentMovement = -transform.right * StrafeSpeed;
127 }
128
129 if( Input.GetKey( KeyCode.E ) == true )
130 {
131 m_CurrentMovement = transform.right * StrafeSpeed;
132 }
133 }
File name: RPGMovement.cs
Copy
135 void UpdateRotateMovement()
136 {
137 if( Input.GetKey( KeyCode.A ) == true )
138 {
139 m_CurrentTurnSpeed = -RotateSpeed;
140 transform.Rotate( 0, -RotateSpeed * Time.deltaTime, 0 );
141 }
142
143 if( Input.GetKey( KeyCode.D ) == true )
144 {
145 m_CurrentTurnSpeed = RotateSpeed;
146 transform.Rotate( 0, RotateSpeed * Time.deltaTime, 0 );
147 }
148 }
File name: PhotonStatsGui.cs
Copy
53 public void Update()
54 {
55 if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
56 {
57 this.statsWindowOn = !this.statsWindowOn;
58 this.statsOn = true; // enable stats when showing the window
59 }
60 }
File name: InRoomChat.cs
Copy
25 public void OnGUI()
26 {
27 if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
28 {
29 return;
30 }
31
32 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
33 {
34 if (!string.IsNullOrEmpty(this.inputLine))
35 {
36 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
37 this.inputLine = "";
38 GUI.FocusControl("");
39 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
40 }
41 else
42 {
43 GUI.FocusControl("ChatInput");
44 }
45 }
46
47 GUI.SetNextControlName("");
48 GUILayout.BeginArea(this.GuiRect);
49
50 scrollPos = GUILayout.BeginScrollView(scrollPos);
51 GUILayout.FlexibleSpace();
52 for (int i = messages.Count - 1; i >= 0; i--)
53 {
54 GUILayout.Label(messages[i]);
55 }
56 GUILayout.EndScrollView();
57
58 GUILayout.BeginHorizontal();
59 GUI.SetNextControlName("ChatInput");
60 inputLine = GUILayout.TextField(inputLine);
61 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
62 {
63 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
64 this.inputLine = "";
65 GUI.FocusControl("");
66 }
67 GUILayout.EndHorizontal();
68 GUILayout.EndArea();
69 }
KeyCode 151 lượt xem
Gõ tìm kiếm nhanh...