推薦閱讀
- CSDN主頁(yè)
- GitHub開(kāi)源地址
- Unity3D插件分享
- 簡(jiǎn)書(shū)地址
- 我的個(gè)人博客
大家好,我是佛系工程師☆恬靜的小魔龍☆,不定時(shí)更新Unity開(kāi)發(fā)技巧,覺(jué)得有用記得一鍵三連哦。
一、前言
在開(kāi)發(fā)中,會(huì)遇到要使用監(jiān)控鍵盤(pán)輸入的KeyCode值來(lái)執(zhí)行代碼的情況。
比如說(shuō):
using System;
using UnityEditor;
using UnityEngine;
public class Test01 : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("點(diǎn)擊了鍵盤(pán)W");
}
}
}
但是,如果是一些不常用的鍵位,比如說(shuō){}[],這些KeyCode值就比較難查看了,因?yàn)锳PI是這樣的:
根本不知道這英文還是數(shù)字代表了啥,于是就誕生了,在Unity做一個(gè)鍵盤(pán),然后在鍵盤(pán)的鍵位下標(biāo)注每個(gè)鍵位的KeyCode值,方便開(kāi)發(fā)。
先看下效果圖:
小明:鍵位沒(méi)有對(duì)齊,逼死強(qiáng)迫癥啊喂!
張三:不重要!不重要!
二、正文
2-1、構(gòu)建鍵盤(pán)鍵值表
讓我們新建一個(gè)腳本,命名為VirtualKeyboardEditor.cs
名字無(wú)所謂,主要是要繼承與EditorWindow
類,并且把腳本放到Editor文件夾內(nèi)。
編輯代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class VirtualKeyboardEditor : EditorWindow
{
[MenuItem("工具/鍵盤(pán)映射值")]
static void OpenWindow()
{
GetWindow<VirtualKeyboardEditor>().Show();
}
//用于繪制窗口內(nèi)容
private void OnGUI()
{
}
}
然后,我們需要寫(xiě)一個(gè)自定義類,用來(lái)保存鍵盤(pán)值名和KeyCode值,以及長(zhǎng)寬。
// 鍵盤(pán)映射value值
public class VirKeyValue
{
public float height;
public float width;
public string name;
public string key;
public VirKeyValue(string name, string key, float width, float height)
{
this.name = name;
this.width = width;
this.height = height;
this.key = key;
}
}
接著構(gòu)建鍵值庫(kù):
static Dictionary<int, List<VirKeyValue>> dicVirKeys;
const int width1 = 50;
const int width2 = 80;
const int width100 = 100;
const int width120 = 120;
const int width140 = 138;
const int width3 = 300;
const int height1 = 30;
private void DataInit()
{
if (dicVirKeys == null)
{
dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Esc","Esc", width1, height1),
new VirKeyValue("F1","F1", width1, height1),
new VirKeyValue("F2","F2", width1, height1),
new VirKeyValue("F3","F3", width1, height1),
new VirKeyValue("F4","F4", width1, height1),
new VirKeyValue("F5","F5", width1, height1),
new VirKeyValue("F6","F6", width1, height1),
new VirKeyValue("F7","F7", width1, height1),
new VirKeyValue("F8","F8", width1, height1),
new VirKeyValue("F9","F9", width1, height1),
new VirKeyValue("F10","F10", width1, height1),
new VirKeyValue("F11","F11", width1, height1),
new VirKeyValue("F12","F12", width1, height1),
new VirKeyValue("Print\nScreen","Print", width1, height1),
new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
new VirKeyValue("Pause","Pause", width1, height1)
};
dicVirKeys.Add(0, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("~\n`","BackQuote", width1, height1),
new VirKeyValue("!\n1","Alpha1", width1, height1),
new VirKeyValue("@\n2","Alpha2", width1, height1),
new VirKeyValue("#\n3","Alpha3", width1, height1),
new VirKeyValue("$\n4","Alpha4", width1, height1),
new VirKeyValue("%\n5","Alpha5", width1, height1),
new VirKeyValue("^\n6","Alpha6", width1, height1),
new VirKeyValue("&\n7","Alpha7", width1, height1),
new VirKeyValue("*\n8","Alpha8", width1, height1),
new VirKeyValue("(\n9","Alpha9", width1, height1),
new VirKeyValue(")\n0","Alpha0", width1, height1),
new VirKeyValue("_\n-","Minus", width1, height1),
new VirKeyValue("+\n=","Equals", width1, height1),
new VirKeyValue("←Backspace","Backspace", width120, height1),
new VirKeyValue("Insert","Insert", width1, height1),
new VirKeyValue("Home", "Home",width1, height1),
new VirKeyValue("Page\nUp", "PageUp",width1, height1),
new VirKeyValue("NumLk","Numlock", width1, height1),
new VirKeyValue("/","KeypadDivide", width1, height1),
new VirKeyValue("*","KeypadMultiply", width1, height1),
new VirKeyValue("-","KeypadMinus", width1, height1),
};
dicVirKeys.Add(1, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Tab","Tab", width100, height1),
new VirKeyValue("Q","Q",width1, height1),
new VirKeyValue("W","W",width1, height1),
new VirKeyValue("E","E",width1, height1),
new VirKeyValue("R","R",width1, height1),
new VirKeyValue("T","T",width1, height1),
new VirKeyValue("Y","Y",width1, height1),
new VirKeyValue("U","U",width1, height1),
new VirKeyValue("I","I",width1, height1),
new VirKeyValue("O","O",width1, height1),
new VirKeyValue("P","P",width1, height1),
new VirKeyValue("{\n[","LeftBracket", width1, height1),
new VirKeyValue("}\n]","RightBracket", width1, height1),
new VirKeyValue("|\n\\", "Backslash",70, height1),
new VirKeyValue("Delete", "Delete",width1, height1),
new VirKeyValue("End", "End",width1, height1),
new VirKeyValue("Page\nDown","PageDown", width1, height1),
new VirKeyValue("7","Keypad7",width1, height1),
new VirKeyValue("8","Keypad8",width1, height1),
new VirKeyValue("9","Keypad9",width1, height1),
new VirKeyValue("+","KeypadPlus",width1, height1),
};
dicVirKeys.Add(2, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Caps\nLock","Backspace", width120, height1),
new VirKeyValue("A","A", width1, height1),
new VirKeyValue("S","S", width1, height1),
new VirKeyValue("D","D", width1, height1),
new VirKeyValue("F","F", width1, height1),
new VirKeyValue("G","G", width1, height1),
new VirKeyValue("H","H", width1, height1),
new VirKeyValue("J","J", width1, height1),
new VirKeyValue("K","K", width1, height1),
new VirKeyValue("L","L", width1, height1),
new VirKeyValue(":\n;","Semicolon", width1, height1),
new VirKeyValue("\"\n'","Quote", width1, height1),
new VirKeyValue("Enter","Enter", 104, height1),
new VirKeyValue("4","Keypad4",width1, height1),
new VirKeyValue("5","Keypad5",width1, height1),
new VirKeyValue("6","Keypad6",width1, height1),
new VirKeyValue("Enter","KeypadEnter", width1, height1),
};
dicVirKeys.Add(3, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Shift","LeftShift", width140, height1),
new VirKeyValue("Z","Z",width1, height1),
new VirKeyValue("X","X",width1, height1),
new VirKeyValue("C","C",width1, height1),
new VirKeyValue("V","V",width1, height1),
new VirKeyValue("B","B",width1, height1),
new VirKeyValue("N","N",width1, height1),
new VirKeyValue("M","M",width1, height1),
new VirKeyValue("<\n,","Comma", width1, height1),
new VirKeyValue(">\n.","Period", width1, height1),
new VirKeyValue("?\n/","Slash", width1, height1),
new VirKeyValue("Shift","RightControl", width140, height1),
new VirKeyValue("↑","UpArrow", width1, height1),
new VirKeyValue("1","Keypad1", width1, height1),
new VirKeyValue("2","Keypad2", width1, height1),
new VirKeyValue("3","Keypad3", width1, height1),
};
dicVirKeys.Add(4, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Ctrl","LeftControl", width2, height1),
new VirKeyValue("Win", "LeftWindows",width1, height1),
new VirKeyValue("Alt", "LeftAlt",width1, height1),
new VirKeyValue("—————Space———","Space", width3, height1),
new VirKeyValue("Alt", "RightAlt",width1, height1),
new VirKeyValue("Ctrl", "RightControl",width2, height1),
new VirKeyValue("←","LeftArrow",width1, height1),
new VirKeyValue("↓","DownArrow",width1, height1),
new VirKeyValue("→","RightArrow",width1, height1),
new VirKeyValue("0","Keypad0",width100, height1),
new VirKeyValue(".","KeypadPeriod",width1, height1),
};
dicVirKeys.Add(5, tempVirKeys);
}
}
整體代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 鍵盤(pán)映射value值
public class VirKeyValue
{
public float height;
public float width;
public string name;
public string key;
public VirKeyValue(string name, string key, float width, float height)
{
this.name = name;
this.width = width;
this.height = height;
this.key = key;
}
}
public class VirtualKeyboardEditor : EditorWindow
{
[MenuItem("工具/鍵盤(pán)映射值")]
static void OpenWindow()
{
GetWindow<VirtualKeyboardEditor>().Show();
}
static Dictionary<int, List<VirKeyValue>> dicVirKeys;
const int width1 = 50;
const int width2 = 80;
const int width100 = 100;
const int width120 = 120;
const int width140 = 138;
const int width3 = 300;
const int height1 = 30;
private void DataInit()
{
if (dicVirKeys == null)
{
dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Esc","Esc", width1, height1),
new VirKeyValue("F1","F1", width1, height1),
new VirKeyValue("F2","F2", width1, height1),
new VirKeyValue("F3","F3", width1, height1),
new VirKeyValue("F4","F4", width1, height1),
new VirKeyValue("F5","F5", width1, height1),
new VirKeyValue("F6","F6", width1, height1),
new VirKeyValue("F7","F7", width1, height1),
new VirKeyValue("F8","F8", width1, height1),
new VirKeyValue("F9","F9", width1, height1),
new VirKeyValue("F10","F10", width1, height1),
new VirKeyValue("F11","F11", width1, height1),
new VirKeyValue("F12","F12", width1, height1),
new VirKeyValue("Print\nScreen","Print", width1, height1),
new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
new VirKeyValue("Pause","Pause", width1, height1)
};
dicVirKeys.Add(0, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("~\n`","BackQuote", width1, height1),
new VirKeyValue("!\n1","Alpha1", width1, height1),
new VirKeyValue("@\n2","Alpha2", width1, height1),
new VirKeyValue("#\n3","Alpha3", width1, height1),
new VirKeyValue("$\n4","Alpha4", width1, height1),
new VirKeyValue("%\n5","Alpha5", width1, height1),
new VirKeyValue("^\n6","Alpha6", width1, height1),
new VirKeyValue("&\n7","Alpha7", width1, height1),
new VirKeyValue("*\n8","Alpha8", width1, height1),
new VirKeyValue("(\n9","Alpha9", width1, height1),
new VirKeyValue(")\n0","Alpha0", width1, height1),
new VirKeyValue("_\n-","Minus", width1, height1),
new VirKeyValue("+\n=","Equals", width1, height1),
new VirKeyValue("←Backspace","Backspace", width120, height1),
new VirKeyValue("Insert","Insert", width1, height1),
new VirKeyValue("Home", "Home",width1, height1),
new VirKeyValue("Page\nUp", "PageUp",width1, height1),
new VirKeyValue("NumLk","Numlock", width1, height1),
new VirKeyValue("/","KeypadDivide", width1, height1),
new VirKeyValue("*","KeypadMultiply", width1, height1),
new VirKeyValue("-","KeypadMinus", width1, height1),
};
dicVirKeys.Add(1, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Tab","Tab", width100, height1),
new VirKeyValue("Q","Q",width1, height1),
new VirKeyValue("W","W",width1, height1),
new VirKeyValue("E","E",width1, height1),
new VirKeyValue("R","R",width1, height1),
new VirKeyValue("T","T",width1, height1),
new VirKeyValue("Y","Y",width1, height1),
new VirKeyValue("U","U",width1, height1),
new VirKeyValue("I","I",width1, height1),
new VirKeyValue("O","O",width1, height1),
new VirKeyValue("P","P",width1, height1),
new VirKeyValue("{\n[","LeftBracket", width1, height1),
new VirKeyValue("}\n]","RightBracket", width1, height1),
new VirKeyValue("|\n\\", "Backslash",70, height1),
new VirKeyValue("Delete", "Delete",width1, height1),
new VirKeyValue("End", "End",width1, height1),
new VirKeyValue("Page\nDown","PageDown", width1, height1),
new VirKeyValue("7","Keypad7",width1, height1),
new VirKeyValue("8","Keypad8",width1, height1),
new VirKeyValue("9","Keypad9",width1, height1),
new VirKeyValue("+","KeypadPlus",width1, height1),
};
dicVirKeys.Add(2, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Caps\nLock","Backspace", width120, height1),
new VirKeyValue("A","A", width1, height1),
new VirKeyValue("S","S", width1, height1),
new VirKeyValue("D","D", width1, height1),
new VirKeyValue("F","F", width1, height1),
new VirKeyValue("G","G", width1, height1),
new VirKeyValue("H","H", width1, height1),
new VirKeyValue("J","J", width1, height1),
new VirKeyValue("K","K", width1, height1),
new VirKeyValue("L","L", width1, height1),
new VirKeyValue(":\n;","Semicolon", width1, height1),
new VirKeyValue("\"\n'","Quote", width1, height1),
new VirKeyValue("Enter","Enter", 104, height1),
new VirKeyValue("4","Keypad4",width1, height1),
new VirKeyValue("5","Keypad5",width1, height1),
new VirKeyValue("6","Keypad6",width1, height1),
new VirKeyValue("Enter","KeypadEnter", width1, height1),
};
dicVirKeys.Add(3, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Shift","LeftShift", width140, height1),
new VirKeyValue("Z","Z",width1, height1),
new VirKeyValue("X","X",width1, height1),
new VirKeyValue("C","C",width1, height1),
new VirKeyValue("V","V",width1, height1),
new VirKeyValue("B","B",width1, height1),
new VirKeyValue("N","N",width1, height1),
new VirKeyValue("M","M",width1, height1),
new VirKeyValue("<\n,","Comma", width1, height1),
new VirKeyValue(">\n.","Period", width1, height1),
new VirKeyValue("?\n/","Slash", width1, height1),
new VirKeyValue("Shift","RightControl", width140, height1),
new VirKeyValue("↑","UpArrow", width1, height1),
new VirKeyValue("1","Keypad1", width1, height1),
new VirKeyValue("2","Keypad2", width1, height1),
new VirKeyValue("3","Keypad3", width1, height1),
};
dicVirKeys.Add(4, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Ctrl","LeftControl", width2, height1),
new VirKeyValue("Win", "LeftWindows",width1, height1),
new VirKeyValue("Alt", "LeftAlt",width1, height1),
new VirKeyValue("—————Space———","Space", width3, height1),
new VirKeyValue("Alt", "RightAlt",width1, height1),
new VirKeyValue("Ctrl", "RightControl",width2, height1),
new VirKeyValue("←","LeftArrow",width1, height1),
new VirKeyValue("↓","DownArrow",width1, height1),
new VirKeyValue("→","RightArrow",width1, height1),
new VirKeyValue("0","Keypad0",width100, height1),
new VirKeyValue(".","KeypadPeriod",width1, height1),
};
dicVirKeys.Add(5, tempVirKeys);
}
}
//用于繪制窗口內(nèi)容
private void OnGUI()
{
}
}
2-2、界面繪制
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 鍵盤(pán)映射value值
public class VirKeyValue
{
public float height;
public float width;
public string name;
public string key;
public VirKeyValue(string name, string key, float width, float height)
{
this.name = name;
this.width = width;
this.height = height;
this.key = key;
}
}
public class VirtualKeyboardEditor : EditorWindow
{
[MenuItem("工具/鍵盤(pán)映射值")]
static void OpenWindow()
{
GetWindow<VirtualKeyboardEditor>().Show();
}
static Dictionary<int, List<VirKeyValue>> dicVirKeys;
const int width1 = 50;
const int width2 = 80;
const int width100 = 100;
const int width120 = 120;
const int width140 = 138;
const int width3 = 300;
const int height1 = 30;
private void DataInit()
{
if (dicVirKeys == null)
{
dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Esc","Esc", width1, height1),
new VirKeyValue("F1","F1", width1, height1),
new VirKeyValue("F2","F2", width1, height1),
new VirKeyValue("F3","F3", width1, height1),
new VirKeyValue("F4","F4", width1, height1),
new VirKeyValue("F5","F5", width1, height1),
new VirKeyValue("F6","F6", width1, height1),
new VirKeyValue("F7","F7", width1, height1),
new VirKeyValue("F8","F8", width1, height1),
new VirKeyValue("F9","F9", width1, height1),
new VirKeyValue("F10","F10", width1, height1),
new VirKeyValue("F11","F11", width1, height1),
new VirKeyValue("F12","F12", width1, height1),
new VirKeyValue("Print\nScreen","Print", width1, height1),
new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
new VirKeyValue("Pause","Pause", width1, height1)
};
dicVirKeys.Add(0, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("~\n`","BackQuote", width1, height1),
new VirKeyValue("!\n1","Alpha1", width1, height1),
new VirKeyValue("@\n2","Alpha2", width1, height1),
new VirKeyValue("#\n3","Alpha3", width1, height1),
new VirKeyValue("$\n4","Alpha4", width1, height1),
new VirKeyValue("%\n5","Alpha5", width1, height1),
new VirKeyValue("^\n6","Alpha6", width1, height1),
new VirKeyValue("&\n7","Alpha7", width1, height1),
new VirKeyValue("*\n8","Alpha8", width1, height1),
new VirKeyValue("(\n9","Alpha9", width1, height1),
new VirKeyValue(")\n0","Alpha0", width1, height1),
new VirKeyValue("_\n-","Minus", width1, height1),
new VirKeyValue("+\n=","Equals", width1, height1),
new VirKeyValue("←Backspace","Backspace", width120, height1),
new VirKeyValue("Insert","Insert", width1, height1),
new VirKeyValue("Home", "Home",width1, height1),
new VirKeyValue("Page\nUp", "PageUp",width1, height1),
new VirKeyValue("NumLk","Numlock", width1, height1),
new VirKeyValue("/","KeypadDivide", width1, height1),
new VirKeyValue("*","KeypadMultiply", width1, height1),
new VirKeyValue("-","KeypadMinus", width1, height1),
};
dicVirKeys.Add(1, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Tab","Tab", width100, height1),
new VirKeyValue("Q","Q",width1, height1),
new VirKeyValue("W","W",width1, height1),
new VirKeyValue("E","E",width1, height1),
new VirKeyValue("R","R",width1, height1),
new VirKeyValue("T","T",width1, height1),
new VirKeyValue("Y","Y",width1, height1),
new VirKeyValue("U","U",width1, height1),
new VirKeyValue("I","I",width1, height1),
new VirKeyValue("O","O",width1, height1),
new VirKeyValue("P","P",width1, height1),
new VirKeyValue("{\n[","LeftBracket", width1, height1),
new VirKeyValue("}\n]","RightBracket", width1, height1),
new VirKeyValue("|\n\\", "Backslash",70, height1),
new VirKeyValue("Delete", "Delete",width1, height1),
new VirKeyValue("End", "End",width1, height1),
new VirKeyValue("Page\nDown","PageDown", width1, height1),
new VirKeyValue("7","Keypad7",width1, height1),
new VirKeyValue("8","Keypad8",width1, height1),
new VirKeyValue("9","Keypad9",width1, height1),
new VirKeyValue("+","KeypadPlus",width1, height1),
};
dicVirKeys.Add(2, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Caps\nLock","Backspace", width120, height1),
new VirKeyValue("A","A", width1, height1),
new VirKeyValue("S","S", width1, height1),
new VirKeyValue("D","D", width1, height1),
new VirKeyValue("F","F", width1, height1),
new VirKeyValue("G","G", width1, height1),
new VirKeyValue("H","H", width1, height1),
new VirKeyValue("J","J", width1, height1),
new VirKeyValue("K","K", width1, height1),
new VirKeyValue("L","L", width1, height1),
new VirKeyValue(":\n;","Semicolon", width1, height1),
new VirKeyValue("\"\n'","Quote", width1, height1),
new VirKeyValue("Enter","Enter", 104, height1),
new VirKeyValue("4","Keypad4",width1, height1),
new VirKeyValue("5","Keypad5",width1, height1),
new VirKeyValue("6","Keypad6",width1, height1),
new VirKeyValue("Enter","KeypadEnter", width1, height1),
};
dicVirKeys.Add(3, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Shift","LeftShift", width140, height1),
new VirKeyValue("Z","Z",width1, height1),
new VirKeyValue("X","X",width1, height1),
new VirKeyValue("C","C",width1, height1),
new VirKeyValue("V","V",width1, height1),
new VirKeyValue("B","B",width1, height1),
new VirKeyValue("N","N",width1, height1),
new VirKeyValue("M","M",width1, height1),
new VirKeyValue("<\n,","Comma", width1, height1),
new VirKeyValue(">\n.","Period", width1, height1),
new VirKeyValue("?\n/","Slash", width1, height1),
new VirKeyValue("Shift","RightControl", width140, height1),
new VirKeyValue("↑","UpArrow", width1, height1),
new VirKeyValue("1","Keypad1", width1, height1),
new VirKeyValue("2","Keypad2", width1, height1),
new VirKeyValue("3","Keypad3", width1, height1),
};
dicVirKeys.Add(4, tempVirKeys);
tempVirKeys = new List<VirKeyValue>
{
new VirKeyValue("Ctrl","LeftControl", width2, height1),
new VirKeyValue("Win", "LeftWindows",width1, height1),
new VirKeyValue("Alt", "LeftAlt",width1, height1),
new VirKeyValue("—————Space———","Space", width3, height1),
new VirKeyValue("Alt", "RightAlt",width1, height1),
new VirKeyValue("Ctrl", "RightControl",width2, height1),
new VirKeyValue("←","LeftArrow",width1, height1),
new VirKeyValue("↓","DownArrow",width1, height1),
new VirKeyValue("→","RightArrow",width1, height1),
new VirKeyValue("0","Keypad0",width100, height1),
new VirKeyValue(".","KeypadPeriod",width1, height1),
};
dicVirKeys.Add(5, tempVirKeys);
}
}
//用于繪制窗口內(nèi)容
private void OnGUI()
{
DataInit();
GUILayout.Label("Note:在開(kāi)發(fā)中會(huì)遇到使用監(jiān)控鍵盤(pán)輸入的情況,但是KeyCode的值往往分不清楚,此工具就是跟鍵盤(pán)一一對(duì)應(yīng)的Key值,不清楚的時(shí)候" +
"看一眼就行了,當(dāng)然,也可以點(diǎn)擊按鈕,點(diǎn)擊后可以打印當(dāng)前KeyCode值。");
for (int i = 0; i < dicVirKeys.Count; i++)
{
GUILayout.BeginVertical();
OnRow(i);
GUILayout.EndVertical();
}
}
void OnRow(int index)
{
GUILayout.BeginHorizontal();
for (int i = 0; i < dicVirKeys[index].Count; i++)
{
if (dicVirKeys[index][i].name == "↑")
{
GUILayout.Space(50);
}
else if (dicVirKeys[index][i].name == "←")
{
GUILayout.Space(180);
}
else if (dicVirKeys[index][i].name == "4")
{
GUILayout.Space(160);
}
else if (dicVirKeys[index][i].name == "1")
{
GUILayout.Space(60);
}
else if (dicVirKeys[index][i].name == "0")
{
GUILayout.Space(6);
}
GUILayout.BeginVertical();
if (GUILayout.Button(dicVirKeys[index][i].name, GUILayout.Width(dicVirKeys[index][i].width), GUILayout.Height(dicVirKeys[index][i].height)))
{
Debug.Log("當(dāng)前按下的鍵位是 : KeyCode." + dicVirKeys[index][i].key);
}
GUILayout.Label(dicVirKeys[index][i].key);
GUILayout.EndVertical();
if (dicVirKeys[index][i].name == "Esc")
{
GUILayout.Space(52);
}
else if (dicVirKeys[index][i].name == "F4")
{
GUILayout.Space(36);
}
else if (dicVirKeys[index][i].name == "F8")
{
GUILayout.Space(36);
}
}
GUILayout.EndHorizontal();
}
}
然后在Untiy編輯器的Edit欄,選擇工具→鍵盤(pán)映射值
打開(kāi)面板:
三、后記
如果覺(jué)得本篇文章有用別忘了點(diǎn)個(gè)關(guān)注,關(guān)注不迷路,持續(xù)分享更多Unity干貨文章。
你的點(diǎn)贊就是對(duì)博主的支持,有問(wèn)題記得留言:
博主主頁(yè)有聯(lián)系方式。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-714638.html
博主還有跟多寶藏文章等待你的發(fā)掘哦:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-714638.html
專欄 | 方向 | 簡(jiǎn)介 |
---|---|---|
Unity3D開(kāi)發(fā)小游戲 | 小游戲開(kāi)發(fā)教程 | 分享一些使用Unity3D引擎開(kāi)發(fā)的小游戲,分享一些制作小游戲的教程。 |
Unity3D從入門(mén)到進(jìn)階 | 入門(mén) | 從自學(xué)Unity中獲取靈感,總結(jié)從零開(kāi)始學(xué)習(xí)Unity的路線,有C#和Unity的知識(shí)。 |
Unity3D之UGUI | UGUI | Unity的UI系統(tǒng)UGUI全解析,從UGUI的基礎(chǔ)控件開(kāi)始講起,然后將UGUI的原理,UGUI的使用全面教學(xué)。 |
Unity3D之讀取數(shù)據(jù) | 文件讀取 | 使用Unity3D讀取txt文檔、json文檔、xml文檔、csv文檔、Excel文檔。 |
Unity3D之?dāng)?shù)據(jù)集合 | 數(shù)據(jù)集合 | 數(shù)組集合:數(shù)組、List、字典、堆棧、鏈表等數(shù)據(jù)集合知識(shí)分享。 |
Unity3D之VR/AR(虛擬仿真)開(kāi)發(fā) | 虛擬仿真 | 總結(jié)博主工作常見(jiàn)的虛擬仿真需求進(jìn)行案例講解。 |
Unity3D之插件 | 插件 | 主要分享在Unity開(kāi)發(fā)中用到的一些插件使用方法,插件介紹等 |
Unity3D之日常開(kāi)發(fā) | 日常記錄 | 主要是博主日常開(kāi)發(fā)中用到的,用到的方法技巧,開(kāi)發(fā)思路,代碼分享等 |
Unity3D之日常BUG | 日常記錄 | 記錄在使用Unity3D編輯器開(kāi)發(fā)項(xiàng)目過(guò)程中,遇到的BUG和坑,讓后來(lái)人可以有些參考。 |
到了這里,關(guān)于【Unity3D編輯器開(kāi)發(fā)】Unity3D中實(shí)現(xiàn)查看鍵盤(pán)對(duì)應(yīng)KeyCode值面板【方便開(kāi)發(fā)】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!