171 lines
4.2 KiB
C#
171 lines
4.2 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.EventSystems;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class JoyStickHandler : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// ҡ<><D2A1>ˮƽƫ<C6BD><C6AB><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static float js_x;
|
|||
|
|
/// <summary>
|
|||
|
|
/// ҡ<>˴<EFBFBD>ֱƫ<D6B1><C6AB><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static float js_y;
|
|||
|
|
|
|||
|
|
// <20><>ǰģʽ
|
|||
|
|
public static Mode CurrentMode;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ҡ<><D2A1>ģʽ
|
|||
|
|
/// </summary>
|
|||
|
|
public enum Mode { None, Up, Down, Left, Right };
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ͼƬRectTransform
|
|||
|
|
/// </summary>
|
|||
|
|
[Tooltip("<22><><EFBFBD><EFBFBD>ͼƬRectTransform")]
|
|||
|
|
public RectTransform background;
|
|||
|
|
/// <summary>
|
|||
|
|
/// Handle<6C><65>RectTransform
|
|||
|
|
/// </summary>
|
|||
|
|
[Tooltip("Handle<6C><65>RectTransform")]
|
|||
|
|
public RectTransform handle;
|
|||
|
|
/// <summary>
|
|||
|
|
/// ҡ<><D2A1><EFBFBD>ٶ<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[Tooltip("ҡ<><D2A1><EFBFBD>ٶ<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>")]
|
|||
|
|
public float js_Speed = 1f;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[Tooltip("ҡ<><D2A1><EFBFBD><EFBFBD><EFBFBD>ڵĻ<DAB5><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
|
|||
|
|
private Canvas canvas;
|
|||
|
|
|
|||
|
|
// <20>뾶
|
|||
|
|
float radius;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Vector2 up, down, left, right;
|
|||
|
|
|
|||
|
|
public bool TestOnEditor;
|
|||
|
|
|
|||
|
|
Color handleColor;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
if (!TestOnEditor && !H5Controller.IsMobileBroswer())
|
|||
|
|
{
|
|||
|
|
Destroy(gameObject);
|
|||
|
|
}
|
|||
|
|
canvas = transform.parent.parent.GetComponent<Canvas>();
|
|||
|
|
|
|||
|
|
CurrentMode = Mode.None;
|
|||
|
|
radius = background.rect.width / 2.0f;
|
|||
|
|
up = new Vector2(0, radius);
|
|||
|
|
down = new Vector2(0, -radius);
|
|||
|
|
left = new Vector2(-radius, 0);
|
|||
|
|
right = new Vector2(radius, 0);
|
|||
|
|
|
|||
|
|
handleColor = handle.GetComponent<Image>().color;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region
|
|||
|
|
//void Update()
|
|||
|
|
//{
|
|||
|
|
// // MessageSend();
|
|||
|
|
//}
|
|||
|
|
void MessageSend()
|
|||
|
|
{
|
|||
|
|
switch (CurrentMode)
|
|||
|
|
{
|
|||
|
|
case Mode.None:
|
|||
|
|
break;
|
|||
|
|
case Mode.Up:
|
|||
|
|
break;
|
|||
|
|
case Mode.Left:
|
|||
|
|
break;
|
|||
|
|
case Mode.Down:
|
|||
|
|
break;
|
|||
|
|
case Mode.Right:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>갴<EFBFBD><EAB0B4>
|
|||
|
|
public void OnPointerDown(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (BaseController.CanControl)
|
|||
|
|
{
|
|||
|
|
OnDrag(eventData);
|
|||
|
|
|
|||
|
|
handle.GetComponent<Image>().color = new Color(150, 150, 150, 255);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD>
|
|||
|
|
public void OnPointerUp(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (BaseController.CanControl)
|
|||
|
|
{
|
|||
|
|
js_x = 0;
|
|||
|
|
js_y = 0;
|
|||
|
|
|
|||
|
|
handle.anchoredPosition = Vector2.zero;
|
|||
|
|
handle.GetComponent<Image>().color = handleColor;
|
|||
|
|
CurrentMode = Mode.None;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
Vector2 to;
|
|||
|
|
public void OnDrag(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (BaseController.CanControl)
|
|||
|
|
{
|
|||
|
|
to = (eventData.position - (Vector2)background.position) / canvas.scaleFactor;
|
|||
|
|
if (to.magnitude <= radius)
|
|||
|
|
{
|
|||
|
|
handle.anchoredPosition = to;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
to = to / (to.magnitude / radius);
|
|||
|
|
handle.anchoredPosition = to;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CalcDis();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
void CalcDis()
|
|||
|
|
{
|
|||
|
|
float dis = 0f;
|
|||
|
|
if (Vector2.Distance(up, handle.anchoredPosition) < dis || dis == 0)
|
|||
|
|
{
|
|||
|
|
dis = Vector2.Distance(up, handle.anchoredPosition);
|
|||
|
|
CurrentMode = Mode.Up;
|
|||
|
|
}
|
|||
|
|
if (Vector2.Distance(down, handle.anchoredPosition) < dis)
|
|||
|
|
{
|
|||
|
|
dis = Vector2.Distance(down, handle.anchoredPosition);
|
|||
|
|
CurrentMode = Mode.Down;
|
|||
|
|
}
|
|||
|
|
if (Vector2.Distance(left, handle.anchoredPosition) < dis)
|
|||
|
|
{
|
|||
|
|
dis = Vector2.Distance(left, handle.anchoredPosition);
|
|||
|
|
CurrentMode = Mode.Left;
|
|||
|
|
}
|
|||
|
|
if (Vector2.Distance(right, handle.anchoredPosition) < dis)
|
|||
|
|
{
|
|||
|
|
dis = Vector2.Distance(right, handle.anchoredPosition);
|
|||
|
|
CurrentMode = Mode.Right;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
js_x = (radius - Vector2.Distance(right, handle.anchoredPosition)) / radius * js_Speed;
|
|||
|
|
js_y = (radius - Vector2.Distance(up, handle.anchoredPosition)) / radius * js_Speed;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|