Unity3D自制虚拟摇杆和一个方法控制多个按钮

教学视频地址:https://v.qq.com/x/page/n03982ncfm2.html

以下为控制代码

BtnsControl.cs

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

public class MyBtnPro
{
    public int id;
    public GameObject obj;
}

public class BtnsControl : MonoBehaviour {
    public static BtnsControl Instance;

    public delegate void BtnsClickDelegate(MyBtnPro bp);
    public event BtnsClickDelegate BtnsClick;

    Button[] btns;

    /// 
    /// Awake is called when the script instance is being loaded.
    /// 
    void Awake()
    {
        btns = transform.GetComponentsInChildren

JoystickControl.cs

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections.Generic;

public class JoystickControl : MonoBehaviour 
{
    public static JoystickControl Instance;

    private Transform fill;
    private Transform fillR;
    private Vector3 startPos;
    private float r;

    public delegate void JoystickDragDelegate(Vector3 coor);
    public event JoystickDragDelegate JoystickDrag;

    public delegate void JoystickUpActonDelegate();
    public event JoystickUpActonDelegate JoystickUp;

    /// 
    /// Awake is called when the script instance is being loaded.
    /// 
    void Awake()
    {
        fill = transform.FindChild("Fill");
        fillR = transform.FindChild("FillR");

        Instance = this;
    }

    /// 
    /// Start is called on the frame when a script is enabled just before
    /// any of the Update methods is called the first time.
    /// 
    void Start()
    {
        AddTriggersListener(fill.gameObject, EventTriggerType.Drag, (o) => OnJoystickDragAction());
        AddTriggersListener(fill.gameObject, EventTriggerType.PointerUp, (o) => OnJoystickUpActon());

        startPos = fill.position;
        r = Vector3.Distance(fillR.position, startPos) * 2;
    }

    /// 
    /// Update is called every frame, if the MonoBehaviour is enabled.
    /// 
    void Update()
    {
        if (JoystickDrag != null)
        {
            if (Vector3.Distance(fill.position, startPos) > 0.1f)
            {
                Vector3 coor = (Input.mousePosition - startPos).normalized;
                JoystickDrag(coor);
            }
        }
    }

    private void OnJoystickDragAction()
    {
        if (Vector3.Distance(Input.mousePosition, startPos) < r)
        {
            fill.position = Input.mousePosition;
        }
        else
        {
            Vector3 dir = (Input.mousePosition - startPos).normalized;
            fill.position =  startPos + dir * r;
        }

    }

    private void OnJoystickUpActon()
    {
        if (JoystickUp != null)
        {
            JoystickUp();
        }

        fill.position = startPos;
    }

    /// 
    ///添加EventTrigger的监听事件
    ///
    private void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction action)
    {
        EventTrigger trigger;
        if (obj.GetComponent() != null)
        {
            trigger = obj.GetComponent();
        }
        else
        {
            trigger = obj.AddComponent();
        }
        if (trigger.triggers.Count == 0)
        {
            trigger.triggers = new List();
        }
        UnityAction callback = new UnityAction(action);
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = eventID;
        entry.callback.AddListener(callback);
        trigger.triggers.Add(entry);
    }



}

你可能感兴趣的:(unity,AR)