开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   yhy137084

按键按下检测管理脚本 热度 5

个人分类: Unity3d | 2015-11-22 20:20
1 个评论 | 阅读 316 | 收藏 | 举报

看到其他人博客写的
让Input能够正确工作
正确的写法是:
using UnityEngine;
public class Test : MonoBehaviour
{
    bool action = false;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            action = true;
        }
    }
    void FixedUpdate()
    {
        if (action)
        {
            //Action 
            action = false;
        }
    }
}


所以写了个管理脚本

using UnityEngine;
using System.Collections;
public class keyTest : MonoBehaviour
{
    void Start()
    {
        KeyCheckRefresh.GetInstance.AddKey(KeyCode.P, PDown);
        KeyCheckRefresh.GetInstance.AddKey(KeyCode.P, P2Down);
        KeyCheckRefresh.GetInstance.AddKey(KeyCode.J, JDown);
    }
    void PDown()
    {
        Debug.Log("Key P Down");
    }
    void P2Down()
    {
        Debug.Log("Key P2 Down");
    }
    void JDown()
    {
        Debug.Log("Key J Down");
    }
}
实现代码
using UnityEngine;
using System.Collections.Generic;
public class KeyCheckRefresh : MonoBehaviour
{
    private static KeyCheckRefresh _instance;
    public static KeyCheckRefresh GetInstance
    {
        get
        {
            if (_instance == null)
            {
                GameObject go = new GameObject("KeyDownCheck");
                _instance = go.AddComponent<KeyCheckRefresh>();
            }
            return _instance;
        }
    }
    public delegate void KeyCodeDownMessage();
    public class KDI
    {
        public List<KeyCodeDownMessage> message_list;
        public bool action;
        public KeyCode keyCode;
        public KDI(KeyCode keyCode, KeyCodeDownMessage message)
        {
            message_list = new List<KeyCodeDownMessage>();
            message_list.Add(message);
            action = false;
            this.keyCode = keyCode;
        }
    }
    List<KDI> list = new List<KDI>();
    void Update()
    {
        DictUpdate();
    }
    void FixedUpdate()
    {
        DictFixedUpdate();
    }
    void DictUpdate()
    {
        foreach (KDI kdi in list)
        {
            if (Input.GetKeyDown(kdi.keyCode))//只会在按下的瞬间返回真,而剩余的时间即使我们长按,它也不会继续返回
            {
                kdi.action = true;
                break;
            }
        }
    }
    void DictFixedUpdate()
    {
        foreach (KDI kdi in list)
        {
            if (kdi.action)
            {
                foreach (KeyCodeDownMessage message in kdi.message_list)
                {
                    message();
                }
                kdi.action = false;
            }
        }
    }
    public void AddKey(KeyCode kc, KeyCodeDownMessage ckdm)
    {
        if (kc == null || ckdm == null)
            return;
        //检测list里是否有kc,有就在kc的list添加ckdm,没有就新建KDI
        bool ishave = false;
        foreach (KDI kdi in list)
        {
            if (kdi.keyCode == kc)
            {
                ishave = true;
                break;
            }
        }
        if (ishave)
        {
            foreach (KDI kdi in list)
            {
                if (kdi.keyCode == kc)
                {
                    //检测list是否包含ckdm,不包含则添加ckdm
                    bool ishave2 = false;
                    int index = kdi.message_list.IndexOf(ckdm);
                    ishave2 = index == -1 ? false : true;
                    if (ishave2 == false)
                    {
                        kdi.message_list.Add(ckdm);
                        break;
                    }
                }
            }
        }
        else
        {
            KDI kdi = new KDI(kc, ckdm);
            list.Add(kdi);
        }
    }
}
4 0

作者的其他最新博客

评论 (1 个评论)

回复 你是八嘎君 2015-11-25 17:18
一直想看看键盘管理类的写法。先收藏看看~~~

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部