Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Unity】CrossPlatformInputを実際に組み込んでみる
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UnityStandardAssets.CrossPlatformInput
{
[RequireComponent( typeof(RectTransform) )]
public class TouchPadCustom : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public enum AxisOption
{
Both,
OnlyHorizontal,
OnlyVertical,
}
public AxisOption axesToUse = AxisOption.Both;
public string horizontalAxisName = "Horizontal";
public string verticalAxisName = "Vertical";
[SerializeField]
private Image m_iconImage = null;
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis = null;
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis = null;
bool m_Dragging = false;
int m_Id = INVALID_TOUCH_ID;
private Vector3 m_Center = Vector3.zero;
private Vector2 m_MovementRange = Vector2.zero;
private const int INVALID_TOUCH_ID = -1;
private Vector3 TouchPosition
{
get
{
#if !UNITY_EDITOR
return Input.touches[ m_Id ].position;
#else
return Input.mousePosition;
#endif
}
}
private Vector3 IconImagePosition
{
set
{
if( m_iconImage == null )
{
return;
}
var rect = GetComponent<RectTransform>();
value.x = Mathf.Clamp( value.x, m_Center.x - m_MovementRange.x, m_Center.x + m_MovementRange.x );
value.y = Mathf.Clamp( value.y, m_Center.y - m_MovementRange.y, m_Center.y + m_MovementRange.y );
value.z = 0.0f;
m_iconImage.transform.position = value;
}
}
void OnEnable()
{
CreateVirtualAxes();
}
void OnDisable()
{
if( CrossPlatformInputManager.AxisExists( horizontalAxisName ) )
CrossPlatformInputManager.UnRegisterVirtualAxis( horizontalAxisName );
if( CrossPlatformInputManager.AxisExists( verticalAxisName ) )
CrossPlatformInputManager.UnRegisterVirtualAxis( verticalAxisName );
DisableValue();
}
void Start()
{
m_Center = transform.position;
{
var rect = GetComponent<RectTransform>().rect;
m_MovementRange.x = rect.width * 0.5f;
m_MovementRange.y = rect.height * 0.5f;
}
}
void Update()
{
if (!m_Dragging)
{
return;
}
if( m_Id == INVALID_TOUCH_ID )
{
return;
}
Vector3 axisValue = TouchPosition - m_Center;
UpdateVirtualAxes( axisValue );
IconImagePosition = TouchPosition;
}
public void OnPointerDown( PointerEventData data )
{
m_Dragging = true;
m_Id = data.pointerId;
}
public void OnPointerUp( PointerEventData data )
{
DisableValue();
}
void CreateVirtualAxes()
{
bool useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
bool useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
m_HorizontalVirtualAxis = null;
m_VerticalVirtualAxis = null;
if ( useX )
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if ( useY )
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
void UpdateVirtualAxes(Vector3 value)
{
if ( m_HorizontalVirtualAxis != null)
{
float axisValue = value.x / m_MovementRange.x;
axisValue = Mathf.Clamp( axisValue, -1.0f, 1.0f );
m_HorizontalVirtualAxis.Update( axisValue );
}
if ( m_VerticalVirtualAxis != null)
{
float axisValue = value.y / m_MovementRange.y;
axisValue = Mathf.Clamp( axisValue, -1.0f, 1.0f );
m_VerticalVirtualAxis.Update( axisValue );
}
}
void DisableValue()
{
m_Dragging = false;
m_Id = INVALID_TOUCH_ID;
IconImagePosition = m_Center;
UpdateVirtualAxes( Vector3.zero );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.