清理项目目录,引入 InputModule 做多平台输入适配
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0303479082e55a64a86a307e56b8538f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f728913a8b734241882f86edcd29284
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,150 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class Joystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
||||
{
|
||||
public float Horizontal { get { return (snapX) ? SnapFloat(input.x, AxisOptions.Horizontal) : input.x; } }
|
||||
public float Vertical { get { return (snapY) ? SnapFloat(input.y, AxisOptions.Vertical) : input.y; } }
|
||||
public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }
|
||||
|
||||
public float HandleRange
|
||||
{
|
||||
get { return handleRange; }
|
||||
set { handleRange = Mathf.Abs(value); }
|
||||
}
|
||||
|
||||
public float DeadZone
|
||||
{
|
||||
get { return deadZone; }
|
||||
set { deadZone = Mathf.Abs(value); }
|
||||
}
|
||||
|
||||
public AxisOptions AxisOptions { get { return axisOptions; } set { axisOptions = value; } }
|
||||
public bool SnapX { get { return snapX; } set { snapX = value; } }
|
||||
public bool SnapY { get { return snapY; } set { snapY = value; } }
|
||||
|
||||
[SerializeField] private float handleRange = 1;
|
||||
[SerializeField] private float deadZone = 0;
|
||||
[SerializeField] private AxisOptions axisOptions = AxisOptions.Both;
|
||||
[SerializeField] private bool snapX = false;
|
||||
[SerializeField] private bool snapY = false;
|
||||
|
||||
[SerializeField] protected RectTransform background = null;
|
||||
[SerializeField] private RectTransform handle = null;
|
||||
private RectTransform baseRect = null;
|
||||
|
||||
private Canvas canvas;
|
||||
private Camera cam;
|
||||
|
||||
private Vector2 input = Vector2.zero;
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
HandleRange = handleRange;
|
||||
DeadZone = deadZone;
|
||||
baseRect = GetComponent<RectTransform>();
|
||||
canvas = GetComponentInParent<Canvas>();
|
||||
if (canvas == null)
|
||||
Debug.LogError("The Joystick is not placed inside a canvas");
|
||||
|
||||
Vector2 center = new Vector2(0.5f, 0.5f);
|
||||
background.pivot = center;
|
||||
handle.anchorMin = center;
|
||||
handle.anchorMax = center;
|
||||
handle.pivot = center;
|
||||
handle.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
public virtual void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
OnDrag(eventData);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
cam = null;
|
||||
if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
|
||||
cam = canvas.worldCamera;
|
||||
|
||||
Vector2 position = RectTransformUtility.WorldToScreenPoint(cam, background.position);
|
||||
Vector2 radius = background.sizeDelta / 2;
|
||||
input = (eventData.position - position) / (radius * canvas.scaleFactor);
|
||||
FormatInput();
|
||||
HandleInput(input.magnitude, input.normalized, radius, cam);
|
||||
handle.anchoredPosition = input * radius * handleRange;
|
||||
}
|
||||
|
||||
protected virtual void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam)
|
||||
{
|
||||
if (magnitude > deadZone)
|
||||
{
|
||||
if (magnitude > 1)
|
||||
input = normalised;
|
||||
}
|
||||
else
|
||||
input = Vector2.zero;
|
||||
}
|
||||
|
||||
private void FormatInput()
|
||||
{
|
||||
if (axisOptions == AxisOptions.Horizontal)
|
||||
input = new Vector2(input.x, 0f);
|
||||
else if (axisOptions == AxisOptions.Vertical)
|
||||
input = new Vector2(0f, input.y);
|
||||
}
|
||||
|
||||
private float SnapFloat(float value, AxisOptions snapAxis)
|
||||
{
|
||||
if (value == 0)
|
||||
return value;
|
||||
|
||||
if (axisOptions == AxisOptions.Both)
|
||||
{
|
||||
float angle = Vector2.Angle(input, Vector2.up);
|
||||
if (snapAxis == AxisOptions.Horizontal)
|
||||
{
|
||||
if (angle < 22.5f || angle > 157.5f)
|
||||
return 0;
|
||||
else
|
||||
return (value > 0) ? 1 : -1;
|
||||
}
|
||||
else if (snapAxis == AxisOptions.Vertical)
|
||||
{
|
||||
if (angle > 67.5f && angle < 112.5f)
|
||||
return 0;
|
||||
else
|
||||
return (value > 0) ? 1 : -1;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value > 0)
|
||||
return 1;
|
||||
if (value < 0)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public virtual void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
input = Vector2.zero;
|
||||
handle.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
protected Vector2 ScreenPointToAnchoredPosition(Vector2 screenPosition)
|
||||
{
|
||||
Vector2 localPoint = Vector2.zero;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(baseRect, screenPosition, cam, out localPoint))
|
||||
{
|
||||
Vector2 pivotOffset = baseRect.pivot * baseRect.sizeDelta;
|
||||
return localPoint - (background.anchorMax * baseRect.sizeDelta) + pivotOffset;
|
||||
}
|
||||
return Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
public enum AxisOptions { Both, Horizontal, Vertical }
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c3be397dcbcf5a42b4e4f0a104fd909
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e40aeae89a478845b0d5a5d879a0699
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(DynamicJoystick))]
|
||||
public class DynamicJoystickEditor : JoystickEditor
|
||||
{
|
||||
private SerializedProperty moveThreshold;
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
moveThreshold = serializedObject.FindProperty("moveThreshold");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
if (background != null)
|
||||
{
|
||||
RectTransform backgroundRect = (RectTransform)background.objectReferenceValue;
|
||||
backgroundRect.anchorMax = Vector2.zero;
|
||||
backgroundRect.anchorMin = Vector2.zero;
|
||||
backgroundRect.pivot = center;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DrawValues()
|
||||
{
|
||||
base.DrawValues();
|
||||
EditorGUILayout.PropertyField(moveThreshold, new GUIContent("Move Threshold", "The distance away from the center input has to be before the joystick begins to move."));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5e9a9da0786d1140aa0a03c549cfcd4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(FloatingJoystick))]
|
||||
public class FloatingJoystickEditor : JoystickEditor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
if (background != null)
|
||||
{
|
||||
RectTransform backgroundRect = (RectTransform)background.objectReferenceValue;
|
||||
backgroundRect.anchorMax = Vector2.zero;
|
||||
backgroundRect.anchorMin = Vector2.zero;
|
||||
backgroundRect.pivot = center;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4eeb7024a2680041ad9126751afc3a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(Joystick), true)]
|
||||
public class JoystickEditor : Editor
|
||||
{
|
||||
private SerializedProperty handleRange;
|
||||
private SerializedProperty deadZone;
|
||||
private SerializedProperty axisOptions;
|
||||
private SerializedProperty snapX;
|
||||
private SerializedProperty snapY;
|
||||
protected SerializedProperty background;
|
||||
private SerializedProperty handle;
|
||||
|
||||
protected Vector2 center = new Vector2(0.5f, 0.5f);
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
handleRange = serializedObject.FindProperty("handleRange");
|
||||
deadZone = serializedObject.FindProperty("deadZone");
|
||||
axisOptions = serializedObject.FindProperty("axisOptions");
|
||||
snapX = serializedObject.FindProperty("snapX");
|
||||
snapY = serializedObject.FindProperty("snapY");
|
||||
background = serializedObject.FindProperty("background");
|
||||
handle = serializedObject.FindProperty("handle");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
|
||||
DrawValues();
|
||||
EditorGUILayout.Space();
|
||||
DrawComponents();
|
||||
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
if (handle != null)
|
||||
{
|
||||
RectTransform handleRect = (RectTransform)handle.objectReferenceValue;
|
||||
handleRect.anchorMax = center;
|
||||
handleRect.anchorMin = center;
|
||||
handleRect.pivot = center;
|
||||
handleRect.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void DrawValues()
|
||||
{
|
||||
EditorGUILayout.PropertyField(handleRange, new GUIContent("Handle Range", "The distance the visual handle can move from the center of the joystick."));
|
||||
EditorGUILayout.PropertyField(deadZone, new GUIContent("Dead Zone", "The distance away from the center input has to be before registering."));
|
||||
EditorGUILayout.PropertyField(axisOptions, new GUIContent("Axis Options", "Which axes the joystick uses."));
|
||||
EditorGUILayout.PropertyField(snapX, new GUIContent("Snap X", "Snap the horizontal input to a whole value."));
|
||||
EditorGUILayout.PropertyField(snapY, new GUIContent("Snap Y", "Snap the vertical input to a whole value."));
|
||||
}
|
||||
|
||||
protected virtual void DrawComponents()
|
||||
{
|
||||
EditorGUILayout.ObjectField(background, new GUIContent("Background", "The background's RectTransform component."));
|
||||
EditorGUILayout.ObjectField(handle, new GUIContent("Handle", "The handle's RectTransform component."));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 179099b45ef8aff4d9827b6a1edbc5c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(VariableJoystick))]
|
||||
public class VariableJoystickEditor : JoystickEditor
|
||||
{
|
||||
private SerializedProperty moveThreshold;
|
||||
private SerializedProperty joystickType;
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
moveThreshold = serializedObject.FindProperty("moveThreshold");
|
||||
joystickType = serializedObject.FindProperty("joystickType");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
if (background != null)
|
||||
{
|
||||
RectTransform backgroundRect = (RectTransform)background.objectReferenceValue;
|
||||
backgroundRect.pivot = center;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DrawValues()
|
||||
{
|
||||
base.DrawValues();
|
||||
EditorGUILayout.PropertyField(moveThreshold, new GUIContent("Move Threshold", "The distance away from the center input has to be before the joystick begins to move."));
|
||||
EditorGUILayout.PropertyField(joystickType, new GUIContent("Joystick Type", "The type of joystick the variable joystick is current using."));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5730e2f409c715545902697544b5abe5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b269c4bf776f2144b9e0b0ba53bac4a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class DynamicJoystick : Joystick
|
||||
{
|
||||
public float MoveThreshold { get { return moveThreshold; } set { moveThreshold = Mathf.Abs(value); } }
|
||||
|
||||
[SerializeField] private float moveThreshold = 1;
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
MoveThreshold = moveThreshold;
|
||||
base.Start();
|
||||
background.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position);
|
||||
background.gameObject.SetActive(true);
|
||||
base.OnPointerDown(eventData);
|
||||
}
|
||||
|
||||
public override void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
background.gameObject.SetActive(false);
|
||||
base.OnPointerUp(eventData);
|
||||
}
|
||||
|
||||
protected override void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam)
|
||||
{
|
||||
if (magnitude > moveThreshold)
|
||||
{
|
||||
Vector2 difference = normalised * (magnitude - moveThreshold) * radius;
|
||||
background.anchoredPosition += difference;
|
||||
}
|
||||
base.HandleInput(magnitude, normalised, radius, cam);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0af7ddc3839ef6e4185afc9ba94e4093
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FixedJoystick : Joystick
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a48c2994a21fff46a4cd3b1e9936b64
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class FloatingJoystick : Joystick
|
||||
{
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
background.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position);
|
||||
background.gameObject.SetActive(true);
|
||||
base.OnPointerDown(eventData);
|
||||
}
|
||||
|
||||
public override void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
background.gameObject.SetActive(false);
|
||||
base.OnPointerUp(eventData);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d60c1fa6c2eae8847a7759d1a045924b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class VariableJoystick : Joystick
|
||||
{
|
||||
public float MoveThreshold { get { return moveThreshold; } set { moveThreshold = Mathf.Abs(value); } }
|
||||
|
||||
[SerializeField] private float moveThreshold = 1;
|
||||
[SerializeField] private JoystickType joystickType = JoystickType.Fixed;
|
||||
|
||||
private Vector2 fixedPosition = Vector2.zero;
|
||||
|
||||
public void SetMode(JoystickType joystickType)
|
||||
{
|
||||
this.joystickType = joystickType;
|
||||
if(joystickType == JoystickType.Fixed)
|
||||
{
|
||||
background.anchoredPosition = fixedPosition;
|
||||
background.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
background.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
fixedPosition = background.anchoredPosition;
|
||||
SetMode(joystickType);
|
||||
}
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
if(joystickType != JoystickType.Fixed)
|
||||
{
|
||||
background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position);
|
||||
background.gameObject.SetActive(true);
|
||||
}
|
||||
base.OnPointerDown(eventData);
|
||||
}
|
||||
|
||||
public override void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
if(joystickType != JoystickType.Fixed)
|
||||
background.gameObject.SetActive(false);
|
||||
|
||||
base.OnPointerUp(eventData);
|
||||
}
|
||||
|
||||
protected override void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam)
|
||||
{
|
||||
if (joystickType == JoystickType.Dynamic && magnitude > moveThreshold)
|
||||
{
|
||||
Vector2 difference = normalised * (magnitude - moveThreshold) * radius;
|
||||
background.anchoredPosition += difference;
|
||||
}
|
||||
base.HandleInput(magnitude, normalised, radius, cam);
|
||||
}
|
||||
}
|
||||
|
||||
public enum JoystickType { Fixed, Floating, Dynamic }
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96c26c3c35bf71b418f3fe9e83fbb3eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"reference": "GUID:73a59a40d41e9ce48b3e0515aeec4217"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48e11eb6d40eeda4ebecdb55503bc9f7
|
||||
AssemblyDefinitionReferenceImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,73 @@
|
||||
using SepCore.InputModule;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace SepCore.InputModule.Runtime.VirtualInput
|
||||
{
|
||||
public sealed class VirtualButtonBridge : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
[SerializeField] private InputActionId _actionId;
|
||||
[SerializeField] private InputContextId _contextId = InputContextId.GameplayExplore;
|
||||
[SerializeField] private bool _injectDeviceKind = true;
|
||||
[SerializeField] private InputModuleComponent _inputModule;
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
Inject(InputCommandPhase.Started);
|
||||
Inject(InputCommandPhase.Performed);
|
||||
|
||||
if (_injectDeviceKind)
|
||||
{
|
||||
TryForceDeviceKind();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
Inject(InputCommandPhase.Canceled);
|
||||
}
|
||||
|
||||
private void Inject(InputCommandPhase phase)
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float scalar = phase == InputCommandPhase.Performed ? 1f : 0f;
|
||||
var command = new InputCommand(
|
||||
_actionId,
|
||||
_contextId,
|
||||
phase,
|
||||
InputDeviceKind.Touch,
|
||||
Vector2.zero,
|
||||
scalar,
|
||||
Time.time);
|
||||
|
||||
inputModule.InjectCommand(command);
|
||||
}
|
||||
|
||||
private void TryForceDeviceKind()
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
inputModule.ForceDeviceKind(InputDeviceKind.Touch);
|
||||
}
|
||||
|
||||
private InputModuleComponent GetInputModule()
|
||||
{
|
||||
if (_inputModule != null)
|
||||
{
|
||||
return _inputModule;
|
||||
}
|
||||
|
||||
_inputModule = FindObjectOfType<InputModuleComponent>();
|
||||
return _inputModule;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b6f36e10530cd44790afe8f90d83475
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,96 @@
|
||||
using SepCore.InputModule;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.InputModule.Runtime.VirtualInput
|
||||
{
|
||||
[RequireComponent(typeof(Joystick))]
|
||||
public sealed class VirtualJoystickBridge : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputActionId _actionId = InputActionId.Move;
|
||||
[SerializeField] private InputContextId _contextId = InputContextId.GameplayExplore;
|
||||
[SerializeField] private bool _injectDeviceKind = true;
|
||||
[SerializeField] private InputModuleComponent _inputModule;
|
||||
|
||||
private Joystick _joystick;
|
||||
private Vector2 _previousDirection;
|
||||
private bool _wasPressed;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_joystick = GetComponent<Joystick>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Vector2 direction = _joystick.Direction;
|
||||
bool isPressed = direction.sqrMagnitude > 0f;
|
||||
|
||||
if (!_wasPressed && isPressed)
|
||||
{
|
||||
Inject(InputCommandPhase.Started, direction);
|
||||
Inject(InputCommandPhase.Performed, direction);
|
||||
}
|
||||
else if (_wasPressed && isPressed)
|
||||
{
|
||||
if ((direction - _previousDirection).sqrMagnitude > 0.0001f)
|
||||
{
|
||||
Inject(InputCommandPhase.Performed, direction);
|
||||
}
|
||||
}
|
||||
else if (_wasPressed && !isPressed)
|
||||
{
|
||||
Inject(InputCommandPhase.Canceled, Vector2.zero);
|
||||
}
|
||||
|
||||
if (isPressed && _injectDeviceKind)
|
||||
{
|
||||
TryForceDeviceKind();
|
||||
}
|
||||
|
||||
_previousDirection = direction;
|
||||
_wasPressed = isPressed;
|
||||
}
|
||||
|
||||
private void Inject(InputCommandPhase phase, Vector2 direction)
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var command = new InputCommand(
|
||||
_actionId,
|
||||
_contextId,
|
||||
phase,
|
||||
InputDeviceKind.Touch,
|
||||
direction,
|
||||
direction.magnitude,
|
||||
Time.time);
|
||||
|
||||
inputModule.InjectCommand(command);
|
||||
}
|
||||
|
||||
private void TryForceDeviceKind()
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
inputModule.ForceDeviceKind(InputDeviceKind.Touch);
|
||||
}
|
||||
|
||||
private InputModuleComponent GetInputModule()
|
||||
{
|
||||
if (_inputModule != null)
|
||||
{
|
||||
return _inputModule;
|
||||
}
|
||||
|
||||
_inputModule = FindObjectOfType<InputModuleComponent>();
|
||||
return _inputModule;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 997d14c0348d4394a89e50969e0e357b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user