Skip to content

Commit b312ae3

Browse files
Add UI Toolkit To Scene - For Runtime & Editor
1 parent e4ce178 commit b312ae3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4521
-103
lines changed

Unity/Assets/Documentation/ReadMe/Scripts/Editor/ReadMeMenuItems.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public static class ReadMeMenuItems
88
// Fields ----------------------------------------
99
public const string PathMenuItemWindowCompanyProject = "Window/" + CompanyName + "/" + ProjectName;
1010
public const string CompanyName = "RMC";
11-
public const string ProjectName = "My Project";
12-
public const int PriorityMenuItem_Examples = 1;
11+
public const string ProjectName = "[MyProject]";
12+
public const int PriorityMenuItem_Examples = -1000;
1313

1414
// Fields ----------------------------------------
1515

Unity/Assets/Scenes/Scene01_Intro.unity

+23-40
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ GameObject:
135135
- component: {fileID: 64991505}
136136
- component: {fileID: 64991504}
137137
- component: {fileID: 64991508}
138+
- component: {fileID: 64991509}
138139
m_Layer: 0
139140
m_Name: Player
140141
m_TagString: Untagged
@@ -226,7 +227,7 @@ Transform:
226227
m_LocalScale: {x: 1, y: 1, z: 1}
227228
m_ConstrainProportionsScale: 0
228229
m_Children: []
229-
m_Father: {fileID: 450182382}
230+
m_Father: {fileID: 0}
230231
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
231232
--- !u!54 &64991508
232233
Rigidbody:
@@ -255,6 +256,22 @@ Rigidbody:
255256
m_Interpolate: 0
256257
m_Constraints: 0
257258
m_CollisionDetection: 0
259+
--- !u!114 &64991509
260+
MonoBehaviour:
261+
m_ObjectHideFlags: 0
262+
m_CorrespondingSourceObject: {fileID: 0}
263+
m_PrefabInstance: {fileID: 0}
264+
m_PrefabAsset: {fileID: 0}
265+
m_GameObject: {fileID: 64991503}
266+
m_Enabled: 1
267+
m_EditorHideFlags: 0
268+
m_Script: {fileID: 11500000, guid: 300aa7a92254b14438d20d9f6346f557, type: 3}
269+
m_Name:
270+
m_EditorClassIdentifier:
271+
_rigidBody: {fileID: 64991508}
272+
_playerData:
273+
MoveSpeed: 5
274+
JumpSpeed: 5
258275
--- !u!1 &126333138
259276
GameObject:
260277
m_ObjectHideFlags: 0
@@ -736,39 +753,6 @@ MonoBehaviour:
736753
m_Script: {fileID: 11500000, guid: ac0b09e7857660247b1477e93731de29, type: 3}
737754
m_Name:
738755
m_EditorClassIdentifier:
739-
--- !u!1 &450182381
740-
GameObject:
741-
m_ObjectHideFlags: 0
742-
m_CorrespondingSourceObject: {fileID: 0}
743-
m_PrefabInstance: {fileID: 0}
744-
m_PrefabAsset: {fileID: 0}
745-
serializedVersion: 6
746-
m_Component:
747-
- component: {fileID: 450182382}
748-
m_Layer: 0
749-
m_Name: World
750-
m_TagString: Untagged
751-
m_Icon: {fileID: 0}
752-
m_NavMeshLayer: 0
753-
m_StaticEditorFlags: 0
754-
m_IsActive: 1
755-
--- !u!4 &450182382
756-
Transform:
757-
m_ObjectHideFlags: 0
758-
m_CorrespondingSourceObject: {fileID: 0}
759-
m_PrefabInstance: {fileID: 0}
760-
m_PrefabAsset: {fileID: 0}
761-
m_GameObject: {fileID: 450182381}
762-
serializedVersion: 2
763-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
764-
m_LocalPosition: {x: 0, y: 0, z: 0}
765-
m_LocalScale: {x: 1, y: 1, z: 1}
766-
m_ConstrainProportionsScale: 0
767-
m_Children:
768-
- {fileID: 64991507}
769-
- {fileID: 2032229242}
770-
m_Father: {fileID: 0}
771-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
772756
--- !u!1 &520134214
773757
GameObject:
774758
m_ObjectHideFlags: 0
@@ -904,9 +888,7 @@ MonoBehaviour:
904888
m_Name:
905889
m_EditorClassIdentifier:
906890
_hudUI: {fileID: 1756644691}
907-
_playerRigidBody: {fileID: 64991508}
908-
_moveSpeed: 10
909-
_jumpSpeed: 4
891+
_player: {fileID: 64991509}
910892
--- !u!4 &1409149260
911893
Transform:
912894
m_ObjectHideFlags: 0
@@ -1199,6 +1181,7 @@ Transform:
11991181
- {fileID: 520134215}
12001182
- {fileID: 281978814}
12011183
- {fileID: 1794521670}
1184+
- {fileID: 2032229242}
12021185
m_Father: {fileID: 0}
12031186
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
12041187
--- !u!1 &2032229241
@@ -1231,11 +1214,11 @@ Transform:
12311214
m_GameObject: {fileID: 2032229241}
12321215
serializedVersion: 2
12331216
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
1234-
m_LocalPosition: {x: 0, y: 0, z: 0.00000035762787}
1217+
m_LocalPosition: {x: 0, y: 0, z: 0}
12351218
m_LocalScale: {x: 1, y: 1, z: 1}
12361219
m_ConstrainProportionsScale: 0
12371220
m_Children: []
1238-
m_Father: {fileID: 450182382}
1221+
m_Father: {fileID: 1880782305}
12391222
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
12401223
--- !u!64 &2032229243
12411224
MeshCollider:
@@ -1558,6 +1541,6 @@ MonoBehaviour:
15581541
SceneRoots:
15591542
m_ObjectHideFlags: 0
15601543
m_Roots:
1561-
- {fileID: 450182382}
15621544
- {fileID: 1880782305}
1545+
- {fileID: 64991507}
15631546
- {fileID: 1409149260}

