教学视频地址:
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;
/// <summary>
/// Awake is called when the script instance is being loaded.
/// </summary>
void Awake()
{
btns = transform.GetComponentsInChildren<Button>();
Instance = this;
}
// Use this for initialization
void Start () {
for (int i = 0; i < btns.Length; i++)
{
MyBtnPro bp = new MyBtnPro();
bp.id = i + 1;
bp.obj = btns[i].gameObject;
btns[i].onClick.AddListener(() => BtnsClickAction(bp));
}
}
// Update is called once per frame
void Update () {
}
private void BtnsClickAction(MyBtnPro bp)
{
Debug.Log(bp.id);
if(BtnsClick != null)
{
BtnsClick(bp);
}
}
}
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;
/// <summary>
/// Awake is called when the script instance is being loaded.
/// </summary>
void Awake()
{
fill = transform.FindChild("Fill");
fillR = transform.FindChild("FillR");
Instance = this;
}
/// <summary>
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
/// </summary>
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;
}
/// <summary>
/// Update is called every frame, if the MonoBehaviour is enabled.
/// </summary>
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;
}
/// <summary>
///添加EventTrigger的监听事件
///</summary>
private void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction<BaseEventData> action)
{
EventTrigger trigger;
if (obj.GetComponent<EventTrigger>() != null)
{
trigger = obj.GetComponent<EventTrigger>();
}
else
{
trigger = obj.AddComponent<EventTrigger>();
}
if (trigger.triggers.Count == 0)
{
trigger.triggers = new List<EventTrigger.Entry>();
}
UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = eventID;
entry.callback.AddListener(callback);
trigger.triggers.Add(entry);
}
}
版权声明:本文为u014581509原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。