Unity+FairyGUI)rpg方向控制的方向盘控制/摇杆

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using FairyGUI;
using Package1;

namespace Assets.Scripts
{
    class Wheel : UIBase
    {
        private float rad = 200f; //活动半径
        private Vector2 center = new Vector2(200, 200);//中心

        private Vector2 resultVector2;//结果

        private UI_WheelCenter center_ui;

        override public void init()
        {            
            center_ui = ((UI_Wheel)this.ui).m_center_ui;
            center_ui.draggable = true;

            center_ui.onDragStart.Add(dragStartHandler);
            center_ui.onDragEnd.Add(dragEndHandler);
            center_ui.onDragMove.Add(dragMoveHandler);
        }

        private void dragEndHandler() {
            center_ui.x = center.x;
            center_ui.y = center.y;
            MoveManager.getInstance().moveAngle = 0;
        }

        private void dragStartHandler() {
            
        }

        private void dragMoveHandler(EventContext _event) {
           
            Vector2 cur = new Vector2(center_ui.x, center_ui.y);

            float dis = Vector2.Distance(center, cur);
            float ang = Mathf.Atan((cur.y - center.y) / (cur.x - center.x));
            if (ang < 0)
            {
                if (cur.x < center.x)
                {
                    ang += Mathf.PI;
                }
                else
                {
                    ang += 2 * Mathf.PI;
                }
            }
            else
            {
                if (cur.x < center.x)
                {
                    ang += Mathf.PI;
                }
            }
            //Debug.Log("罗盘角度=" + Mathf.Rad2Deg * ang + ",坐标=" + cur.x +"," +cur.y);
            MoveManager.getInstance().moveAngle = ang;

            if (dis > rad*2)
            {                
                center_ui.x = center.x + rad * Mathf.Cos(ang);
                center_ui.y = center.y + rad * Mathf.Sin(ang);
            }
            else {
                Vector2 touchVec = new Vector2(_event.inputEvent.x, _event.inputEvent.y);
                Vector2 newVec = this.ui.GlobalToLocal(touchVec);
                center_ui.x = newVec.x;
                center_ui.y = newVec.y;
            }

            MoveManager.getInstance().breakLock();
        }
    }
}
Unity+FairyGUI)rpg方向控制的方向盘控制/摇杆_第1张图片

你可能感兴趣的:(Unity3d,FairyGUI)