カスタムボタン

次のUIを構築できます
ここに画像の説明を挿入

按键控制器类

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum Key 
{
    
    
    ForWord,
    BackWord,
    Right,
    Left,
    //不存在的键
    None
}
public class KeyController : MonoBehaviour
{
    
    
    Button[] ButtonArray;
    Text[] TextArray;
    bool IsWattingForInput = false;
    Button button;
    //单例
    public static KeyController Instance;
    private void Awake()
    {
    
    
        if (Instance == null)
        {
    
    
            Instance = this;
        }
        else 
        {
    
    
            Destroy(this);
        }
    }
  
    //初始化
    private void Start()
    {
    
    
        ButtonArray = GetComponentsInChildren<Button>();
        TextArray= new Text[ButtonArray.Length];
        int i = 0;
        foreach (var item in ButtonArray)
        {
    
    
            TextArray[i] = item.gameObject.GetComponentInChildren<Text>();
            item.onClick.AddListener(()=>ChangeKeyBoard(item));
            i++;
        }
        Refresh();
    }
    private void Update()
    {
    
    
        if (IsWattingForInput)
        {
    
    
            ChangeKeyBoard(button);
        }
    }
    public static Dictionary<Key, KeyCode> KeyDict = new Dictionary<Key, KeyCode>() 
    {
    
    
        {
    
     Key.ForWord,KeyCode.W },{
    
    Key.BackWord,KeyCode.S},{
    
    Key.Right,KeyCode.D},{
    
    Key.Left,KeyCode.A }
    };
    /// <summary>
    /// 改变键值
    /// </summary>
    /// <param name="key">键值类型</param>
    /// <param name="code">KeyCode</param>
    public  void ChangeKey(Key key,KeyCode code) 
    {
    
    
        if (KeyDict.ContainsKey(key))
        {
    
    
            KeyDict[key] =code;
        }
        else 
        {
    
    
            KeyDict.Add(key,code);
        }
    }
    //刷新界面
    public void Refresh() 
    {
    
    
        int i = 0;
        foreach (var item in KeyDict.Values)
        {
    
    
            TextArray[i].text = item.ToString();
            i++;
        }
    }
    //自定义按键,修改UI显示
    public void ChangeKeyBoard(Button button) 
    {
    
    
        this.button = button;
        IsWattingForInput = true;
        //拿到值
        string values= button.GetComponentInChildren<Text>().text;
        //查找键
        Key key=Key.None;
        foreach (var item in KeyDict)
        {
    
    
            if (item.Value.ToString()==values)
            {
    
    
                key = item.Key;
            }
        }
        if (!Input.inputString.Equals(""))
        {
    
    
           ChangeKey(key,(KeyCode)System.Enum.Parse(typeof(KeyCode),Input.inputString.ToUpper()));
           IsWattingForInput = false;
           Refresh();
        }
           
    }

}

测试类

using UnityEngine;
using static KeyController;
public class Player2 : MonoBehaviour
{
    
    
    public float Speed;
    public float RotateSpeed;
    bool IsActive = false;
    private void Update()
    {
    
    
        Move();
        ShowUI();
    }
    public void Move() 
    {
    
    
        if (Input.GetKey(KeyDict[Key.ForWord]))
        {
    
    
            Debug.Log("向前"+ KeyDict[Key.ForWord]);
        }
        else if (Input.GetKey(KeyDict[Key.BackWord]))
        {
    
    
            Debug.Log("向后" + KeyDict[Key.BackWord]);
        }
        else if (Input.GetKey(KeyDict[Key.Right]))
        {
    
    
            Debug.Log("向右" + KeyDict[Key.Right]);
        }
        else if (Input.GetKey(KeyDict[Key.Left]))
        {
    
    
            Debug.Log("向左" + KeyDict[Key.Left]);
        }
        else {
    
     return; }
    }
    public void ShowUI() 
    {
    
    
        if (Input.GetKeyDown(KeyCode.Escape))
        {
    
    
            KeyController.Instance.gameObject.SetActive(IsActive);
            IsActive = !IsActive;
        }
    }
}


おすすめ

転載: blog.csdn.net/qq_51978873/article/details/122636392