-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
- Loading branch information
There are no files selected for viewing
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.
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,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 -------------------------------- | ||
} | ||
} |
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,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.