码迷,mamicode.com
首页 > 其他好文 > 详细

UGUI实现摇杆(模仿太极熊猫)

时间:2015-07-30 00:27:39      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

核心代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public delegate void JoystickMoveDelegate(JoystickData data);

public class Joystick : MonoBehaviour {


    public GameObject joystickUI;                   //摇杆整体UI,方便Active
    public RectTransform joystickCenter;            //摇杆重心
    public RectTransform joystickBackground;        //摇杆背景


    private float halfScreenWidth;
    private float halfScreenHeight;
    private RectTransform joystickRect;
    private float radius;
    private Vector3 lastMousePosition;


    public static event JoystickMoveDelegate JoystickMoveEvent;



    // Use this for initialization
    void Start () {
        halfScreenWidth = Screen.width / 2;
        halfScreenHeight = Screen.height / 2;
        joystickRect = this.gameObject.GetComponent<RectTransform>();
        radius = 71;
        
    }
    
    // Update is called once per frame
    void Update () {
        JoystickController();
    }

    public void JoystickController() 
    {
        //鼠标一直按下
        if (Input.GetMouseButton(0))
        {
            float distance = Vector2.Distance(new Vector2(Input.mousePosition.x, Input.mousePosition.y), joystickRect.anchoredPosition);

            if (distance < radius)
            {
                //当距离小于半径就开始移动 摇杆重心
                joystickCenter.anchoredPosition = new Vector2(Input.mousePosition.x - joystickRect.anchoredPosition.x, Input.mousePosition.y - joystickRect.anchoredPosition.y);
            }
            else
            {
                //求圆上的一点:(目标点-原点) * 半径/原点到目标点的距离
                Vector2 endPosition = (new Vector2(Input.mousePosition.x, Input.mousePosition.y) - joystickRect.anchoredPosition) * radius / distance;
                joystickCenter.anchoredPosition = endPosition;
            }

            if (JoystickMoveEvent != null) 
            {

                JoystickMoveEvent(new JoystickData() { x = joystickCenter.anchoredPosition.x - joystickBackground.anchoredPosition.x, y = joystickCenter.anchoredPosition.y - joystickBackground.anchoredPosition.y });
            }

        }

        //鼠标按下
        if (Input.GetMouseButtonDown(0))
        {
            this.joystickUI.SetActive(true);

            //鼠标单击左边屏幕
            if (Input.mousePosition.x < halfScreenWidth)
            {
                joystickRect.anchoredPosition = Input.mousePosition;

            }

            //鼠标单击右边屏幕
            if (Input.mousePosition.x >= halfScreenWidth)
            {
                // Debug.Log("鼠标单击右边");
            }
        }


        //鼠标释放的时候
        if (Input.GetMouseButtonUp(0))
        {
            this.joystickUI.SetActive(false);
        }
    }
}

public class JoystickData
{
    public float x;
    public float y;

}

 

效果图:

技术分享

 

Unity5.1版本:

实例下载地址: http://yunpan.cn/cchdURtqCWXxv  访问密码 0aba

UGUI实现摇杆(模仿太极熊猫)

标签:

原文地址:http://www.cnblogs.com/plateFace/p/4687896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!