Unity3d+moba+UGUI摇杆

来源:互联网 发布:淘宝加盟的骗局揭秘 编辑:程序博客网 时间:2024/04/30 17:53

1 功能描述

使用UGUI制作摇杆,摇杆的初始位置是可变的,当按下同时改变Bg位置和thumb位置,当松手时Bgthumb返回初始原位。主要函数:ScreenPointToLocalPointInRectangleVector3.Distance

2 详细设计


Bg为摇杆的大圆背景,thumb为中心小圆,JoyCollider为可触发碰撞区(可设置大小,只有按在此区域才能触发摇杆的操作)

2.1 主要类成员

public GameObject m_objJoyCollider;//碰撞区域    public GameObject m_objThumb;//中心小圆    public GameObject m_objBg;//背景大圆    public Canvas can;//主要为获取画布上的摄像机    private float m_limitBg;//大圆移动限制    private float m_limitThumb;//小圆移动限制    public GameObject m_cube;    public bool m_bMoving;//可用作防在摇杆区域多点触控    public Vector3 m_dir;//方向供外部调用摇杆的方向public Touch m_touch;//保存按下时触摸点信息

2.2 当按在触摸区域时

void onDownCollider(GameObject obj)    {        m_bMoving = true;        RectTransform rect = transform as RectTransform;        Vector3 newPos;        if (Application.platform == UnityEngine.RuntimePlatform.WindowsEditor)        {            newPos = uiPosGet(Input.mousePosition, rect);        }        else        {            m_touch = Input.touches[Input.touches.Length - 1];            newPos = uiPosGet(m_touch.position, rect);        }        float dis = Vector3.Distance(newPos, new Vector3(0,0,0));//鼠标与中心距离        if (dis <= m_limitBg)        {            newPos = newPos*dis/m_limitBg;        }        else        {            Vector3 normalPos = newPos.normalized;            newPos = normalPos*m_limitBg*1.5f;        }        m_objBg.transform.localPosition = newPos;        StartCoroutine("yieldStickMove");        }

1.得到触摸坐标转化为相对ui父节点的localPostion

2.设置Bg的位置,注意位置有限制,不能让这个大圆的部分超过屏幕边缘。

3.开启协程

2.3 手指移动时

private IEnumerator yieldStickMove()    {        while (true)        {            RectTransform rect = m_objBg.GetComponent<RectTransform>();            Vector3 realTouchPos = Vector3.zero;            if (Application.platform == UnityEngine.RuntimePlatform.WindowsEditor)            {                realTouchPos = uiPosGet(Input.mousePosition, rect);            }            else            {                for (int i = 0; i < Input.touchCount; i++ )                {                    if ( m_touch.fingerId == Input.touches[i].fingerId)                    {                        realTouchPos = uiPosGet(Input.touches[i].position, rect);                    }                }            }                   float dis = Vector3.Distance(new Vector3(0,0,0), realTouchPos);            if (dis <= m_limitThumb)            {                Vector3 normalPos = realTouchPos.normalized;                m_objThumb.transform.localPosition = realTouchPos;            }            else            {                Vector3 normalPos = realTouchPos.normalized;                realTouchPos = normalPos * m_limitThumb;                m_objThumb.transform.localPosition = realTouchPos;            }            m_dir = m_objThumb.transform.localPosition.normalized;            Vector3 cubePos = m_cube.transform.position;            m_cube.transform.LookAt(new Vector3(cubePos.x + m_dir.x, cubePos.y, cubePos.z + m_dir.y));            m_cube.transform.Translate(Vector3.forward * Time.deltaTime * 70);            yield return null;        }}

1.thumb坐标相对bg坐标超过限制时与没超过时处理

2.得到thumblocalPostion的移动偏移的标准向量作为摇杆的传出信息

2.3当手指松开始时

    void onUpCollider(GameObject obj)    {        StopCoroutine("yieldStickMove");        m_objBg.transform.localPosition = new Vector3(0,0,0);        m_objThumb.transform.localPosition = new Vector3(0,0,0);        m_bMoving = false;}

Ui回到初始位置。

3 运行效果




0 0
原创粉丝点击