Initial commit
This commit is contained in:
1 parent
ee9615239e
commit
16feac691f
4093 files changed
+1030491
No files matched your search
+318
@@ -0,0 +1,318 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityStandardAssets.CrossPlatformInput.PlatformSpecific;
|
||||
|
||||
namespace UnityStandardAssets.CrossPlatformInput
|
||||
{
|
||||
public static class CrossPlatformInputManager
|
||||
{
|
||||
public enum ActiveInputMethod
|
||||
{
|
||||
Hardware,
|
||||
Touch
|
||||
}
|
||||
|
||||
|
||||
private static VirtualInput activeInput;
|
||||
|
||||
private static VirtualInput s_TouchInput;
|
||||
private static VirtualInput s_HardwareInput;
|
||||
|
||||
|
||||
static CrossPlatformInputManager()
|
||||
{
|
||||
s_TouchInput = new MobileInput();
|
||||
s_HardwareInput = new StandaloneInput();
|
||||
#if MOBILE_INPUT
|
||||
activeInput = s_TouchInput;
|
||||
#else
|
||||
activeInput = s_HardwareInput;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void SwitchActiveInputMethod(ActiveInputMethod activeInputMethod)
|
||||
{
|
||||
switch (activeInputMethod)
|
||||
{
|
||||
case ActiveInputMethod.Hardware:
|
||||
activeInput = s_HardwareInput;
|
||||
break;
|
||||
|
||||
case ActiveInputMethod.Touch:
|
||||
activeInput = s_TouchInput;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool AxisExists(string name)
|
||||
{
|
||||
return activeInput.AxisExists(name);
|
||||
}
|
||||
|
||||
public static bool ButtonExists(string name)
|
||||
{
|
||||
return activeInput.ButtonExists(name);
|
||||
}
|
||||
|
||||
public static void RegisterVirtualAxis(VirtualAxis axis)
|
||||
{
|
||||
activeInput.RegisterVirtualAxis(axis);
|
||||
}
|
||||
|
||||
|
||||
public static void RegisterVirtualButton(VirtualButton button)
|
||||
{
|
||||
activeInput.RegisterVirtualButton(button);
|
||||
}
|
||||
|
||||
|
||||
public static void UnRegisterVirtualAxis(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException("name");
|
||||
}
|
||||
activeInput.UnRegisterVirtualAxis(name);
|
||||
}
|
||||
|
||||
|
||||
public static void UnRegisterVirtualButton(string name)
|
||||
{
|
||||
activeInput.UnRegisterVirtualButton(name);
|
||||
}
|
||||
|
||||
|
||||
// returns a reference to a named virtual axis if it exists otherwise null
|
||||
public static VirtualAxis VirtualAxisReference(string name)
|
||||
{
|
||||
return activeInput.VirtualAxisReference(name);
|
||||
}
|
||||
|
||||
|
||||
// returns the platform appropriate axis for the given name
|
||||
public static float GetAxis(string name)
|
||||
{
|
||||
return GetAxis(name, false);
|
||||
}
|
||||
|
||||
|
||||
public static float GetAxisRaw(string name)
|
||||
{
|
||||
return GetAxis(name, true);
|
||||
}
|
||||
|
||||
|
||||
// private function handles both types of axis (raw and not raw)
|
||||
private static float GetAxis(string name, bool raw)
|
||||
{
|
||||
return activeInput.GetAxis(name, raw);
|
||||
}
|
||||
|
||||
|
||||
// -- Button handling --
|
||||
public static bool GetButton(string name)
|
||||
{
|
||||
return activeInput.GetButton(name);
|
||||
}
|
||||
|
||||
|
||||
public static bool GetButtonDown(string name)
|
||||
{
|
||||
return activeInput.GetButtonDown(name);
|
||||
}
|
||||
|
||||
|
||||
public static bool GetButtonUp(string name)
|
||||
{
|
||||
return activeInput.GetButtonUp(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetButtonDown(string name)
|
||||
{
|
||||
activeInput.SetButtonDown(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetButtonUp(string name)
|
||||
{
|
||||
activeInput.SetButtonUp(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetAxisPositive(string name)
|
||||
{
|
||||
activeInput.SetAxisPositive(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetAxisNegative(string name)
|
||||
{
|
||||
activeInput.SetAxisNegative(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetAxisZero(string name)
|
||||
{
|
||||
activeInput.SetAxisZero(name);
|
||||
}
|
||||
|
||||
|
||||
public static void SetAxis(string name, float value)
|
||||
{
|
||||
activeInput.SetAxis(name, value);
|
||||
}
|
||||
|
||||
|
||||
public static Vector3 mousePosition
|
||||
{
|
||||
get { return activeInput.MousePosition(); }
|
||||
}
|
||||
|
||||
|
||||
public static void SetVirtualMousePositionX(float f)
|
||||
{
|
||||
activeInput.SetVirtualMousePositionX(f);
|
||||
}
|
||||
|
||||
|
||||
public static void SetVirtualMousePositionY(float f)
|
||||
{
|
||||
activeInput.SetVirtualMousePositionY(f);
|
||||
}
|
||||
|
||||
|
||||
public static void SetVirtualMousePositionZ(float f)
|
||||
{
|
||||
activeInput.SetVirtualMousePositionZ(f);
|
||||
}
|
||||
|
||||
|
||||
// virtual axis and button classes - applies to mobile input
|
||||
// Can be mapped to touch joysticks, tilt, gyro, etc, depending on desired implementation.
|
||||
// Could also be implemented by other input devices - kinect, electronic sensors, etc
|
||||
public class VirtualAxis
|
||||
{
|
||||
public string name { get; private set; }
|
||||
private float m_Value;
|
||||
public bool matchWithInputManager { get; private set; }
|
||||
|
||||
|
||||
public VirtualAxis(string name)
|
||||
: this(name, true)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public VirtualAxis(string name, bool matchToInputSettings)
|
||||
{
|
||||
this.name = name;
|
||||
matchWithInputManager = matchToInputSettings;
|
||||
}
|
||||
|
||||
|
||||
// removes an axes from the cross platform input system
|
||||
public void Remove()
|
||||
{
|
||||
UnRegisterVirtualAxis(name);
|
||||
}
|
||||
|
||||
|
||||
// a controller gameobject (eg. a virtual thumbstick) should update this class
|
||||
public void Update(float value)
|
||||
{
|
||||
m_Value = value;
|
||||
}
|
||||
|
||||
|
||||
public float GetValue
|
||||
{
|
||||
get { return m_Value; }
|
||||
}
|
||||
|
||||
|
||||
public float GetValueRaw
|
||||
{
|
||||
get { return m_Value; }
|
||||
}
|
||||
}
|
||||
|
||||
// a controller gameobject (eg. a virtual GUI button) should call the
|
||||
// 'pressed' function of this class. Other objects can then read the
|
||||
// Get/Down/Up state of this button.
|
||||
public class VirtualButton
|
||||
{
|
||||
public string name { get; private set; }
|
||||
public bool matchWithInputManager { get; private set; }
|
||||
|
||||
private int m_LastPressedFrame = -5;
|
||||
private int m_ReleasedFrame = -5;
|
||||
private bool m_Pressed;
|
||||
|
||||
|
||||
public VirtualButton(string name)
|
||||
: this(name, true)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public VirtualButton(string name, bool matchToInputSettings)
|
||||
{
|
||||
this.name = name;
|
||||
matchWithInputManager = matchToInputSettings;
|
||||
}
|
||||
|
||||
|
||||
// A controller gameobject should call this function when the button is pressed down
|
||||
public void Pressed()
|
||||
{
|
||||
if (m_Pressed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Pressed = true;
|
||||
m_LastPressedFrame = Time.frameCount;
|
||||
}
|
||||
|
||||
|
||||
// A controller gameobject should call this function when the button is released
|
||||
public void Released()
|
||||
{
|
||||
m_Pressed = false;
|
||||
m_ReleasedFrame = Time.frameCount;
|
||||
}
|
||||
|
||||
|
||||
// the controller gameobject should call Remove when the button is destroyed or disabled
|
||||
public void Remove()
|
||||
{
|
||||
UnRegisterVirtualButton(name);
|
||||
}
|
||||
|
||||
|
||||
// these are the states of the button which can be read via the cross platform input system
|
||||
public bool GetButton
|
||||
{
|
||||
get { return m_Pressed; }
|
||||
}
|
||||
|
||||
|
||||
public bool GetButtonDown
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_LastPressedFrame - Time.frameCount == -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool GetButtonUp
|
||||
{
|
||||
get
|
||||
{
|
||||
return (m_ReleasedFrame == Time.frameCount - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ac1ce5a5adfd9f46adbf5b6f752a47c
|
||||
labels:
|
||||
- Done
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -1010
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bc72db1e9dcb9647818df5a07871127
|
||||
folderAsset: yes
|
||||
timeCreated: 1436977288
|
||||
licenseType: Store
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityStandardAssets.CrossPlatformInput.PlatformSpecific
|
||||
{
|
||||
public class MobileInput : VirtualInput
|
||||
{
|
||||
private void AddButton(string name)
|
||||
{
|
||||
// we have not registered this button yet so add it, happens in the constructor
|
||||
CrossPlatformInputManager.RegisterVirtualButton(new CrossPlatformInputManager.VirtualButton(name));
|
||||
}
|
||||
|
||||
|
||||
private void AddAxes(string name)
|
||||
{
|
||||
// we have not registered this button yet so add it, happens in the constructor
|
||||
CrossPlatformInputManager.RegisterVirtualAxis(new CrossPlatformInputManager.VirtualAxis(name));
|
||||
}
|
||||
|
||||
|
||||
public override float GetAxis(string name, bool raw)
|
||||
{
|
||||
if (!m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
AddAxes(name);
|
||||
}
|
||||
return m_VirtualAxes[name].GetValue;
|
||||
}
|
||||
|
||||
|
||||
public override void SetButtonDown(string name)
|
||||
{
|
||||
if (!m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
AddButton(name);
|
||||
}
|
||||
m_VirtualButtons[name].Pressed();
|
||||
}
|
||||
|
||||
|
||||
public override void SetButtonUp(string name)
|
||||
{
|
||||
if (!m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
AddButton(name);
|
||||
}
|
||||
m_VirtualButtons[name].Released();
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisPositive(string name)
|
||||
{
|
||||
if (!m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
AddAxes(name);
|
||||
}
|
||||
m_VirtualAxes[name].Update(1f);
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisNegative(string name)
|
||||
{
|
||||
if (!m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
AddAxes(name);
|
||||
}
|
||||
m_VirtualAxes[name].Update(-1f);
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisZero(string name)
|
||||
{
|
||||
if (!m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
AddAxes(name);
|
||||
}
|
||||
m_VirtualAxes[name].Update(0f);
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxis(string name, float value)
|
||||
{
|
||||
if (!m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
AddAxes(name);
|
||||
}
|
||||
m_VirtualAxes[name].Update(value);
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButtonDown(string name)
|
||||
{
|
||||
if (m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
return m_VirtualButtons[name].GetButtonDown;
|
||||
}
|
||||
|
||||
AddButton(name);
|
||||
return m_VirtualButtons[name].GetButtonDown;
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButtonUp(string name)
|
||||
{
|
||||
if (m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
return m_VirtualButtons[name].GetButtonUp;
|
||||
}
|
||||
|
||||
AddButton(name);
|
||||
return m_VirtualButtons[name].GetButtonUp;
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButton(string name)
|
||||
{
|
||||
if (m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
return m_VirtualButtons[name].GetButton;
|
||||
}
|
||||
|
||||
AddButton(name);
|
||||
return m_VirtualButtons[name].GetButton;
|
||||
}
|
||||
|
||||
|
||||
public override Vector3 MousePosition()
|
||||
{
|
||||
return virtualMousePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9703d53e47195aa4190acd11369ccd1b
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityStandardAssets.CrossPlatformInput.PlatformSpecific
|
||||
{
|
||||
public class StandaloneInput : VirtualInput
|
||||
{
|
||||
public override float GetAxis(string name, bool raw)
|
||||
{
|
||||
return raw ? Input.GetAxisRaw(name) : Input.GetAxis(name);
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButton(string name)
|
||||
{
|
||||
return Input.GetButton(name);
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButtonDown(string name)
|
||||
{
|
||||
return Input.GetButtonDown(name);
|
||||
}
|
||||
|
||||
|
||||
public override bool GetButtonUp(string name)
|
||||
{
|
||||
return Input.GetButtonUp(name);
|
||||
}
|
||||
|
||||
|
||||
public override void SetButtonDown(string name)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override void SetButtonUp(string name)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisPositive(string name)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisNegative(string name)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxisZero(string name)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override void SetAxis(string name, float value)
|
||||
{
|
||||
throw new Exception(
|
||||
" This is not possible to be called for standalone input. Please check your platform and code where this is called");
|
||||
}
|
||||
|
||||
|
||||
public override Vector3 MousePosition()
|
||||
{
|
||||
return Input.mousePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9961032f4f02c4f41997c3ea399d2f22
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace UnityStandardAssets.CrossPlatformInput
|
||||
{
|
||||
public abstract class VirtualInput
|
||||
{
|
||||
public Vector3 virtualMousePosition { get; private set; }
|
||||
|
||||
|
||||
protected Dictionary<string, CrossPlatformInputManager.VirtualAxis> m_VirtualAxes =
|
||||
new Dictionary<string, CrossPlatformInputManager.VirtualAxis>();
|
||||
// Dictionary to store the name relating to the virtual axes
|
||||
protected Dictionary<string, CrossPlatformInputManager.VirtualButton> m_VirtualButtons =
|
||||
new Dictionary<string, CrossPlatformInputManager.VirtualButton>();
|
||||
protected List<string> m_AlwaysUseVirtual = new List<string>();
|
||||
// list of the axis and button names that have been flagged to always use a virtual axis or button
|
||||
|
||||
|
||||
public bool AxisExists(string name)
|
||||
{
|
||||
return m_VirtualAxes.ContainsKey(name);
|
||||
}
|
||||
|
||||
public bool ButtonExists(string name)
|
||||
{
|
||||
return m_VirtualButtons.ContainsKey(name);
|
||||
}
|
||||
|
||||
|
||||
public void RegisterVirtualAxis(CrossPlatformInputManager.VirtualAxis axis)
|
||||
{
|
||||
// check if we already have an axis with that name and log and error if we do
|
||||
if (m_VirtualAxes.ContainsKey(axis.name))
|
||||
{
|
||||
Debug.LogError("There is already a virtual axis named " + axis.name + " registered.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// add any new axes
|
||||
m_VirtualAxes.Add(axis.name, axis);
|
||||
|
||||
// if we dont want to match with the input manager setting then revert to always using virtual
|
||||
if (!axis.matchWithInputManager)
|
||||
{
|
||||
m_AlwaysUseVirtual.Add(axis.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void RegisterVirtualButton(CrossPlatformInputManager.VirtualButton button)
|
||||
{
|
||||
// check if already have a buttin with that name and log an error if we do
|
||||
if (m_VirtualButtons.ContainsKey(button.name))
|
||||
{
|
||||
Debug.LogError("There is already a virtual button named " + button.name + " registered.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// add any new buttons
|
||||
m_VirtualButtons.Add(button.name, button);
|
||||
|
||||
// if we dont want to match to the input manager then always use a virtual axis
|
||||
if (!button.matchWithInputManager)
|
||||
{
|
||||
m_AlwaysUseVirtual.Add(button.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UnRegisterVirtualAxis(string name)
|
||||
{
|
||||
// if we have an axis with that name then remove it from our dictionary of registered axes
|
||||
if (m_VirtualAxes.ContainsKey(name))
|
||||
{
|
||||
m_VirtualAxes.Remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UnRegisterVirtualButton(string name)
|
||||
{
|
||||
// if we have a button with this name then remove it from our dictionary of registered buttons
|
||||
if (m_VirtualButtons.ContainsKey(name))
|
||||
{
|
||||
m_VirtualButtons.Remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// returns a reference to a named virtual axis if it exists otherwise null
|
||||
public CrossPlatformInputManager.VirtualAxis VirtualAxisReference(string name)
|
||||
{
|
||||
return m_VirtualAxes.ContainsKey(name) ? m_VirtualAxes[name] : null;
|
||||
}
|
||||
|
||||
|
||||
public void SetVirtualMousePositionX(float f)
|
||||
{
|
||||
virtualMousePosition = new Vector3(f, virtualMousePosition.y, virtualMousePosition.z);
|
||||
}
|
||||
|
||||
|
||||
public void SetVirtualMousePositionY(float f)
|
||||
{
|
||||
virtualMousePosition = new Vector3(virtualMousePosition.x, f, virtualMousePosition.z);
|
||||
}
|
||||
|
||||
|
||||
public void SetVirtualMousePositionZ(float f)
|
||||
{
|
||||
virtualMousePosition = new Vector3(virtualMousePosition.x, virtualMousePosition.y, f);
|
||||
}
|
||||
|
||||
|
||||
public abstract float GetAxis(string name, bool raw);
|
||||
|
||||
public abstract bool GetButton(string name);
|
||||
public abstract bool GetButtonDown(string name);
|
||||
public abstract bool GetButtonUp(string name);
|
||||
|
||||
public abstract void SetButtonDown(string name);
|
||||
public abstract void SetButtonUp(string name);
|
||||
public abstract void SetAxisPositive(string name);
|
||||
public abstract void SetAxisNegative(string name);
|
||||
public abstract void SetAxisZero(string name);
|
||||
public abstract void SetAxis(string name, float value);
|
||||
public abstract Vector3 MousePosition();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f57aeb1b8dce3342bea5c28ac17db24
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
Reference in new issue
Block a user