Skip to content

Commit 6067de5

Browse files
committed
feat(core): add tutorial 28 code
1 parent dc8e3f2 commit 6067de5

22 files changed

Lines changed: 1036 additions & 80 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Adapted from:
2+
* https://learn.unity.com/tutorial/property-drawers-and-custom-inspectors */
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
[CustomPropertyDrawer(typeof(InputBinding))]
7+
public class InputBindingDrawer : PropertyDrawer
8+
{
9+
// Draw the property inside the given rect
10+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
11+
{
12+
// Using BeginProperty / EndProperty on the parent property means that
13+
// prefab override logic works on the entire property.
14+
EditorGUI.BeginProperty(position, label, property);
15+
16+
// Draw label
17+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
18+
19+
// Don't make child fields be indented
20+
int indent = EditorGUI.indentLevel;
21+
EditorGUI.indentLevel = 0;
22+
23+
// Calculate rects
24+
float rowHeight = EditorGUIUtility.singleLineHeight;
25+
Rect displayNameRect = new Rect(position.x, position.y, position.width, rowHeight);
26+
Rect keyRect = new Rect(position.x, position.y + rowHeight, 50, rowHeight);
27+
Rect inputEventRect = new Rect(position.x + 55, position.y + rowHeight, position.width - 55, rowHeight);
28+
29+
// Draw fields - passs GUIContent.none to each so they are drawn without labels
30+
EditorGUI.PropertyField(displayNameRect, property.FindPropertyRelative("displayName"), GUIContent.none);
31+
EditorGUI.PropertyField(keyRect, property.FindPropertyRelative("key"), GUIContent.none);
32+
EditorGUI.PropertyField(inputEventRect, property.FindPropertyRelative("inputEvent"), GUIContent.none);
33+
34+
// Set indent back to what it was
35+
EditorGUI.indentLevel = indent;
36+
37+
EditorGUI.EndProperty();
38+
}
39+
40+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
41+
{
42+
return EditorGUIUtility.singleLineHeight * 2f;
43+
}
44+
}

Assets/Editor/InputBindingDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/PlayerDataDrawer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2222
EditorGUI.indentLevel = 0;
2323

2424
// Calculate rects
25-
float fullWidth = EditorGUIUtility.labelWidth;
26-
float nameWidth = fullWidth * 0.7f;
27-
float colorWidth = fullWidth * 0.3f;
25+
float nameWidth = position.width * 0.7f;
26+
float colorWidth = position.width * 0.3f;
2827
Rect nameRect = new Rect(position.x, position.y, nameWidth, position.height);
2928
Rect colorRect = new Rect(position.x + nameWidth + 5, position.y, colorWidth, position.height);
3029

Assets/Resources/Prefabs/UI/GameSettingsParameter.prefab

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ GameObject:
8989
- component: {fileID: 8808713573750823601}
9090
- component: {fileID: 8808713573750823603}
9191
- component: {fileID: 8808713573750823600}
92+
- component: {fileID: 4474194273264893024}
9293
m_Layer: 5
9394
m_Name: GameSettingsParameter
9495
m_TagString: Untagged
@@ -153,3 +154,28 @@ MonoBehaviour:
153154
m_FillOrigin: 0
154155
m_UseSpriteMesh: 0
155156
m_PixelsPerUnitMultiplier: 1
157+
--- !u!114 &4474194273264893024
158+
MonoBehaviour:
159+
m_ObjectHideFlags: 0
160+
m_CorrespondingSourceObject: {fileID: 0}
161+
m_PrefabInstance: {fileID: 0}
162+
m_PrefabAsset: {fileID: 0}
163+
m_GameObject: {fileID: 8808713573750823606}
164+
m_Enabled: 1
165+
m_EditorHideFlags: 0
166+
m_Script: {fileID: 11500000, guid: 30649d3a9faa99c48a7b1166b86bf2a0, type: 3}
167+
m_Name:
168+
m_EditorClassIdentifier:
169+
m_Padding:
170+
m_Left: 0
171+
m_Right: 0
172+
m_Top: 0
173+
m_Bottom: 0
174+
m_ChildAlignment: 0
175+
m_Spacing: 4
176+
m_ChildForceExpandWidth: 0
177+
m_ChildForceExpandHeight: 1
178+
m_ChildControlWidth: 0
179+
m_ChildControlHeight: 1
180+
m_ChildScaleWidth: 1
181+
m_ChildScaleHeight: 0

0 commit comments

Comments
 (0)