Unity3d+moba+UGUI摇杆

1 功能描述

使用UGUI制作摇杆,摇杆的初始位置是可变的,当按下同时改变Bg位置和thumb位置,当松手时Bg,thumb返回初始原位。主要函数:ScreenPointToLocalPointInRectangle,Vector3.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.得到thumb的localPostion的移动偏移的标准向量作为摇杆的传出信息

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 运行效果

源码地址

https://download.csdn.net/download/luoyikun/10345848

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值