Unity3d游戏设计之游戏内自定义快捷键

第一次写博客,对Unity游戏开发也只是一个半吊子。俗话说:好记性不如烂笔头。所以想把自己学过用过的一些知识记录下来。

很多大型游戏都会有在游戏设置里自定义快捷键的功能。最近做项目,策划提出了这个要求,就开始着手操作了。

       UI部分是使用UGUI开发的,对这个比较熟。为了实现这个功能,原来是打算用InputField组件来做,但是后面有引发了一串操作上的Bug,看了UI源码,思考了好久也没解决。所以决定用最普通的Text组件来实现。下面直接上代码,然后慢慢解释。

using UnityEngine.EventSystems;
using UnityEngine.UI;
//需要继承的接口:IPointerEnterHandler,IPointerExitHandler,IPointerExitHandler,IPointerDownHandler
public class UserDefinedText : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerExitHandler,IPointerDownHandler
{
    [HideInInspector]
    public bool isSelecting = false;//是否被选中

    [HideInInspector]
    public Text inputField;//文本显示
    // Use this for initialization
    void Awake()
    {
        Init();
    }

    private void Init()
    {
        inputField = GetComponent();

        isSelecting = false;
    }

    string temp;

    void Update()
    {
        if (!isSelecting)
            return;
        
        if (Input.anyKeyDown)
        {
            KeyCode key = InputManager.Instance.GetKeyDownCode();
            if ((key == KeyCode.Mouse0 || key == KeyCode.Mouse1 || key == KeyCode.Mouse2) && currentPointedAt != inputField.gameObject)
            {
                //取消操作
                inputField.text = temp;
            }
            else
            {
                inputField.text = key.ToString();
                if (key == KeyCode.Mouse0 || key == KeyCode.Mouse1 || key == KeyCode.Mouse2)
                    return;
            }
            isSelecting = false;
        }
    }

    #region Events
    private GameObject currentPointedAt = null;

    public void OnPointerEnter(PointerEventData eventData)
    {
        currentPointedAt = eventData.pointerEnter.gameObject;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        currentPointedAt = null;
    }

    //鼠标抬起,完成修改或者开始修改
    public void OnPointerUp(PointerEventData eventData)
    {
        if (isSelecting)
        {
            //编辑结束 
            isSelecting = false;
        }
        else
        {
            //开始编辑
            isSelecting = true;
            temp = inputField.text;
            inputField.text = "......";
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {

    }
    #endregion
}

下面是一个捕获键值的管理器,继承了一个单例。

/// 
    /// 键值输入管理
    /// 
	public class InputManager:Singleton
	{
        public KeyCode cacheKey = KeyCode.None;

        public void Set_KeyCode(string name,KeyCode keycode)
        {

        }
        
        public KeyCode GetKeyDownCode()
		{
			if (Input.anyKeyDown)
			{
				foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
				{
					if (Input.GetKeyDown(keyCode))
					{
						//Debug.Log(keyCode.ToString());
						return keyCode;
					}
				}
			}
			return KeyCode.None;
		}
	}

补上在网上学的一个单例类代码:

/// 
    /// 单例类
    /// 
    public class Singleton : MonoBehaviour where T :Singleton
    {
        private static readonly object systemLock = new object();//线程锁
        private static T instance;
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (systemLock)//锁一下,避免多线程出问题
                    {
                        instance = FindObjectOfType();
                        if (instance == null)
                        {
                            GameObject obj = new GameObject("_" + typeof(T).Name);
                            DontDestroyOnLoad(obj);
                            instance = obj.AddComponent();
                        }
                        if (instance == null)
                            Debug.LogError("Failed to create instance of " + typeof(T).FullName + ".");
                    }
                }
                return instance;
            }
        }

        void OnApplicationQuit()
        {
            if (instance != null)
            {
                instance = null;
            }
        }

        public static T CreateInstance()
        {
            if (Instance != null) Instance.OnCreate();

            return Instance;
        }

        protected virtual void OnCreate() { }
    }

新建一个Text,挂在脚本,简单的自定义快捷键功能就实现了!


初次发帖,有不足的地方请大家原谅,多写指正,一起进步,谢谢大家!

你可能感兴趣的:(Unity3d使用)