Rewired 使用笔记

目录

在Editor窗口中初步设置的步骤:

脚本调用:

【导出常量在脚本中使用】

【UGUI】

【为特定Joystick Element显示对应的‘符号’(ABXY/╳〇□△)】 

【检测最后一次输入用的控制器】

【检测控制器连接/断开】

【震动功能】

使用建议

优化建议【原文】

图像说明


在Editor窗口中初步设置的步骤:

脚本调用:

// The Rewired Player
Player player = ReInput.players.GetPlayer(0);//单人游戏,id=0
// Get the System Player
Player systemPlayer = ReInput.players.GetSystemPlayer();

//获取Axis
float x = player.GetAxis("Move Horizontal"); // get input by Action Name or Action Id

//button按下
if (player.GetButtonDown("Fire")) //参数使用Action Name
{//....}


 

【导出常量在脚本中使用】

在InputManager上打开RewiredEditor窗口,Tools选项页。点击【Export】按钮。

调用:bool fire = player.GetButton(RewiredConsts.Action.Fire);

【UGUI】

使用Rewired Event System替换EventSystem(或使用RewiredStandaloneInputModule代替StandaloneInputModule)。

为UI创建一组Action,包含以下四个Action。然后设置好手柄\键盘的映射。

注意这四个Action的名字与RewiredStandaloneInputModule上的四个轴名字要保持一致。

Unknown Controller mapping

为特定Joystick Element显示对应的‘符号’(ABXY/╳〇□△)】 

Joystick Element 可以理解为控制器上的按键。要显示特定Joystick的Element ‘符号’,拢共分三步:

  1. 创建JoystickElement图片。
  2. 创建一个JoystickElement符号查找表。
    (To save time, you can download a CSV file of the current controller element identifiers here.)
    (You can download a CSV file of the current Controllers here.)
  3.  在屏幕上显示该‘符号’。

第三部的实现取决于你的需求。基本的步骤如下:

  1. Player获取最后使用的Controller ,可以使用Player.controllers.GetLastActiveController方法。
  2. 用 Controller.type 属性判断使用的 Controller 类型.
  3. 如果是Joystick, 可以用 Joystick.hardwareTypeGuid 属性判断它是否是能被识别的类型. (每个已识别的Controller 的GUID类型,在这个 CSV file里可以找到)
  4. 检查要查询的Action在该Controller有没有element映射,可以使用下列方法获取ActionElementMap
  5. 如果element已映射,可通过 ActionElementMap.elementIdentifierId属性和 Joystick.hardwareTypeGuid 属性从查找表中查找字形。
  6. 可选项:有关映射的更多信息,可以在ActionElementMap中找到,例如Axis Range,Axis Contribution,Type或其他有用的信息(如果您需要更具体的字形以获取更详细的信息)。

检测最后一次输入用的控制器

(由此可以实现的功能:根据用户使用的是 鼠标/键盘/手柄 来切换屏幕上的字形)

You can also subscribe to events instead of polling:

【检测控制器连接/断开】

using UnityEngine;
using Rewired;

public MyClass : MonoBehaviour {

    void Awake() {
        // Subscribe to events
        ReInput.ControllerConnectedEvent += OnControllerConnected;
        ReInput.ControllerDisconnectedEvent += OnControllerDisconnected;
        ReInput.ControllerPreDisconnectEvent += OnControllerPreDisconnect;
    }

    // This function will be called when a controller is connected
    // You can get information about the controller that was connected via the args parameter
    void OnControllerConnected(ControllerStatusChangedEventArgs args) {
        Debug.Log("A controller was connected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + args.controllerType);
    }

     // This function will be called when a controller is fully disconnected
     // You can get information about the controller that was disconnected via the args parameter
     void OnControllerDisconnected(ControllerStatusChangedEventArgs args) {
        Debug.Log("A controller was disconnected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + args.controllerType);
    }

     // This function will be called when a controller is about to be disconnected
     // You can get information about the controller that is being disconnected via the args parameter
     // You can use this event to save the controller's maps before it's disconnected
     void OnControllerPreDisconnect(ControllerStatusChangedEventArgs args) {
        Debug.Log("A controller is being disconnected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + args.controllerType);
    }

    void OnDestroy() {
        // Unsubscribe from events
        ReInput.ControllerConnectedEvent -= OnControllerConnected;
        ReInput.ControllerDisconnectedEvent -= OnControllerDisconnected;
        ReInput.ControllerPreDisconnectEvent -= OnControllerPreDisconnect;
    }
}

震动功能

支持某些手柄在某些平台的全部特性。

int motorIndex = 0; // the first motor
float motorLevel = 1.0f; // full motor speed
float duration = 2.0f; // 2 seconds

player.SetVibration(motorIndex, motorLevel, duration);

 

使用建议

https://guavaman.com/projects/rewired/docs/BestPractices.html

  • Always make a mapping for the Gamepad Template
    绝大多数玩家使用的控制器是手柄。为了节省时间,请映射Gamepad Template,Rewired识别的所有Gamepad都可以使用。
  • Always make a mapping for the Unknown Controller
    如果玩家使用了不识别的手柄,他们至少可以有某种输入而无需手动映射。请参阅此以获取更多信息和建议的映射指南
  • If your game is 1-Player: Set Max Joysticks Per Player to 100 so all controllers attached to the system may be used by the player.
    如果你在制作单人游戏,将Max Joysticks Per Player 设置为100,这样做玩家可以使所有连接的手柄。
  • Windows requires that you enable XInput or use Direct Input as the primary input source in order to support the Steam Controller, Steam Streaming, and Steam Link. The recommended primary input source setting is Raw Input + Use XInput.
    Windows要求您启用XInput或使用直接输入作为主要输入源,以支持Steam控制器,Steam流媒体和Steam链接。推荐的主要输入源设置为Raw Input + Use XInput。
  • OSX should be set to Native for the primary input source. Steam Controller, Steam Streaming, and Steam Link work using the Native input source. (See this for known issues on OSX with Steam emulated controllers.)
    对于主要输入源,应将OSX设置为``本机''。Steam Controller,Steam Streaming和Steam Link使用本机输入源工作。(有关带有Steam模拟控制器的OSX上的已知问题,请参见...
  • Create a Rewired Initializer from the menu: Window -> Rewired -> Create -> Initializer (It is recommended you create a Rewired Initializer in each of your scenes. The Initializer will spawn the Rewired Input Manager as needed on Awake, but will not spawn multiple Rewired Input Managers when loading new scenes avoiding errors. This will allow you to use the same Rewired Input Manager in each scene and be able to test these scenes individually in the editor or load them during gameplay.)
    从菜单中创建一个Rewired Initializer:Window -> Rewired -> Create -> Initializer (建议您在每个场景中创建一个Rewired Initializer。初始化程序将在Awake上根据需要生成Rewired Input Manager,但为避免错误,在加载新场景时不会生成多个Rewired Input Manager。这样,您就可以在每个场景中使用相同的Rewired Input Manager,并能够在编辑器中单独测试这些场景或在游戏过程中加载它们。)

  • When writing scripts, it's recommended you use the input events method to get input as opposed to polling in Update because the Update loop will not be executed every frame by Unity.
    编写脚本时,建议您使用 input events 方法来获取输入,而不是在Update中进行轮询,因为Unity不会在每一帧都执行Update循环。

优化建议【原文

1.如果不曾在游戏的FixedUpdate或OnGUI中获得输入,请确保在“ Rewired Input Manager - Settings”中禁用了这些更新循环。您启用的每个其他更新循环都会增加Rewired的CPU使用率。

2.您拥有的Player,Action,已启用的Controller Maps和ActionElementMap绑定越多,则cpu为每一项每次更新评估输入所需的时间越长。

3.禁用当前游戏状态中不需要的所有Controller Maps 。例如,如果有用于UI控制的 Map Category,请在不显示UI时禁用所有UI Controller Maps 。

4.使用整数常量而不是字符串名称来调用采用Action,Map Category或Layout的方法。字符串比整数慢。如何获取常量列表的信息,请参见 Exporting constants for use in scripting 。

5.如果游戏在某些平台上不需要键盘输入,请使用Rewired Input Manager - Settings页中的平台选项将其禁用。

图像说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值