Skip to content

Commit

Permalink
Add UI Toolkit Editor Use Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelAsherRivello committed Sep 30, 2024
1 parent e4ce178 commit ec0d033
Show file tree
Hide file tree
Showing 39 changed files with 4,494 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public static class ReadMeMenuItems
// Fields ----------------------------------------
public const string PathMenuItemWindowCompanyProject = "Window/" + CompanyName + "/" + ProjectName;
public const string CompanyName = "RMC";
public const string ProjectName = "My Project";
public const int PriorityMenuItem_Examples = 1;
public const string ProjectName = "[MyProject]";
public const int PriorityMenuItem_Examples = -1000;

// Fields ----------------------------------------

Expand Down
63 changes: 23 additions & 40 deletions Unity/Assets/Scenes/Scene01_Intro.unity
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 64991505}
- component: {fileID: 64991504}
- component: {fileID: 64991508}
- component: {fileID: 64991509}
m_Layer: 0
m_Name: Player
m_TagString: Untagged
Expand Down Expand Up @@ -226,7 +227,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 450182382}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!54 &64991508
Rigidbody:
Expand Down Expand Up @@ -255,6 +256,22 @@ Rigidbody:
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!114 &64991509
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 64991503}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 300aa7a92254b14438d20d9f6346f557, type: 3}
m_Name:
m_EditorClassIdentifier:
_rigidBody: {fileID: 64991508}
_playerData:
MoveSpeed: 33
JumpSpeed: 343
--- !u!1 &126333138
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -736,39 +753,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ac0b09e7857660247b1477e93731de29, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &450182381
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 450182382}
m_Layer: 0
m_Name: World
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &450182382
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 450182381}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 64991507}
- {fileID: 2032229242}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &520134214
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -904,9 +888,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_hudUI: {fileID: 1756644691}
_playerRigidBody: {fileID: 64991508}
_moveSpeed: 10
_jumpSpeed: 4
_player: {fileID: 64991509}
--- !u!4 &1409149260
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1199,6 +1181,7 @@ Transform:
- {fileID: 520134215}
- {fileID: 281978814}
- {fileID: 1794521670}
- {fileID: 2032229242}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2032229241
Expand Down Expand Up @@ -1231,11 +1214,11 @@ Transform:
m_GameObject: {fileID: 2032229241}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.00000035762787}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 450182382}
m_Father: {fileID: 1880782305}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!64 &2032229243
MeshCollider:
Expand Down Expand Up @@ -1558,6 +1541,6 @@ MonoBehaviour:
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 450182382}
- {fileID: 1880782305}
- {fileID: 64991507}
- {fileID: 1409149260}
3 changes: 2 additions & 1 deletion Unity/Assets/Scripts/Editor/RMC.MyProject.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"references": [
"GUID:27619889b8ba8c24980f49ee34dbb44a",
"GUID:0acc523941302664db1f4e527237feb3",
"Runtime"
"Runtime",
"GUID:1bd920aa2cc01364d8e15ad9ca79abf3"
],
"includePlatforms": [
"Editor"
Expand Down
8 changes: 8 additions & 0 deletions Unity/Assets/Scripts/Editor/RMC/[MyProject]/PlayerData.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace RMC.MyProject.Data
{
/// <summary>
/// Renders the <see cref="PlayerData"/> in the Unity Custom Editor Window.
/// </summary>
public class PlayerDataEditorWindow : EditorWindow
{
// Properties ------------------------------------


// Fields ----------------------------------------
[SerializeField]
private PlayerData _playerData;

public const string PathMenuItemWindowCompanyProject = "Window/" + CompanyName + "/" + ProjectName;
public const string CompanyName = "RMC";
public const string ProjectName = "[MyProject]";
private const int PriorityMenuItem = -100;

private SerializedObject serializedPlayerData;
private TemplateContainer _playerDataLayout;

// Methods ---------------------------------------
[MenuItem( PathMenuItemWindowCompanyProject + "/" + "Open SampleEditorWindow",
false,
PriorityMenuItem)]
public static void ShowWindow()
{
var window = GetWindow<PlayerDataEditorWindow>();
window.titleContent = new GUIContent("Player Data Editor");
}

// Unity Methods ---------------------------------
private void OnEnable()
{
// Load and clone the UXML layout
var visualTree = Resources.Load<VisualTreeAsset>("Layouts/PlayerDataLayout");
_playerDataLayout = visualTree.CloneTree();
rootVisualElement.Add(_playerDataLayout);

// Bind PlayerData
BindPlayerData();

}

private void BindPlayerData()
{
// Create a dummy player data object to display in the editor
_playerData = new PlayerData { MoveSpeed = 5.0f, JumpSpeed = 3.0f };

Player player = GameObject.FindAnyObjectByType<Player>();

if (player == null)
{
Debug.LogError($"BindPlayerData() failed. Add '{nameof(Player)}' Component to Scene.");
return;
}
serializedPlayerData = new SerializedObject(player);
serializedPlayerData.Update();

// Bind the serialized object to the UI fields
_playerDataLayout.Bind(serializedPlayerData);

// Bind individual fields if needed
_playerDataLayout.Q<FloatField>("MoveSpeed").BindProperty(serializedPlayerData.FindProperty("_playerData.MoveSpeed"));
_playerDataLayout.Q<FloatField>("JumpSpeed").BindProperty(serializedPlayerData.FindProperty("_playerData.JumpSpeed"));
}


// Event Handlers --------------------------------
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace RMC.MyProject.Data
{
/// <summary>
/// Renders the <see cref="PlayerData"/> in the Unity Inspector Window.
/// </summary>
[CustomPropertyDrawer(typeof (PlayerData), true)]
public class PlayerDataPropertyDrawer : PropertyDrawer
{
// Properties ------------------------------------


// Fields ----------------------------------------
private TemplateContainer _playerDataLayout;

// Unity Methods ---------------------------------
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
// Load the UXML layout
var visualTree = Resources.Load<VisualTreeAsset>("Layouts/PlayerDataLayout");
_playerDataLayout = visualTree.CloneTree();

BindPlayerData(property);
return _playerDataLayout;
}


// Methods ---------------------------------------
private void BindPlayerData(SerializedProperty property)
{
// Bind the serialized property to the UI fields
_playerDataLayout.Q<FloatField>("MoveSpeed").BindProperty(property.FindPropertyRelative("MoveSpeed"));
_playerDataLayout.Q<FloatField>("JumpSpeed").BindProperty(property.FindPropertyRelative("JumpSpeed"));
}

// Event Handlers --------------------------------
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using UnityEngine;

namespace RMC.MyProject
{

/// <summary>
/// Represent the Player in the game world.
/// </summary>
public class Player : MonoBehaviour
{
// Properties ------------------------------------
public Rigidbody Rigidbody { get { return _rigidBody; }}
public PlayerData PlayerData { get { return _playerData; }}


// Fields ----------------------------------------
[Header("Player")]
[SerializeField]
private Rigidbody _rigidBody;

[Header("Player Data")]
[SerializeField]
private PlayerData _playerData;


// Unity Methods ---------------------------------
protected void Start()
{
Debug.Log($"{GetType().Name}.Start()");
}


// Methods ---------------------------------------


// Event Handlers --------------------------------
}
}
11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player/Player.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Unity/Assets/Scripts/Runtime/RMC/[MyProject]/Player/PlayerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace RMC.MyProject
{
/// <summary>
/// Store the configuration data for the <see cref="Player"/>
/// </summary>
[Serializable]
public class PlayerData
{
// Properties ------------------------------------


// Fields ----------------------------------------
public float MoveSpeed = 10;
public float JumpSpeed = 4;


// Methods ---------------------------------------


// Event Handlers --------------------------------
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ec0d033

Please # to comment.