123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System;
- public class UGUIJoyStick : UIBase, IDragHandler, IEndDragHandler,IBeginDragHandler
- {
- protected float mRadius;
- public RectTransform content;
- public UGUIJoyStickHandle onJoyStick;
- public bool canMove = true;
- //当前分辨率,与UImanager上的标准分辨率之间的换算比
- public float conversionX;
- public float conversionY;
- void Start()
- {
- //计算摇杆块的半径
- mRadius = ((transform as RectTransform).sizeDelta.x-content.sizeDelta.x) * 0.5f;
- Vector2 referenceResolution = UIManager.UIManagerGo.GetComponent<CanvasScaler>().referenceResolution;
- conversionX = referenceResolution.x / Screen.width ;
- conversionY = referenceResolution.y/Screen.height ;
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- canMove = true;
- }
- Vector2 centerDelta = new Vector2(0, 0);
- public void OnDrag(PointerEventData eventData)
- {
- centerDelta.x = eventData.delta.x * conversionX;
- centerDelta.y = eventData.delta.y * conversionY;
- Vector3 contentPostion = content.anchoredPosition + centerDelta;
- if (contentPostion.magnitude > mRadius)
- {
- contentPostion = contentPostion.normalized * mRadius;
- }
- content.anchoredPosition3D = contentPostion;
- //onJoyStick(GetDir());
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- canMove = false;
- content.anchoredPosition3D = Vector3.zero;
- //onJoyStick(Vector3.zero);
- }
- public Vector3 GetDir()
- {
- Vector3 dir = new Vector3(content.anchoredPosition3D.x, 0, content.anchoredPosition3D.y);
- dir /= mRadius;
- return dir;
- }
- void Update()
- {
- if (onJoyStick != null)
- {
- try
- {
- if (GetDir() != Vector3.zero && canMove)
- {
- onJoyStick(GetDir());
- }
- }
- catch (Exception e)
- {
- Debug.LogError(e.ToString());
- }
- }
- }
- public void ReHomePos()
- {
- canMove = false;
-
- content.anchoredPosition3D = Vector3.zero;
- onJoyStick(Vector3.zero);
- }
- }
- public delegate void UGUIJoyStickHandle(Vector3 dir);
|