Unity/Assets/Scripts/Editor/RMC.MyProject.Editor.asmdef

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"GUID:27619889b8ba8c24980f49ee34dbb44a",
66
"GUID:0acc523941302664db1f4e527237feb3",
7-
"Runtime"
7+
"Runtime",
8+
"GUID:1bd920aa2cc01364d8e15ad9ca79abf3"
89
],
910
"includePlatforms": [
1011
"Editor"

Unity/Assets/Scripts/Editor/RMC/[MyProject]/PlayerData.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using UnityEditor;
2+
using UnityEditor.UIElements;
3+
using UnityEngine;
4+
using UnityEngine.UIElements;
5+
6+
namespace RMC.MyProject.Data
7+
{
8+
/// <summary>
9+
/// Renders the <see cref="PlayerData"/> in the Unity Custom Editor Window.
10+
/// </summary>
11+
public class PlayerDataEditorWindow : EditorWindow
12+
{
13+
// Properties ------------------------------------
14+
15+
16+
// Const -----------------------------------------
17+
private const string PathMenuItemWindowCompanyProject = "Window/" + CompanyName + "/" + ProjectName;
18+
private const string CompanyName = "RMC";
19+
private const string ProjectName = "[MyProject]";
20+
private const int PriorityMenuItem = -100;
21+
22+
// Fields ----------------------------------------
23+
[SerializeField]
24+
private PlayerData _playerData;
25+
26+
private SerializedObject serializedPlayerData;
27+
private TemplateContainer _playerDataLayout;
28+
29+
// Methods ---------------------------------------
30+
[MenuItem( PathMenuItemWindowCompanyProject + "/" + "Open SampleEditorWindow",
31+
false,
32+
PriorityMenuItem)]
33+
public static void ShowWindow()
34+
{
35+
var window = GetWindow<PlayerDataEditorWindow>();
36+
window.titleContent = new GUIContent("Player Data Editor");
37+
}
38+
39+
40+
// Unity Methods ---------------------------------
41+
private void OnEnable()
42+
{
43+
// Load and clone the UXML layout
44+
var visualTree = Resources.Load<VisualTreeAsset>("Layouts/PlayerDataLayout");
45+
_playerDataLayout = visualTree.CloneTree();
46+
rootVisualElement.Add(_playerDataLayout);
47+
48+
// Bind PlayerData
49+
BindPlayerData();
50+
51+
}
52+
53+
54+
private void BindPlayerData()
55+
{
56+
// Create a dummy player data object to display in the editor
57+
_playerData = new PlayerData { MoveSpeed = 5.0f, JumpSpeed = 3.0f };
58+
59+
Player player = GameObject.FindAnyObjectByType<Player>();
60+
61+
if (player == null)
62+
{
63+
Debug.LogError($"BindPlayerData() failed. Add '{nameof(Player)}' Component to Scene.");
64+
return;
65+
}
66+
serializedPlayerData = new SerializedObject(player);
67+
serializedPlayerData.Update();
68+
69+
// Bind the serialized object to the UI fields
70+
_playerDataLayout.Bind(serializedPlayerData);
71+
72+
// Bind individual fields if needed
73+
_playerDataLayout.Q<FloatField>("MoveSpeed").BindProperty(serializedPlayerData.FindProperty("_playerData.MoveSpeed"));
74+
_playerDataLayout.Q<FloatField>("JumpSpeed").BindProperty(serializedPlayerData.FindProperty("_playerData.JumpSpeed"));
75+
}
76+
77+
78+
// Event Handlers --------------------------------
79+
}
80+
}

Unity/Assets/Scripts/Editor/RMC/[MyProject]/PlayerData/PlayerDataEditorWindow.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEditor;
2+
using UnityEditor.UIElements;
3+
using UnityEngine;
4+
using UnityEngine.UIElements;
5+
6+
namespace RMC.MyProject.Data
7+
{
8+
/// <summary>
9+
/// Renders the <see cref="PlayerData"/> in the Unity Inspector Window.
10+
/// </summary>
11+
[CustomPropertyDrawer(typeof (PlayerData), true)]
12+
public class PlayerDataPropertyDrawer : PropertyDrawer
13+
{
14+
// Properties ------------------------------------
15+
16+
17+
// Fields ----------------------------------------
18+
private TemplateContainer _playerDataLayout;
19+
20+
// Unity Methods ---------------------------------
21+
public override VisualElement CreatePropertyGUI(SerializedProperty property)
22+
{
23+
// Load the UXML layout
24+
var visualTree = Resources.Load<VisualTreeAsset>("Layouts/PlayerDataLayout");
25+
_playerDataLayout = visualTree.CloneTree();
26+
27+
BindPlayerData(property);
28+
return _playerDataLayout;
29+
}
30+
31+
32+
// Methods ---------------------------------------
33+
private void BindPlayerData(SerializedProperty property)
34+
{
35+
// Bind the serialized property to the UI fields
36+
_playerDataLayout.Q<FloatField>("MoveSpeed").BindProperty(property.FindPropertyRelative("MoveSpeed"));
37+
_playerDataLayout.Q<FloatField>("JumpSpeed").BindProperty(property.FindPropertyRelative("JumpSpeed"));
38+
}
39+
40+
// Event Handlers --------------------------------
41+
}
42+
}

Unity/Assets/Scripts/Editor/RMC/[MyProject]/PlayerData/PlayerDataPropertyDrawer.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using UnityEngine;
2+
3+
namespace RMC.MyProject
4+
{
5+
6+
/// <summary>
7+
/// Represent the Player in the game world.
8+
/// </summary>
9+
public class Player : MonoBehaviour
10+
{
11+
// Properties ------------------------------------
12+
public Rigidbody Rigidbody { get { return _rigidBody; }}
13+
public PlayerData PlayerData { get { return _playerData; }}
14+
15+
16+
// Fields ----------------------------------------
17+
[Header("Player")]
18+
[SerializeField]
19+
private Rigidbody _rigidBody;
20+
21+
[Header("Player Data")]
22+
[SerializeField]
23+
private PlayerData _playerData;
24+
25+
26+
// Unity Methods ---------------------------------
27+
protected void Start()
28+
{
29+
Debug.Log($"{GetType().Name}.Start()");
30+
}
31+
32+
33+
// Methods ---------------------------------------
34+
35+
36+
// Event Handlers --------------------------------
37+
}
38+
}

Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player/Player.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace RMC.MyProject
4+
{
5+
/// <summary>
6+
/// Store the configuration data for the <see cref="Player"/>
7+
/// </summary>
8+
[Serializable]
9+
public class PlayerData
10+
{
11+
// Properties ------------------------------------
12+
13+
14+
// Fields ----------------------------------------
15+
public float MoveSpeed = 10;
16+
public float JumpSpeed = 4;
17+
18+
19+
// Methods ---------------------------------------
20+
21+
22+
// Event Handlers --------------------------------
23+
}
24+
}

Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player/PlayerData.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)