129 lines
3.5 KiB
C#
129 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class joystick : MonoBehaviour
|
|
{
|
|
public bool touchDown;
|
|
public bool autoConfigMaxDist;
|
|
public float maxDist;
|
|
public Vector2 input;
|
|
public Vector2 deltaPos;
|
|
public Vector2 startPos;
|
|
public Vector2 endPos;
|
|
public RectTransform joyBG;
|
|
public RectTransform joyStick;
|
|
|
|
public static Vector2 Input => instance.input;
|
|
public static joystick instance;
|
|
void Awake(){
|
|
instance = this;
|
|
}
|
|
void Update()
|
|
{/*
|
|
if (autoConfigMaxDist)
|
|
{
|
|
maxDist = joyBG.sizeDelta.x / 2;
|
|
}
|
|
if (touchDown)
|
|
{
|
|
if (Input.touchCount > 0)
|
|
{
|
|
Touch[] tlist = Input.touches;
|
|
int tId = 0;
|
|
for (int i = 0; i < tlist.Length; i++)
|
|
{
|
|
if (tlist[tId].position.x < Screen.width / 2)
|
|
{
|
|
tId = i;
|
|
continue;
|
|
}
|
|
}
|
|
if(tlist[tId].position.x > Screen.width / 2) { return; }
|
|
endPos = tlist[tId].position;
|
|
}
|
|
else if (Input.mousePosition.x < Screen.width / 2)
|
|
{
|
|
endPos = Input.mousePosition;
|
|
}
|
|
deltaPos = endPos - startPos;
|
|
|
|
deltaPos = new Vector2(Mathf.Clamp(deltaPos.x, -maxDist, maxDist), Mathf.Clamp(deltaPos.y, -maxDist, +maxDist));
|
|
|
|
}
|
|
else
|
|
{
|
|
// deltaPos = Vector2.zero;
|
|
}
|
|
Vector2 _input = new Vector2(deltaPos.x / maxDist, deltaPos.y / maxDist);
|
|
|
|
if(_input.magnitude < 0.1f)
|
|
{
|
|
return;
|
|
}
|
|
input = _input;
|
|
|
|
input = (input.magnitude > 1.0f) ? input.normalized : input;
|
|
|
|
joyStick.localPosition = new Vector2(input.x * (joyBG.sizeDelta.x / 2f), input.y * (joyBG.sizeDelta.y / 2f));
|
|
*/
|
|
}
|
|
|
|
public void onDrag(BaseEventData pointer)
|
|
{
|
|
PointerEventData ped = pointer as PointerEventData;
|
|
endPos = ped.position;
|
|
deltaPos = endPos - startPos;
|
|
|
|
deltaPos = new Vector2(Mathf.Clamp(deltaPos.x, -maxDist, maxDist), Mathf.Clamp(deltaPos.y, -maxDist, +maxDist));
|
|
|
|
Vector2 _input = new Vector2(deltaPos.x / maxDist, deltaPos.y / maxDist);
|
|
|
|
if (_input.magnitude < 0.1f)
|
|
{
|
|
return;
|
|
}
|
|
input = _input;
|
|
|
|
input = (input.magnitude > 1.0f) ? input.normalized : input;
|
|
|
|
joyStick.localPosition = new Vector2(input.x * (joyBG.sizeDelta.x / 2f), input.y * (joyBG.sizeDelta.y / 2f));
|
|
}
|
|
|
|
public void pointerDown(BaseEventData pointer)
|
|
{
|
|
PointerEventData ped = pointer as PointerEventData;
|
|
startJoy(ped);
|
|
}
|
|
|
|
public void pointerUp()
|
|
{
|
|
endJoy();
|
|
}
|
|
|
|
|
|
void startJoy(PointerEventData pointer)
|
|
{
|
|
startPos = pointer.position;
|
|
joyBG.gameObject.SetActive(true);
|
|
joyStick.gameObject.SetActive(true);
|
|
joyStick.localPosition = Vector2.zero;
|
|
joyBG.position = startPos;
|
|
|
|
touchDown = true;
|
|
|
|
|
|
}
|
|
|
|
void endJoy()
|
|
{
|
|
// joyBG.gameObject.SetActive(false);
|
|
// joyStick.gameObject.SetActive(false);
|
|
input = Vector2.zero;
|
|
touchDown = false;
|
|
joyStick.localPosition = Vector2.zero;
|
|
|
|
}
|
|
}
|