Unity3D 对普通手柄的支持



Unity3D 对普通手柄 (非 x360手柄) 的支持


using UnityEngine;

using System.Collections;

public class JoystickController : MonoBehaviour {
    
    /// <summary>
    /// This is a joystick detect demo/project. Made by project Team Unity~ from the Entertainment Technology Center at Carnegie Mellon.
    /// The purpose for this demo/project is to understand what is the mapping for you joystick.
    /// </summary>
    
    private string currentButton;
    private string currentAxis;
    private float  axisInput;
    
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update ()
    {
        getAxis();
        getButton();
        
    
    }
    
    /// <summary>
    /// Get Axis data of the joysick
    /// </summary>
    void getAxis()
    {
        if(Input.GetAxisRaw("X axis")> 0.3|| Input.GetAxisRaw("X axis") < -0.3)
        {
            currentAxis = "X axis";
            axisInput = Input.GetAxisRaw("X axis");
        }
        
        if(Input.GetAxisRaw("Y axis")> 0.3|| Input.GetAxisRaw("Y axis") < -0.3)
        {
            currentAxis = "Y axis";
            axisInput = Input.GetAxisRaw("Y axis");
        }
        
        if(Input.GetAxisRaw("3rd axis")> 0.3|| Input.GetAxisRaw("3rd axis") < -0.3)
        {
            currentAxis = "3rd axis";
            axisInput = Input.GetAxisRaw("3rd axis");
        }
        
        if(Input.GetAxisRaw("4th axis")> 0.3|| Input.GetAxisRaw("4th axis") < -0.3)
        {
            currentAxis = "4th axis";
            axisInput = Input.GetAxisRaw("4th axis");
        }
        
        if(Input.GetAxisRaw("5th axis")> 0.3|| Input.GetAxisRaw("5th axis") < -0.3)
        {
            currentAxis = "5th axis";
            axisInput = Input.GetAxisRaw("5th axis");
        }
        
        if(Input.GetAxisRaw("6th axis")> 0.3|| Input.GetAxisRaw("6th axis") < -0.3)
        {
            currentAxis = "6th axis";
            axisInput = Input.GetAxisRaw("6th axis");
        }
        
        if(Input.GetAxisRaw("7th axis")> 0.3|| Input.GetAxisRaw("7th axis") < -0.3)
        {
            currentAxis = "7th axis";
            axisInput = Input.GetAxisRaw("7th axis");
        }
        
        if(Input.GetAxisRaw("8th axis") > 0.3|| Input.GetAxisRaw("8th axis") < -0.3)
        {
            currentAxis = "8th axis";
            axisInput = Input.GetAxisRaw("8th axis");
        }
    }
    
    /// <summary>
    /// get the button data of the joystick
    /// </summary>
    void getButton()
    {
        if(Input.GetButton("joystick button 0"))
            currentButton = "joystick button 0";
           
        if(Input.GetButton("joystick button 1"))
            currentButton = "joystick button 1";
           
        if(Input.GetButton("joystick button 2"))
            currentButton = "joystick button 2";
           
        if(Input.GetButton("joystick button 3"))
            currentButton = "joystick button 3";
           
        if(Input.GetButton("joystick button 4"))
            currentButton = "joystick button 4";
           
        if(Input.GetButton("joystick button 5"))
            currentButton = "joystick button 5";
           
        if(Input.GetButton("joystick button 6"))
            currentButton = "joystick button 6";
           
        if(Input.GetButton("joystick button 7"))
            currentButton = "joystick button 7";
           
        if(Input.GetButton("joystick button 8"))
            currentButton = "joystick button 8";
           
        if(Input.GetButton("joystick button 9"))
            currentButton = "joystick button 9";
           
        if(Input.GetButton("joystick button 10"))
            currentButton = "joystick button 10";
           
        if(Input.GetButton("joystick button 11"))
            currentButton = "joystick button 11";
           
        if(Input.GetButton("joystick button 12"))
            currentButton = "joystick button 12";
           
        if(Input.GetButton("joystick button 13"))
            currentButton = "joystick button 13";
           
        if(Input.GetButton("joystick button 14"))
            currentButton = "joystick button 14";
        
        if(Input.GetButton("joystick button 15"))
            currentButton = "joystick button 15";
        
        if(Input.GetButton("joystick button 16"))
            currentButton = "joystick button 16";
        
        if(Input.GetButton("joystick button 17"))
            currentButton = "joystick button 17";
        
        if(Input.GetButton("joystick button 18"))
            currentButton = "joystick button 18";
        
        if(Input.GetButton("joystick button 19"))
            currentButton = "joystick button 19";       
    }
    
    /// <summary>
    /// show the data onGUI
    /// </summary>
    void OnGUI()
    {
        GUI.TextArea(new Rect(400, 100, 250, 50), "Current Button : " + currentButton);
        GUI.TextArea(new Rect(400, 200, 250, 50), "Current Axis : " + currentAxis);
        GUI.TextArea(new Rect(400, 300, 250, 50), "Axis Value : " +  axisInput);
    }
}



这是全部代码 当然你需要去 input manager 设置下

deme   http://download.csdn.net/detail/nicepainkiller/9611967

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nicepainkiller

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值