public override void OnInspectorGUI() { using (var check = new EditorGUI.ChangeCheckScope()) { //Create new style for large sized labels based of normal label style GUIStyle styleLargeLabel = new GUIStyle(GUI.skin.GetStyle("label")) { fontSize = 15 }; //Create new style for medium sized labels based of normal label style GUIStyle styleMediumLabel = new GUIStyle(GUI.skin.GetStyle("label")) { fontSize = 12 }; //Create new style for large sized foldouts based of normal foldout style GUIStyle styleLargeFoldout = new GUIStyle(GUI.skin.GetStyle("foldout")) { fontSize = 15 }; //Draw field for changing key distance EditorGUILayout.LabelField("Key Distance", styleLargeLabel, GUILayout.Height(20)); GUILayout.Space(5); layout.distance = EditorGUILayout.FloatField(layout.distance); GUILayout.Space(10); //Draw buttons to allow for adjusting row count EditorGUILayout.LabelField("Rows | " + layout.rowCount, styleLargeLabel, GUILayout.Height(20)); EditorGUILayout.BeginHorizontal(); GUILayout.Space(EditorGUI.indentLevel * 17); if (GUILayout.Button("+", GUILayout.MaxWidth(30), GUILayout.MaxHeight(20))) { layout.rowCount++; } if (GUILayout.Button("-", GUILayout.MaxWidth(30), GUILayout.MaxHeight(20))) { layout.rowCount = Mathf.Max(1, layout.rowCount - 1); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); //Update row list to comply with updated row count while (layout.rowCount > layout.rows.Count) { layout.rows.Add(new KeyboardLayout.Row()); } while (layout.rowCount < layout.rows.Count) { layout.rows.RemoveAt(layout.rows.Count - 1); } //For each row, draw row editor for (int i = 0; i < layout.rowCount; i++) { KeyboardLayout.Row row = layout.rows[i]; //Draw foldout EditorGUI.indentLevel++; row.foldout = EditorGUILayout.Foldout(row.foldout, "Row " + (i + 1), true, styleLargeFoldout); GUILayout.Space(15); EditorGUI.indentLevel--; if (row.foldout) { EditorGUI.indentLevel++; //Draw field for changing row offset EditorGUILayout.LabelField("Row offset", styleMediumLabel, GUILayout.Height(15)); GUILayout.Space(5); row.offset = EditorGUILayout.FloatField(row.offset); GUILayout.Space(10); //Draw buttons to allow for adjusting key count EditorGUILayout.LabelField("Keys | " + row.keyCount, styleMediumLabel, GUILayout.Height(20)); GUILayout.BeginHorizontal(); GUILayout.Space(EditorGUI.indentLevel * 16); if (GUILayout.Button("+", GUILayout.MaxWidth(30), GUILayout.MaxHeight(15))) { row.keyCount++; } if (GUILayout.Button("-", GUILayout.MaxWidth(30), GUILayout.MaxHeight(15))) { row.keyCount = Mathf.Max(0, row.keyCount - 1); } GUILayout.EndHorizontal(); GUILayout.Space(20); //Update key list to comply with updated key count while (row.keyCount > row.keys.Count) { row.keys.Add(null); } while (row.keyCount < row.keys.Count) { row.keys.RemoveAt(row.keys.Count - 1); } //For each key, draw object field for (int j = 0; j < row.keyCount; j++) { row.keys[j] = (GameObject)EditorGUILayout.ObjectField("Key " + (j + 1), row.keys[j], typeof(GameObject), false); GUILayout.Space(5); } EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); EditorGUI.indentLevel--; } } //Draw buttons to allow for adjusting special key count EditorGUILayout.LabelField("Special Keys | " + layout.specialKeyCount, styleLargeLabel, GUILayout.Height(20)); EditorGUILayout.BeginHorizontal(); GUILayout.Space(EditorGUI.indentLevel * 17); if (GUILayout.Button("+", GUILayout.MaxWidth(30), GUILayout.MaxHeight(20))) { layout.specialKeyCount++; } if (GUILayout.Button("-", GUILayout.MaxWidth(30), GUILayout.MaxHeight(20))) { layout.specialKeyCount = Mathf.Max(0, layout.specialKeyCount - 1); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); //Update special key list to comply with updated special key count while (layout.specialKeyCount > layout.SpecialKeys.Count) { layout.SpecialKeys.Add(new KeyboardLayout.SpecialKey()); } while (layout.specialKeyCount < layout.SpecialKeys.Count) { layout.SpecialKeys.RemoveAt(layout.SpecialKeys.Count - 1); } //For each special key, draw editor for (int i = 0; i < layout.SpecialKeys.Count; i++) { KeyboardLayout.SpecialKey key = layout.SpecialKeys[i]; //Draw foldout EditorGUI.indentLevel++; key.foldout = EditorGUILayout.Foldout(key.foldout, "Key " + (i + 1), true, styleLargeFoldout); GUILayout.Space(15); EditorGUI.indentLevel--; if (key.foldout) { EditorGUI.indentLevel++; //Draw field for changing x offset EditorGUILayout.LabelField("Key X", styleMediumLabel, GUILayout.Height(15)); GUILayout.Space(5); key.offsetX = EditorGUILayout.FloatField(key.offsetX); GUILayout.Space(10); //Draw field for changing z offset EditorGUILayout.LabelField("Key Z", styleMediumLabel, GUILayout.Height(15)); GUILayout.Space(5); key.offsetZ = EditorGUILayout.FloatField(key.offsetZ); GUILayout.Space(10); //Draw object field key.key = (GameObject)EditorGUILayout.ObjectField("Key", key.key, typeof(GameObject), false); EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); EditorGUI.indentLevel--; } } //If changes have been made, mark object as dirty if (check.changed) { EditorUtility.SetDirty(layout); } } }
/// <summary> /// Removes unused keys and creates new as well as updates positions of all keys. /// </summary> public void UpdateKeys() { #if UNITY_EDITOR if (KeyboardLayout != null) { /* * To check if all keys are still in the keyboard layout, all keys will be added to a list. * If they are still in the list by the end of the method they will be deleted. */ List <ManagedKey> removeStack = new List <ManagedKey>(managedKeys); //For each row for (int i = 0; i < KeyboardLayout.rowCount; i++) { KeyboardLayout.Row row = KeyboardLayout.rows[i]; //For each key prefab for (int j = 0; j < row.keys.Count; j++) { GameObject prefab = row.keys[j]; if (prefab == null) { continue; } GameObject key = null; //Check if key prefab is already instantiated foreach (var managedKey in managedKeys) { if (managedKey.sourcePrefab != prefab || managedKey.row != i) { continue; } key = managedKey.key; removeStack.Remove(managedKey); } //if not, instantiate prefab if (key == null) { key = (GameObject)PrefabUtility.InstantiatePrefab(prefab, transform); key.GetComponent <Key>().KeyListeners.Add(gameObject); managedKeys.Add(new ManagedKey(key, prefab, i)); } //Update key position key.transform.localPosition = new Vector3(KeyboardLayout.distance * j + KeyboardLayout.distance * row.offset, 0, -KeyboardLayout.distance * i); } } //For each special key prefab foreach (var specialKeyPrefab in KeyboardLayout.SpecialKeys) { if (specialKeyPrefab.key == null) { continue; } GameObject specialKey = null; //Check if key prefab is already instantiated foreach (var managedKey in managedKeys) { if (managedKey.sourcePrefab != specialKeyPrefab.key) { continue; } specialKey = managedKey.key; removeStack.Remove(managedKey); } //if not, instantiate prefab if (specialKey == null) { specialKey = (GameObject)PrefabUtility.InstantiatePrefab(specialKeyPrefab.key, transform); specialKey.GetComponent <Key>().KeyListeners.Add(gameObject); managedKeys.Add(new ManagedKey(specialKey, specialKeyPrefab.key, 0)); } //Update key position specialKey.transform.localPosition = new Vector3(KeyboardLayout.distance * specialKeyPrefab.offsetX, 0, -KeyboardLayout.distance * specialKeyPrefab.offsetZ); } //Remove keys that are no longer a part of the layout foreach (var managedKey in removeStack) { if (managedKey.key != null) { DestroyImmediate(managedKey.key); } managedKeys.Remove(managedKey); } } #endif }