I can’t find a solution how to construct the code, so that the effectivePath/overridePath is used by my code.
I’m using:
- [Behavior: Invoke C# Unity Events] in the Player Input Component as an own gameobject (I’ve tested the component on the Player gameobject, too).
- A script “Player_Input.cs”: This script controls every action/binding and sends values to other scripts.
- A script “Hotkey_Change.cs”: This script should show bindings on buttons and with “onClick” the player can change the binding of the specific action → works well so far (correct binding on buttons, even after rebinding), but the actual rebinding don’t takes occur.
The 3 Debug.Logs at the end are showing the right path of the action. - A script “Buttons_InGameMenu.cs”: This script controls the visibility of the inGame menus and also it enables and disables the PlayerActionMap for rebinding.
So, probably I’m a bit blind and someone has the answer.
Following the scripts:
Player_Input.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player_Input : MonoBehaviour
{
[HideInInspector] public LewdWitch _controls = null;
// Script Access
private Player_Attack _plAtt_script;
private Player_TakeObj _plTObj_script;
private Player_ThrowObj _plThObj_script;
private Levitation _levi_script;
private Player_Weapon _plWea_script;
private TeleAttack _plTeleAtt_script;
private player_movement _plMove_script;
private Aiming _aim_script;
private ShowInfoUI _showInfoUI_script;
private Zoom _zoom_script;
private Player_Stats _pStats_script;
private MJump _mjump_script;
private Player_Controls plyCon_script;
private MenuOpen menuOpen_script;
private void Start()
{
_controls.Enable();
_plAtt_script = GetComponent<Player_Attack>();
if (_plAtt_script == null)
{
Debug.LogError("Script: Player_Attack.cs missing! Please add it to the PLAYER.");
return;
}
_plTObj_script = GetComponent<Player_TakeObj>();
if (_plTObj_script == null)
{
Debug.LogError("Script: Player_TakeObj.cs missing! Please add it to the PLAYER.");
return;
}
_plThObj_script = GetComponent<Player_ThrowObj>();
if (_plThObj_script == null)
{
Debug.LogError("Script: Player_ThrowObj.cs missing! Please add it to the PLAYER.");
return;
}
_levi_script = GetComponent<Levitation>();
if (_levi_script == null)
{
Debug.LogError("Script: Levitation.cs missing! Please add it to the PLAYER.");
return;
}
_plWea_script = GetComponent<Player_Weapon>();
if (_plWea_script == null)
{
Debug.LogError("Script: Player_Weapon.cs missing! Please add it to the object PLAYER.");
return;
}
_plTeleAtt_script = GetComponent<TeleAttack>();
if (_plTeleAtt_script == null)
{
Debug.LogError("Script: TeleAttack.cs missing! Please add it to the object PLAYER.");
return;
}
_plMove_script = GetComponent<player_movement>();
if (_plMove_script == null)
{
Debug.LogError("Script: player_movement.cs missing! Please add it to the object PLAYER.");
return;
}
_aim_script = FindObjectOfType<Aiming>();
if (_aim_script == null)
{
Debug.LogError("Script: Aiming.cs missing! Please add it to the object UI/Pointer/SCRIPTS.");
return;
}
_showInfoUI_script = FindObjectOfType<ShowInfoUI>();
if (_showInfoUI_script == null)
{
Debug.LogError("Script: Aiming.cs missing! Please add it to the object UI/Pointer/SCRIPTS.");
return;
}
_zoom_script = GetComponent<Zoom>();
if (_zoom_script == null)
{
Debug.LogError("Script: player_movement.cs missing! Please add it to the object PLAYER.");
return;
}
_pStats_script = GetComponent<Player_Stats>();
if (_pStats_script == null)
{
Debug.LogError("Script: Player_Stats.cs missing! Please add it to the object PLAYER.");
return;
}
_mjump_script = GetComponent<MJump>();
if (_mjump_script == null)
{
Debug.LogError("Script: MJump.cs missing! Please add it to the object PLAYER.");
return;
}
plyCon_script = GetComponent<Player_Controls>();
if (plyCon_script == null)
{
Debug.LogError("Script: Player_Controls.cs missing! Please add it to the object PLAYER.");
return;
}
menuOpen_script = FindObjectOfType<MenuOpen>();
if (menuOpen_script == null)
{
Debug.LogError("Script: MenuOpen.cs missing! Please add it to the object PLAYER.");
return;
}
}
public void OnEnable()
{
if (_controls == null)
{
_controls = new LewdWitch();
}
//_controls.Enable();
_controls.Player.Move.performed += HandleMove;
//_controls.Player.Move.Enable();
_controls.Player.Run.performed += HandleRun;
//_controls.Player.Run.Enable();
_controls.Player.Strike_Right.performed += HandleStrikeRight;
//_controls.Player.Strike_Right.Enable();
_controls.Player.Strike_Left.performed += HandleStrikeLeft;
//_controls.Player.Strike_Left.Enable();
_controls.Player.Fire.performed += HandleAttack;
//_controls.Player.Fire.Enable();
_controls.Player.ChangeHand.performed += HandleChangeHand;
//_controls.Player.ChangeHand.Enable();
_controls.Player.Take.performed += HandleTake;
//_controls.Player.Take.Enable();
_controls.Player.Telekinesis.started += HandleTelekinesis;
_controls.Player.Telekinesis.performed += HandleTeleAT;
_controls.Player.Telekinesis.canceled += CancleTeleAT;
//_controls.Player.Telekinesis.Enable();
_controls.Player.UpDown.performed += HandleUpDown;
//_controls.Player.UpDown.Enable();
_controls.Player.AIM.performed += HandleAIM;
//_controls.Player.AIM.Enable();
_controls.Player.Jump.performed += HandleJump;
//_controls.Player.Jump.Enable();
_controls.Player.Info.performed += HandleInfo;
//_controls.Player.Info.Enable();
_controls.Player.Zoom.performed += HandleZoom;
//_controls.Player.Zoom.Enable();
_controls.Player.Menu.performed += HandleMenu;
//_controls.Player.Menu.Enable();
}
void OnDisable()
{
_controls.Disable();
//_controls.Player.Move.Disable();
//_controls.Player.Run.Disable();
//_controls.Player.Strike_Right.Disable();
//_controls.Player.Strike_Left.Disable();
//_controls.Player.Fire.Disable();
//_controls.Player.ChangeHand.Disable();
//_controls.Player.Take.Disable();
//_controls.Player.Telekinesis.Disable();
//_controls.Player.UpDown.Disable();
//_controls.Player.AIM.Disable();
//_controls.Player.Jump.Disable();
//_controls.Player.Info.Disable();
//_controls.Player.Zoom.Disable();
//_controls.Player.Menu.Disable();
}
// MOVING
void HandleMove(InputAction.CallbackContext context)
{
_plMove_script._moveAxis = context.ReadValue<Vector2>();
}
private void HandleRun(InputAction.CallbackContext context)
{
if (plyCon_script._run == context.ReadValueAsButton())
{
plyCon_script._run = false;
}
else
{
plyCon_script._run = context.ReadValueAsButton();
}
}
// Attack with weapons
private void HandleStrikeRight(InputAction.CallbackContext context)
{
// Weapon in the players RIGHT hand
if (_plWea_script.wearWeapon_R == false && _plTObj_script.activeTake == true)
{
_plTObj_script._wantWeapon_R = context.ReadValueAsButton();
}
if (_plWea_script.wearWeapon_R == true)
{
if (_aim_script.AIM == false)
{
if (_plTObj_script.Weapon_R.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.STAB)
{
_plWea_script._stabRight = context.ReadValueAsButton();
}
else if (_plTObj_script.Weapon_R.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.STRIKE)
{
_plWea_script._strikeRight = context.ReadValueAsButton();
}
else if (_plTObj_script.Weapon_R.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.BLUNT)
{
_plWea_script._bluntRight = context.ReadValueAsButton();
}
}
else
{
_plThObj_script.throw_R = true;
}
}
}
private void HandleStrikeLeft(InputAction.CallbackContext context)
{
// Weapon in the players LEFT hand
if (_plWea_script.wearWeapon_L == false && _plTObj_script.activeTake == true)
{
_plTObj_script._wantWeapon_L = context.ReadValueAsButton();
}
if (_plWea_script.wearWeapon_L == true)
{
if (_aim_script.AIM == false)
{
if (_plTObj_script.Weapon_L.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.STAB)
{
_plWea_script._stabLeft = context.ReadValueAsButton();
}
else if (_plTObj_script.Weapon_L.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.STRIKE)
{
_plWea_script._strikeLeft = context.ReadValueAsButton();
}
else if (_plTObj_script.Weapon_L.GetComponent<ObjectSettings>().weaponType == ObjectSettings.WeaponType.BLUNT)
{
_plWea_script._bluntLeft = context.ReadValueAsButton();
}
}
else
{
_plThObj_script.throw_L = true;
}
}
}
// Attack with magic
private void HandleAttack(InputAction.CallbackContext context)
{
_plAtt_script._attack = context.ReadValueAsButton();
}
private void HandleChangeHand(InputAction.CallbackContext context)
{
if (_plAtt_script.leftHand == false)
{
_plAtt_script.leftHand = context.ReadValueAsButton();
}
else
{
_plAtt_script.leftHand = false;
}
}
private void HandleTake(InputAction.CallbackContext context)
{
_plTObj_script.activeTake = context.ReadValueAsButton();
}
private void HandleTelekinesis(InputAction.CallbackContext context)
{
if (_levi_script._telek == false)
{
if ((_plWea_script.wearWeapon_R == false && _plAtt_script.leftHand == true) || (_plWea_script.wearWeapon_L == false && _plAtt_script.leftHand == false))
{
_levi_script._telek = context.ReadValueAsButton();
}
}
else if (_levi_script._telek == true)
{
_levi_script._telek = false;
_levi_script.noHit = true;
}
}
private void HandleUpDown(InputAction.CallbackContext context)
{
if (_levi_script.onCursor == true)
{
_levi_script.inputSOChange = context.ReadValue<Vector2>();
_levi_script.inputYSOChange = _levi_script.inputSOChange.y / 120;
if (_levi_script.inputYSOChange > 1)
{
_levi_script.inputYSOChange = 1;
}
if (_levi_script.inputYSOChange < -1)
{
_levi_script.inputYSOChange = -1;
}
if (_levi_script.counterForSOCh > 0.0F)
{
_levi_script.counterForSOCh -= Time.deltaTime;
}
if (_levi_script.counterForSOCh <= 0.0F)
{
if (_levi_script.inputYSOChange == 1)
{
_levi_script._mObjAlig_script.changeSortingOrderBy = 1;
}
else if (_levi_script.inputYSOChange == -1 && _levi_script._mObjAlig_script.onObject == false)
{
_levi_script._mObjAlig_script.minusNextSO = true;
_levi_script._mObjAlig_script.changeSortingOrderBy = 1;
}
if (_levi_script.inputYSOChange == -1 && _levi_script._mObjAlig_script.onObject == true)
{
Debug.Log("Object can't sink, because it's on an object.");
}
_levi_script.counterForSOCh = _levi_script.timeForSOCh;
}
}
}
private void HandleTeleAT(InputAction.CallbackContext context)
{
if (_plTeleAtt_script._teleAT == false)
{
if ((_plWea_script.wearWeapon_R == false && _plAtt_script.leftHand == true) || (_plWea_script.wearWeapon_L == false && _plAtt_script.leftHand == false))
{
_plTeleAtt_script._teleAT = context.ReadValueAsButton();
}
}
}
private void CancleTeleAT(InputAction.CallbackContext context)
{
if (_plTeleAtt_script._shot == false && _plTeleAtt_script._inTeleAT == true)
{
_plTeleAtt_script._shot = true;
}
_plTeleAtt_script._hits = 0;
_plTeleAtt_script._teleAT = false;
_plTeleAtt_script._inTeleAT = false;
}
private void HandleAIM(InputAction.CallbackContext context)
{
if (_aim_script.AIM == context.ReadValueAsButton())
{
_aim_script.AIM = false;
}
else
{
_aim_script.AIM = context.ReadValueAsButton();
}
}
private void HandleJump(InputAction.CallbackContext context)
{
if (_pStats_script.PlrSE >= _mjump_script.SECost)
{
if (_plMove_script._NoMOVE == false && _aim_script.AIM == false)
_mjump_script.jumpNow = context.ReadValueAsButton();
if (_aim_script.AIM == true)
_mjump_script.jumpNowAIM = context.ReadValueAsButton();
}
}
private void HandleInfo(InputAction.CallbackContext context)
{
if (_showInfoUI_script.Info == context.ReadValueAsButton())
{
_showInfoUI_script.Info = false;
}
else
{
_showInfoUI_script.Info = context.ReadValueAsButton();
}
}
private void HandleZoom(InputAction.CallbackContext context)
{
_zoom_script._inputZoom = context.ReadValue<float>();
}
private void HandleMenu(InputAction.CallbackContext context)
{
menuOpen_script._active = context.ReadValueAsButton();
}
}
Hotkey_Change.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
//using UnityEngine.InputSystem.LowLevel;
public class HotKey_Change : MonoBehaviour
{
public TMPro.TMP_Text _buttonText;
public InputActionReference m_ActionReference;
//public InputActionAsset current_ActionAsset;
public InputControlScheme? _controlScheme;
private InputAction m_Action;
private InputActionRebindingExtensions.RebindingOperation m_RebindOperation;
private string clearedPath;
private string oldPath;
private int _charIndex;
private char _singleChar;
//private Buttons_InGameMenu BIGM_script;
private Player_Input plyInput_script;
public bool _checkSomething = false;
public bool _isComposite = false;
// Start is called before the first frame update
void Start()
{
plyInput_script = FindObjectOfType<Player_Input>();
//BIGM_script = FindObjectOfType<Buttons_InGameMenu>();
// The single char "/" we need for deleting the path to this point
_singleChar = '/';
m_Action = m_ActionReference.action;
PathToButtonLMPText();
gameObject.GetComponent<Button>().onClick.AddListener(OnClickChangeKey);
// For check something at the beginning
if (_checkSomething == true)
{
//m_Action = m_ActionReference.action;
//for (int i = 0; i < m_ActionReference.action.bindings.Count; i++)
//{
// Debug.Log(m_Action.bindings);
//}
}
}
private void OnDestroy()
{
m_RebindOperation?.Dispose();
}
private void PathToButtonLMPText()
{
// We check, if the action has more than 1 bindings
//if (m_ActionReference.action.bindings.Count > 1)
//{
// We check every binding
for (int i = 0; i < m_Action.bindings.Count; i++)
{
// We chose all bindings with the group "Keyboard&Mouse"
if (m_Action.bindings[i].groups.Contains("Keyboard&Mouse"))
{
// We check, if the gameobject (the button) has the bindings name in its own name
if (gameObject.name.Contains(m_Action.bindings[i].name))
{
if (m_Action.bindings[i].effectivePath != null)
{
// We get the path of the binding
clearedPath = m_Action.bindings[i].effectivePath;
}
else
{
clearedPath = m_Action.bindings[i].path;
}
// We get the Index of the char "/" in the path
_charIndex = clearedPath.IndexOf(_singleChar);
// Now we clear the name to the index of "/". Example: <Keyboard>/w -> w
_buttonText.text = clearedPath.Remove(0, _charIndex + 1);
}
if (m_Action.bindings[i].name.Length !> 0)
{
// We check, if the gameobject (the button) has the bindings name in its own name
if (gameObject.name.Contains(m_Action.name))
{
if (m_Action.bindings[i].effectivePath != null)
{
// We get the path of the binding
clearedPath = m_Action.bindings[i].effectivePath;
}
else
{
clearedPath = m_Action.bindings[i].path;
}
// We get the Index of the char "/" in the path
_charIndex = clearedPath.IndexOf(_singleChar);
// Now we clear the name to the index of "/". Example: <Keyboard>/w -> w
_buttonText.text = clearedPath.Remove(0, _charIndex + 1);
}
}
}
// TODO: GAMEPAD
}
}
private void OnClickChangeKey()
{
for (int i = 0; i < m_Action.bindings.Count; i++)
{
if (m_Action.bindings[i].groups.Contains("Keyboard&Mouse"))
{
// We check, if the gameobject (the button) has the bindings name in its own name
if (gameObject.name.Contains(m_Action.bindings[i].name))
{
ActiveChanging(i);
//Debug.Log(m_ActionReference.action.bindings[i].path);
}
if (m_Action.bindings[i].name.Length !> 0)
{
// We check, if the gameobject (the button) has the action name in its own name
if (gameObject.name.Contains(m_Action.name))
{
Debug.Log(m_Action.bindings[i].path);
}
}
}
}
}
private void ActiveChanging(int index)
{
gameObject.GetComponent<Button>().enabled = false;
_buttonText.text = "Press Key";
m_RebindOperation?.Dispose();
m_RebindOperation = m_Action.PerformInteractiveRebinding(index)
.WithControlsExcluding("<Mouse>/position")
.WithControlsExcluding("<Mouse>/delta")
.OnMatchWaitForAnother(0.1f)
//.WithTargetBinding(index)
.WithBindingGroup("Keyboard&Mouse")
.OnComplete(operation => ButtonRebindCompleted(index));
m_RebindOperation = m_RebindOperation
.OnGeneratePath(x => "")
.OnApplyBinding((x, path) =>
{
m_Action.ApplyBindingOverride(index, new InputBinding { overridePath = path });
}).Start();
}
void ButtonRebindCompleted(int index)
{
// Test of a more direct change -> not working
//plyInput_script._controls.Player.Fire.ApplyBindingOverride(bindingOverride: new InputBinding(m_Action.bindings[index].overridePath));
m_RebindOperation.Dispose();
m_RebindOperation = null;
PathToButtonLMPText();
gameObject.GetComponent<Button>().enabled = true;
Debug.Log(m_Action.bindings[index].overridePath);
Debug.Log(m_Action.bindings[index].path);
Debug.Log(m_Action.bindings[index].effectivePath);
}
}
Buttons_InGameMenu.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
public class Buttons_InGameMenu : MonoBehaviour
{
// Panel Element
public GameObject _mainMenuPanel;
public GameObject _optionsPanel;
// Buttons
public Button _back;
public Button _resume;
public Button _options;
public InputActionAsset witchesInputActions;
private InputActionMap playerActionMap;
// Start is called before the first frame update
void Start()
{
playerActionMap = witchesInputActions.FindActionMap("Player");
_back.onClick.AddListener(Back);
_resume.onClick.AddListener(Resume);
_options.onClick.AddListener(Options);
}
void Back()
{
playerActionMap.Enable();
_optionsPanel.SetActive(false);
_mainMenuPanel.SetActive(true);
}
void Resume()
{
_mainMenuPanel.SetActive(false);
Time.timeScale = 1;
}
void Options()
{
playerActionMap.Disable();
_mainMenuPanel.SetActive(false);
_optionsPanel.SetActive(true);
}
}