Files
2026-03-04 10:03:45 +08:00

145 lines
3.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class KeyBindListItem : MonoBehaviour
{
public Transform containerForKeyItem;
private KeyBindItem currentKey;
public GameObject[] KeyPrefabs;
public GameObject ContflictGameobject;
public KeyCode keycode;
public Text DescText;
public Text ClickButtonText;
public Text ContflictText;
public int indexOfKey;
[HideInInspector]
public KeyBindManager keyBindManager;
private void Start()
{
GetCurrentKey();
ClickButtonText.gameObject.SetActive(value: false);
}
private void Update()
{
CheckConflict();
}
private void CheckConflict()
{
for (int i = 0; i < keyBindManager.inputManager.keysbind.Count; i++)
{
if (keyBindManager.inputManager.keysbind[i].key == keycode && i != indexOfKey)
{
ContflictGameobject.gameObject.SetActive(value: true);
ContflictText.text = keyBindManager.ConvertKeyToString(keycode);
break;
}
ContflictGameobject.gameObject.SetActive(value: false);
}
}
public void OnGUI()
{
if (!ClickButtonText.gameObject.activeSelf)
{
return;
}
Event current = Event.current;
if (current.isKey && current.type == EventType.KeyDown)
{
Debug.Log(current.keyCode);
if (current.keyCode != KeyCode.None && current.keyCode != KeyCode.Escape && current.keyCode != KeyCode.Return)
{
keycode = current.keyCode;
Debug.Log("nadany key: " + keycode);
GetCurrentKey();
ClickButtonText.gameObject.SetActive(value: false);
keyBindManager.inputManager.keysbind[indexOfKey].key = keycode;
keyBindManager.isSelect = false;
}
}
}
public void OnClickSetKey()
{
if (!keyBindManager.isSelect)
{
ClickButtonText.gameObject.SetActive(value: true);
currentKey.gameObject.SetActive(value: false);
keyBindManager.isSelect = true;
}
}
public void GetCurrentKey()
{
if ((bool)currentKey)
{
Object.Destroy(currentKey.gameObject);
currentKey = null;
}
GameObject original;
switch (keycode)
{
case KeyCode.Backspace:
case KeyCode.Tab:
case KeyCode.Return:
case KeyCode.Delete:
case KeyCode.Keypad0:
case KeyCode.Keypad1:
case KeyCode.Keypad2:
case KeyCode.Keypad3:
case KeyCode.Keypad4:
case KeyCode.Keypad5:
case KeyCode.Keypad6:
case KeyCode.Keypad7:
case KeyCode.Keypad8:
case KeyCode.Keypad9:
case KeyCode.KeypadPeriod:
case KeyCode.KeypadDivide:
case KeyCode.KeypadMultiply:
case KeyCode.KeypadMinus:
case KeyCode.KeypadPlus:
case KeyCode.UpArrow:
case KeyCode.DownArrow:
case KeyCode.RightArrow:
case KeyCode.LeftArrow:
case KeyCode.Insert:
case KeyCode.Home:
case KeyCode.End:
case KeyCode.PageUp:
case KeyCode.PageDown:
case KeyCode.Numlock:
case KeyCode.CapsLock:
case KeyCode.RightShift:
case KeyCode.LeftShift:
case KeyCode.RightControl:
case KeyCode.LeftControl:
case KeyCode.RightAlt:
case KeyCode.LeftAlt:
original = KeyPrefabs[1];
break;
case KeyCode.Space:
case KeyCode.KeypadEnter:
original = KeyPrefabs[2];
break;
default:
original = KeyPrefabs[0];
break;
}
currentKey = Object.Instantiate(original, containerForKeyItem).GetComponent<KeyBindItem>();
currentKey.transform.localPosition = Vector3.zero;
currentKey.keyText.text = keyBindManager.ConvertKeyToString(keycode).ToUpper();
}
}