using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Rocker : MonoBehaviour {
public RectTransform RockerPoint; //触控点
public RectTransform BottomPlane; //触控盘父物体
public RectTransform BottomImage; //边框
public RectTransform RockerEffect; //特效显示(头朝上)
private float DragRadius; //拖动半径
private Vector2 RockerPointPosition; //触点坐标
private Vector2 initPosition; //按下时坐标
public static Vector2 RockerXY =new Vector2(0,0); //摇杆坐标数据等同于Input.GetAxis (0~1)
private float DragMin = 0.015f; //最小移动距离
private void Start()
{ //根据分辨率自适应摇杆区域大小
RockerPointPosition = new Vector2(Screen.height * 0.2f, Screen.height * 0.2f);
BottomPlane.position = RockerPointPosition;
BottomPlane.sizeDelta = new Vector2(Screen.width * 0.3f, Screen.width * 0.3f);
BottomImage.sizeDelta = new Vector2(Screen.width * 0.16f, Screen.width * 0.16f);
RockerEffect.sizeDelta = BottomImage.sizeDelta * 0.9f;
RockerPoint.sizeDelta = new Vector2(Screen.width * 0.03f, Screen.width * 0.03f);
RockerPoint.localPosition = new Vector2(0,0);
DragRadius = Screen.width * 0.05f; //拖动距离
DragMin = Screen.width * DragMin; //最小拖动半径
BottomImage.GetComponent<Image>().color = new Color(1, 1, 1, 0);
RockerEffect.GetComponent<Image>().color = new Color(1, 1, 1, 0);
}
/// <summary>
/// 淡出淡如特效
/// </summary>
/// <param name="start"></param>
/// <returns></returns>
IEnumerator EffectAnime(bool start)
{ //我这里设置10帧
int x1 = start ? 0 : 10;
int x2 = start ? 10 : 0;
do
{
if (x1 >= 6)
{ RockerPoint.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f); }
RockerEffect.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f);
BottomImage.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f);
x1 += start ? 1 : -1;
yield return new WaitForSeconds(0.03f);
}
while (start ? x1 <= x2 : x1 >= x2);
}
/// <summary>
/// 按下摇杆
/// </summary>
/// <param name="eventData">触发物体</param>
public void ClickDown()
{
StartCoroutine("EffectAnime",true);
initPosition = GetTouches(1);
if (initPosition.x > RockerPointPosition.x & initPosition.y > RockerPointPosition.y)
{ BottomPlane.position = initPosition; }
}
/// <summary>
/// 抬起摇杆
/// </summary>
public void ClickUP()
{
StartCoroutine("EffectAnime", false);
RockerXY = new Vector2(0, 0);
RockerPoint.localPosition = new Vector2(0, 0);
BottomPlane.position = RockerPointPosition;
TouchUp(1);
}
//拖动摇杆
public void RockerDrag()
{
Vector2 dir = GameToolScript.tool.GetTouches(1) - initPosition;
if (Mathf.Abs(dir.x) > DragMin | Mathf.Abs(dir.y) > DragMin)
{
RockerEffect.gameObject.SetActive(true);
RockerPoint.localPosition = dir.normalized * DragRadius;
RockerXY = RockerPoint.localPosition.normalized;
Vector3 v = (RockerPoint.transform.position - BottomPlane.transform.position).normalized;
RockerEffect.up= v;
}
else
{
RockerEffect.gameObject.SetActive(false);
RockerPoint.localPosition = dir;
RockerXY = new Vector2(0, 0);
}
}
private List<int> touchesList = new List<int> { };
/// <summary>
/// 查找是哪个触点
/// <para>适用于多点触控自动判定触点, 推荐放置于静态变量中</para>
/// </summary>
/// <param name="touch">触摸的物体</param>
/// <returns></returns>
public Vector2 GetTouches(int touch)
{
int index = touchesList.IndexOf(touch);
if (index > -1)
{
return Input.touches[index].position;
}
touchesList.Add(touch);
return Input.touches[Input.touchCount - 1].position;
}
public void TouchUp(int touch)
{
touchesList.Remove(touch);
}
}