Last active
March 15, 2016 22:58
-
-
Save talecrafter/35fb1441fd3ba0ff7ae9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using CraftingLegends.Framework; | |
using CraftingLegends.Core; | |
using System; | |
using CraftingLegends.ExtendedFramework; | |
/// <summary> | |
/// short example class showing how you can adapt to user switching control scheme | |
/// won't compile | |
/// </summary> | |
public class ExampleGameInput : MonoBehaviour | |
{ | |
public enum InputScheme : int | |
{ | |
None, | |
Mouse, | |
Gamepad, | |
Touch | |
} | |
// this is the most important part: other components like GUI should listen to this and then adapt when changed | |
public event Action<InputScheme> inputSchemeChanged; | |
[SerializeField] | |
private InputScheme _inputScheme = InputScheme.None; | |
public InputScheme inputScheme // public so other components can look at this when initializing | |
{ | |
get | |
{ | |
return _inputScheme; | |
} | |
} | |
private bool anyMouseButtonDown | |
{ | |
get | |
{ | |
return Input.GetMouseButtonDown(0) | |
|| Input.GetMouseButtonDown(1) | |
|| Input.GetMouseButtonDown(2); | |
} | |
} | |
private Vector3? _mousePosStart = null; | |
private bool mouseHasMoved | |
{ | |
get | |
{ | |
if (_mousePosStart == null) | |
return false; | |
return _mousePosStart != Input.mousePosition; | |
} | |
} | |
// ================================================================================ | |
// unity methods | |
// -------------------------------------------------------------------------------- | |
void Awake() | |
{ | |
if (Input.GetJoystickNames().Length > 0) | |
{ | |
SetInputScheme(InputScheme.Gamepad); | |
} | |
else if (MainBase.Info.hasTouch) // game specific code, looks for current platform | |
{ | |
SetInputScheme(InputScheme.Touch); | |
} | |
else | |
{ | |
SetInputScheme(InputScheme.Mouse); | |
} | |
} | |
void Update() | |
{ | |
CheckInputScheme(); | |
} | |
// ================================================================================ | |
// private methods | |
// -------------------------------------------------------------------------------- | |
/// <summary> | |
/// listen for input entries which indicate a specific control scheme | |
/// </summary> | |
private void CheckInputScheme() | |
{ | |
if (Input.touchCount > 0) | |
{ | |
SetInputScheme(InputScheme.Touch); | |
} | |
else if (mouseHasMoved || anyMouseButtonDown) | |
{ | |
SetInputScheme(InputScheme.Mouse); | |
} | |
// this is very specific code, this game uses a wrapper around the InControl plugin | |
else if (MainBase.Instance.gamepadInput != null | |
&& (MainBase.Instance.gamepadInput.anyButton || MainBase.Instance.gamepadInput.hasMovement | |
|| MainBase.Instance.gamepadInput.hasLookDirection)) | |
{ | |
SetInputScheme(InputScheme.Gamepad); | |
} | |
} | |
private void SetInputScheme(InputScheme newScheme) | |
{ | |
if (newScheme == _inputScheme) | |
return; | |
_inputScheme = newScheme; | |
// save mouse position so we can check if the mouse is moved | |
if (newScheme == InputScheme.Mouse) | |
{ | |
_mousePosStart = null; | |
} | |
else | |
{ | |
_mousePosStart = Input.mousePosition; | |
} | |
OnInputSchemeChanged(); | |
} | |
private void OnInputSchemeChanged() | |
{ | |
if (inputSchemeChanged != null) | |
{ | |
inputSchemeChanged(_inputScheme); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment