[Unity] Input功能扩展:手游触控

在使用Unity开发游戏时,免不了使用Input.GetAxis、Input.GetButtonDown等方法来检测玩家是否要执行前进后退、左右上下、攻击等操作。

但是在开发手游时这样不太好使了,比如不能通过Input.GetAxis("horizontal")来判断是否进行了左右移动。

 

为此我写了InputEx.cs,实现了对一些基本按键的兼容,抛砖引玉吧。

先看代码:

#if UNITY_STANDALONE // || UNITY_EDITOR
#else
#define USE_INPUT_EX  // 定义使用InputEx
#endif

using UnityEngine;
#if USE_INPUT_EX
using UnityEngine.UI;
#endif
using System.Collections;

/// <summary>
/// 扩展输入工具。 方便跨平台使用
/// <remarks>作者: YangYxd</remarks>
/// </summary>
public class InputEx : MonoBehaviour {
    /// <summary>
    /// 触控工具层,在移动设备上才显示
    /// </summary>
    public GameObject EasyTools;

    #if USE_INPUT_EX
    private static float lastTimeH = 0;
    private static float lastTimeV = 0;
    private static bool isLeft = false;
    private static bool isRight = false;
    private static bool isUp = false;
    private static bool isDown = false;
    private static bool isJump = false;

    private static bool isFire1 = false;
    private static bool isFire2 = false;
    private static bool isFire3 = false;
    private static bool isFire4 = false;
    private static bool isFire5 = false;
    #endif

    void Awake () {
        #if USE_INPUT_EX
        if (EasyTools != null)
            EasyTools.SetActive(true);
        #else
        if (EasyTools != null)
            EasyTools.SetActive(false);
        #endif
    }

    public void SetLeft(bool v) {
        #if USE_INPUT_EX
        isLeft = v;
        lastTimeH = Time.time;
        #endif
    }

    public void SetRight(bool v) {
        #if USE_INPUT_EX
        lastTimeH = Time.time;
        isRight = v;
        #endif
    }

    public void SetUp(bool v) {
        #if USE_INPUT_EX
        isUp = v;
        lastTimeV = Time.time;
        #endif
    }

    public void SetDown(bool v) {
        #if USE_INPUT_EX
        isDown = v;
        lastTimeV = Time.time;
        #endif
    }

    public void SetJump(bool v) {
        #if USE_INPUT_EX
        isJump = v;
        #endif
    }

    public void SetFire1(bool v) {
        SetFire (1, v);
    }
    public void SetFire2(bool v) {
        SetFire (2, v);
    }
    public void SetFire3(bool v) {
        SetFire (3, v);
    }
    public void SetFire4(bool v) {
        SetFire (4, v);
    }
    public void SetFire5(bool v) {
        SetFire (5, v);
    }

    public void SetFire(int index, bool v) {
        #if USE_INPUT_EX
        switch (index) {
        case 1:
        isFire1 = v; break;
        case 2:
        isFire2 = v; break;
        case 3:
        isFire3 = v; break;
        case 4:
        isFire4 = v; break;
        case 5:
        isFire5 = v; break;
        }
        #endif
    }

    private static float GetAxisValue(float lastTime) {
        float v = (Time.time - lastTimeH) * 1.2f;
        if (v > 1) v = 1;
        return v;
    }

    public static float GetAxis(string axisName) {
        #if USE_INPUT_EX
        switch (axisName.ToLower()) {
        case "horizontal":
        case "mouse x":
            if (isLeft) return -1 * GetAxisValue(lastTimeH);
            else if (isRight) return GetAxisValue(lastTimeH);
            else return 0;
        case "vertical":
        case "mouse y":
            if (isUp) return -1 * GetAxisValue(lastTimeV);
            else if (isDown) return GetAxisValue(lastTimeV);
            else return 0;
        case "jump":
        case "space":
        if (isJump) return 1; else return 0; 
        case "fire1":
        if (isFire1) return 1; else return 0;
        case "fire2":
        if (isFire2) return 1; else return 0;
        case "fire3":
        if (isFire3) return 1; else return 0;
        case "fire4":
        if (isFire4) return 1; else return 0;
        case "fire5":
        if (isFire5) return 1; else return 0;
        default:
        return Input.GetAxis (axisName);
        }
        #else
        return Input.GetAxis (axisName);
        #endif
    }

    public static bool GetButtonDown(string buttonName) {
        #if USE_INPUT_EX
        switch (buttonName.ToLower()) {
        case "jump":
                if (isJump) {
                    isJump = false;
                    return true;
                } else return false;
        case "space":
                if (isJump) {
                    isJump = false;
                    return true;
                } else return false;
        case "fire1":
                if (isFire1) {
                    isFire1 = false;
                    return true;
                } else return false;
        case "fire2":
                if (isFire2) {
                    isFire2 = false;
                    return true;
                } else return false;
        case "fire3":
                if (isFire3) { 
                    isFire3 = false;
                    return true;
                } else return false;
        case "fire4":
                if (isFire4) {
                    isFire4 = false;
                    return true;
                } else return false;
        case "fire5":
                if (isFire5) {
                    isFire5 = false;
                    return true;
                } else return false;
        default:
            return Input.GetButtonDown (buttonName);
        }
        #else
        return Input.GetButtonDown (buttonName);
        #endif
    }
}

 

使用方法很简单,只需要在使用 Input.xxx 的地方, 将 Input 替换为  InputEx 即可。

原理很简单, 在电脑或编辑模式时, InputEx 将直接调用 Input 的对应功能。 在其它的平台, 则通过判断自身存放的各种状态来返回数据。

我们需要做的是, 在游戏上添加手游的控制层, 将InputEx挂载到这个游戏对象上, 再添加相关功能按钮, 比如 左移、右移、跳跃、攻击等, 然后给按钮添加事件。

 

举个例子吧:

 

比如我们需要按下 ButtonJump 实现跳跃功能, 那么在 ButtonJump 的 OnClick 中设置事件函数为  InputEx.SetJump(true) 就可以了。

如果要左移, 则在按钮的 OnDown 事件中调用 InputEx.SetLeft(true),在 OnUp 事件中调用 InputEx.SetLeft(false) 即可。

 

比如,我为豆子先生的游戏添加相关的触控按钮:

 

 

设置好相关事件就可以正常运行了。 

 

InputEx.cs 可能会在将来不断完善, 你可以在  https://github.com/yangyxd/YxdUGUI   获取最新的代码。

 

原创内容,转载注明出处。(YangYxd)

 

posted @ 2016-03-23 13:00  我爱我家喵喵  阅读(848)  评论(0编辑  收藏  举报