34 KeyBoardHomeEndPgUpPgDown,
56 public float SensitivityScale = 3.0f;
58 [Tooltip(
"Use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
59 public bool UseUnscaledTime =
true;
71 private Vector3 lastMousePosition = Vector3.zero;
73 private const float MouseSensitivity = 0.015f;
74 private const float MouseUnlockedSensitivity = 0.1f;
75 private const float KeyboardSensitivity = 10.0f;
76 private const float InputManagerAxisSensitivity = 0.05f;
78 private bool isMouseJumping =
false;
79 private bool appHasFocus =
true;
80 private bool usingMouse =
false;
82 private bool inputManagerAxesNeedApproval =
true;
83 private bool inputManagerHorizontalAxisApproved =
false;
84 private bool inputManagerVerticalAxisApproved =
false;
86 public bool AxisTypeIsKeyboard
88 get {
return AxisType.KeyboardArrows <= axisType && axisType <=
AxisType.KeyBoardHomeEndPgUpPgDown; }
90 public bool AxisTypeIsInputManagerAxis
92 get {
return axisType ==
AxisType.InputManagerAxis; }
94 public bool AxisTypeIsMouse
96 get {
return axisType ==
AxisType.Mouse; }
98 public bool AxisTypeIsMouseScroll
100 get {
return axisType ==
AxisType.MouseScroll; }
105 this.enabled = value;
108 InputManagerAxisCheck();
120 if (IsRunningUnderRemoteDesktop())
125 Debug.LogWarning(
"Running under Remote Desktop, so changed AxisContol method to Left mouse button");
130 Debug.LogWarning(
"Running under Remote Desktop, so changed AxisContol method to Right mouse button");
135 Debug.LogWarning(
"Running under Remote Desktop, so changed AxisContol method to Middle mouse button");
139 UnityEngine.Cursor.lockState = CursorLockMode.None;
144 private static float InputCurve(
float x)
147 return (Mathf.Sign(x) * (1.0f - Mathf.Cos(.5f * Mathf.PI * Mathf.Clamp(x, -1.0f, 1.0f))));
155 Vector3 source = GetDisplacement();
156 Vector3 dest = Vector3.zero;
157 RemapAdditive(source, 0, ref dest, Axis0Destination);
158 RemapAdditive(source, 1, ref dest, Axis1Destination);
159 RemapAdditive(source, 2, ref dest, Axis2Destination);
168 Vector3 source = GetDisplacement();
169 Vector3 middle = Vector3.zero;
170 Vector2 dest = Vector2.zero;
171 RemapAdditive(source, 0, ref middle, Axis0Destination);
172 RemapAdditive(source, 1, ref middle, Axis1Destination);
183 Vector3 source = GetDisplacement();
184 Vector3 middle = Vector2.zero;
185 RemapAdditive(source, 0, ref middle, Axis0Destination);
189 private void RemapAdditive(Vector3 source,
int sourceDim, ref Vector3 dest,
AxisDestination destDim)
191 float inp = source[sourceDim];
210 private Vector3 GetDisplacement()
212 Vector3 rot = Vector3.zero;
215 if (!AxisTypeIsInputManagerAxis)
217 inputManagerAxesNeedApproval =
true;
221 if (AxisTypeIsInputManagerAxis)
223 if (inputManagerAxesNeedApproval)
225 InputManagerAxisCheck();
229 rot = InputManagerAxisLookTick();
232 else if (AxisTypeIsKeyboard)
235 rot = KeyboardLookTick();
237 else if (AxisTypeIsMouseScroll)
240 rot.x += Input.GetAxis(
"Mouse ScrollWheel");
242 else if (AxisTypeIsMouse)
246 if (!this.usingMouse)
249 this.usingMouse =
true;
251 rot = MouseLookTick();
258 this.usingMouse =
false;
263 rot *= this.SensitivityScale;
267 private void OnStartMouseLook()
272 SetWantsMouseJumping(
true);
277 UnityEngine.Cursor.lockState = CursorLockMode.Locked;
284 private void OnEndMouseLook()
289 SetWantsMouseJumping(
false);
294 UnityEngine.Cursor.lockState = CursorLockMode.None;
301 private Vector3 MouseLookTick()
303 Vector3 rot = Vector3.zero;
307 Vector3 mousePositionDelta = Input.mousePosition - this.lastMousePosition;
308 this.lastMousePosition = Input.mousePosition;
310 if (
UnityEngine.Cursor.lockState == CursorLockMode.Locked)
312 mousePositionDelta.x = Input.GetAxis(
"Mouse X");
313 mousePositionDelta.y = Input.GetAxis(
"Mouse Y");
317 mousePositionDelta.x *= MouseUnlockedSensitivity;
318 mousePositionDelta.y *= MouseUnlockedSensitivity;
321 rot.x += -InputCurve(mousePositionDelta.y) * MouseSensitivity;
322 rot.y += InputCurve(mousePositionDelta.x) * MouseSensitivity;
326 private float GetKeyDir(KeyCode neg, KeyCode pos)
328 return Input.GetKey(neg) ? -1.0f : Input.GetKey(pos) ? 1.0f : 0.0f;
331 private float GetKeyDir(
string neg,
string pos)
333 return Input.GetKey(neg) ? -1.0f : Input.GetKey(pos) ? 1.0f : 0.0f;
336 private void InputManagerAxisCheck()
338 inputManagerHorizontalAxisApproved =
false;
339 inputManagerVerticalAxisApproved =
false;
342 inputManagerHorizontalAxisApproved =
true;
345 Input.GetAxis(InputManagerHorizontalAxisName);
349 Debug.LogWarningFormat(
"Input Axis {0} is not setup. Use Edit -> Project Settings -> Input", InputManagerHorizontalAxisName);
350 inputManagerHorizontalAxisApproved =
false;
355 inputManagerVerticalAxisApproved =
true;
358 Input.GetAxis(InputManagerVerticalAxisName);
362 Debug.LogWarningFormat(
"Input Axis {0} is not setup. Use Edit -> Project Settings -> Input", InputManagerVerticalAxisName);
363 inputManagerVerticalAxisApproved =
false;
366 inputManagerAxesNeedApproval =
false;
369 private Vector3 InputManagerAxisLookTick()
371 Vector3 rot = Vector3.zero;
372 if (inputManagerHorizontalAxisApproved)
374 rot.x += InputManagerAxisSensitivity * InputCurve(Input.GetAxis(InputManagerHorizontalAxisName));
376 if (inputManagerVerticalAxisApproved)
378 rot.y += InputManagerAxisSensitivity * InputCurve(Input.GetAxis(InputManagerVerticalAxisName));
383 private Vector3 KeyboardLookTick()
385 float deltaTime = UseUnscaledTime
386 ? Time.unscaledDeltaTime
389 Vector3 rot = Vector3.zero;
390 if (axisType ==
AxisType.KeyboardArrows)
392 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftArrow, KeyCode.RightArrow));
393 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.DownArrow, KeyCode.UpArrow));
395 else if (axisType ==
AxisType.KeyboardWASD)
397 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.A, KeyCode.D));
398 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.S, KeyCode.W));
400 else if (axisType ==
AxisType.KeyboardQE)
402 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Q, KeyCode.E));
404 else if (axisType ==
AxisType.KeyboardIJKL)
406 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.J, KeyCode.L));
407 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.K, KeyCode.I));
409 else if (axisType ==
AxisType.KeyboardUO)
411 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.U, KeyCode.O));
413 else if (axisType ==
AxisType.Keyboard8426)
415 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad4, KeyCode.Keypad6));
416 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad2, KeyCode.Keypad8));
418 else if (axisType ==
AxisType.Keyboard7193)
420 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad1, KeyCode.Keypad7));
421 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Keypad3, KeyCode.Keypad9));
423 else if (axisType ==
AxisType.KeyboardPeriodComma)
425 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.Comma, KeyCode.Period));
427 else if (axisType ==
AxisType.KeyboardBrackets)
429 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.LeftBracket, KeyCode.RightBracket));
431 else if (axisType ==
AxisType.KeyBoardHomeEndPgUpPgDown)
433 rot.x += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.End, KeyCode.Home));
434 rot.y += InputCurve(deltaTime * KeyboardSensitivity * GetKeyDir(KeyCode.PageDown, KeyCode.PageUp));
447 if (!this.appHasFocus)
457 return Input.GetMouseButton((
int)this.buttonType);
461 return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
465 return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
469 return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
473 return Input.GetKey(KeyCode.Space);
477 return Input.GetKey(KeyCode.Return);
481 if (!this.usingMouse)
491 return !Input.GetKeyDown(KeyCode.Escape);
498 private void OnApplicationFocus(
bool focusStatus)
500 this.appHasFocus = focusStatus;
509 private void SetWantsMouseJumping(
bool wantsJumping)
511 if (wantsJumping != this.isMouseJumping)
513 this.isMouseJumping = wantsJumping;
518 UnityEngine.Cursor.lockState = CursorLockMode.None;
523 this.lastMousePosition = Input.mousePosition;
528 UnityEngine.Cursor.lockState = CursorLockMode.Locked;
529 UnityEngine.Cursor.lockState = CursorLockMode.None;
536 UnityEditor.EditorGUIUtility.SetWantsMouseJumping(wantsJumping ? 1 : 0);
542 [
System.Runtime.InteropServices.DllImport(
"kernel32.dll")]
543 private static extern uint GetCurrentProcessId();
545 [
System.Runtime.InteropServices.DllImport(
"kernel32.dll")]
546 private static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);
548 [
System.Runtime.InteropServices.DllImport(
"kernel32.dll")]
549 private static extern uint WTSGetActiveConsoleSessionId();
551 private bool IsRunningUnderRemoteDesktop()
553 uint processId = GetCurrentProcessId();
555 return ProcessIdToSessionId(processId, out sessionId) && (sessionId != WTSGetActiveConsoleSessionId());
558 private bool IsRunningUnderRemoteDesktop()