229 lines
6.6 KiB
C#
229 lines
6.6 KiB
C#
using System.Collections;
|
|
using Invector.vCharacterController;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Serialization;
|
|
using UnityStandardAssets.CrossPlatformInput;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
|
{
|
|
public enum AxisOption
|
|
{
|
|
// Options for which axes to use
|
|
Both, // Use both
|
|
OnlyHorizontal, // Only horizontal
|
|
OnlyVertical // Only vertical
|
|
}
|
|
|
|
public int MovementRange = 100;
|
|
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
|
|
|
|
public string
|
|
horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
|
|
|
|
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
|
|
|
|
//public string pressButtonName;
|
|
Vector3 m_StartPos;
|
|
private Vector2 m_PointerDownPos;
|
|
bool m_UseX; // Toggle for using the x axis
|
|
bool m_UseY; // Toggle for using the Y axis
|
|
|
|
CrossPlatformInputManager.VirtualAxis
|
|
m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
|
|
|
|
CrossPlatformInputManager.VirtualAxis
|
|
m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
|
|
|
|
IEnumerator Start()
|
|
{
|
|
//m_StartPos = transform.localPosition;
|
|
m_StartPos = ((RectTransform)transform).anchoredPosition;
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
CreateVirtualAxes();
|
|
}
|
|
|
|
void UpdateVirtualAxes(Vector3 value)
|
|
{
|
|
var delta = m_StartPos - value;
|
|
delta.y = -delta.y;
|
|
delta /= MovementRange;
|
|
if (m_UseX)
|
|
{
|
|
m_HorizontalVirtualAxis.Update(-delta.x);
|
|
}
|
|
|
|
if (m_UseY)
|
|
{
|
|
m_VerticalVirtualAxis.Update(delta.y);
|
|
}
|
|
}
|
|
|
|
void CreateVirtualAxes()
|
|
{
|
|
// set axes to use
|
|
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
|
|
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
|
|
|
|
// create new axes based on axes to use
|
|
if (m_UseX)
|
|
{
|
|
if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
|
|
{
|
|
m_HorizontalVirtualAxis = CrossPlatformInputManager.VirtualAxisReference(horizontalAxisName);
|
|
}
|
|
else
|
|
{
|
|
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
|
|
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
if (m_UseY)
|
|
{
|
|
if (CrossPlatformInputManager.AxisExists(verticalAxisName))
|
|
{
|
|
m_VerticalVirtualAxis = CrossPlatformInputManager.VirtualAxisReference(verticalAxisName);
|
|
}
|
|
else
|
|
{
|
|
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
|
|
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
/*
|
|
Vector3 newPos = Vector3.zero;
|
|
|
|
if (m_UseX)
|
|
{
|
|
int delta = (int) (eventData.position.x - m_StartPos.x);
|
|
//delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
|
|
newPos.x = delta;
|
|
}
|
|
|
|
if (m_UseY)
|
|
{
|
|
int delta = (int) (eventData.position.y - m_StartPos.y);
|
|
//delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
|
|
newPos.y = delta;
|
|
}
|
|
|
|
// change to clamp on a circular area instead of a square area
|
|
transform.localPosition =
|
|
Vector3.ClampMagnitude(
|
|
transform.parent.InverseTransformPoint(new Vector3(newPos.x, newPos.y, newPos.z)), MovementRange) +
|
|
m_StartPos;
|
|
UpdateVirtualAxes(transform.localPosition);
|
|
*/
|
|
//////////
|
|
if (eventData == null)
|
|
throw new System.ArgumentNullException(nameof(eventData));
|
|
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var position);
|
|
var delta = position - m_PointerDownPos;
|
|
|
|
delta = Vector2.ClampMagnitude(delta, MovementRange);
|
|
((RectTransform)transform).anchoredPosition = m_StartPos + (Vector3)delta;
|
|
|
|
var newPos = new Vector2(delta.x / MovementRange, delta.y / MovementRange);
|
|
UpdateVirtualAxes(newPos);
|
|
//SendValueToControl(newPos);
|
|
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData data)
|
|
{
|
|
//transform.localPosition = m_StartPos;
|
|
((RectTransform)transform).anchoredPosition = m_StartPos;
|
|
UpdateVirtualAxes(m_StartPos);
|
|
|
|
//if (!string.IsNullOrEmpty(pressButtonName))
|
|
//{
|
|
// CrossPlatformInputManager.SetButtonUp(pressButtonName);
|
|
//}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (eventData == null)
|
|
throw new System.ArgumentNullException(nameof(eventData));
|
|
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out m_PointerDownPos);
|
|
//if (!string.IsNullOrEmpty(pressButtonName))
|
|
//{
|
|
// CrossPlatformInputManager.SetButtonDown(pressButtonName);
|
|
//}
|
|
}
|
|
}
|
|
/*
|
|
public class Joystick : OnScreenControl, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
|
{
|
|
[FormerlySerializedAs("movementRange")]
|
|
[SerializeField]
|
|
private float m_MovementRange = 50;
|
|
|
|
[InputControl(layout = "Vector2")]
|
|
[SerializeField]
|
|
private string m_ControlPath;
|
|
|
|
private Vector3 m_StartPos;
|
|
private Vector2 m_PointerDownPos;
|
|
|
|
protected override string controlPathInternal
|
|
{
|
|
get => m_ControlPath;
|
|
set => m_ControlPath = value;
|
|
}
|
|
|
|
public float movementRange
|
|
{
|
|
get => m_MovementRange;
|
|
set => m_MovementRange = value;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
m_StartPos = ((RectTransform)transform).anchoredPosition;
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (eventData == null)
|
|
throw new System.ArgumentNullException(nameof(eventData));
|
|
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out m_PointerDownPos);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (eventData == null)
|
|
throw new System.ArgumentNullException(nameof(eventData));
|
|
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var position);
|
|
var delta = position - m_PointerDownPos;
|
|
|
|
delta = Vector2.ClampMagnitude(delta, movementRange);
|
|
((RectTransform)transform).anchoredPosition = m_StartPos + (Vector3)delta;
|
|
|
|
var newPos = new Vector2(delta.x / movementRange, delta.y / movementRange);
|
|
SendValueToControl(newPos);
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
((RectTransform)transform).anchoredPosition = m_StartPos;
|
|
SendValueToControl(Vector2.zero);
|
|
}
|
|
}
|
|
*/
|
|
} |