Add Debug UI integration and update TestScript interaction logic

- Implement `DebugUi` component for updating and displaying debug information.
- Configure new UI Toolkit assets, including `PanelSettings` and `UIDocument`.
- Update TestScript to support dynamic ForceMode, velocity, and difference tracking with debug UI.
- Minor adjustments to Rigidbody torque and interaction parameters.
This commit is contained in:
2025-10-11 00:41:23 +02:00
parent cac112a79c
commit 04d419d6aa
12 changed files with 284 additions and 9 deletions

42
Assets/DebugUi.cs Normal file
View File

@@ -0,0 +1,42 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
public class DebugUi : MonoBehaviour
{
private UIDocument _uiDocument;
private Label _label;
private Label _currentLabel;
private Label _forceMultiplierLabel;
private Label _velocityLabel;
private void OnEnable()
{
_uiDocument = GetComponent<UIDocument>();
_label = _uiDocument.rootVisualElement.Q<Label>("springDifferenceLabel");
_currentLabel = _uiDocument.rootVisualElement.Q<Label>("currentDifferenceLabel");
_forceMultiplierLabel = _uiDocument.rootVisualElement.Q<Label>("forceMultiplier");
_velocityLabel = _uiDocument.rootVisualElement.Q<Label>("velocityLabel");
}
public void UpdateText(string text)
{
_label.text = text;
}
public void UpdateCurrentText(string text)
{
_currentLabel.text = text;
}
public void UpdateForceMultiplier(string forceMultiplier)
{
_forceMultiplierLabel.text = forceMultiplier;
}
public void UpdateVelocity(string velocity)
{
_velocityLabel.text = velocity;
}
}

3
Assets/DebugUi.cs.meta Normal file
View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 906875a0321a453385162fea37ac5e73
timeCreated: 1760115512

View File

@@ -561,13 +561,13 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 330585543}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: 0, y: 1, z: 4.5}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 2.21, y: 1, z: -4.5}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &330585547
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -739,6 +739,74 @@ MonoBehaviour:
m_ShadowLayerMask: 1
m_RenderingLayers: 1
m_ShadowRenderingLayers: 1
--- !u!1 &430138086
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 430138089}
- component: {fileID: 430138088}
- component: {fileID: 430138087}
m_Layer: 5
m_Name: UIDocument
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &430138087
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 430138086}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 906875a0321a453385162fea37ac5e73, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::DebugUi
--- !u!114 &430138088
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 430138086}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 19102, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier: UnityEngine.dll::UnityEngine.UIElements.UIDocument
m_PanelSettings: {fileID: 11400000, guid: ecc77eee43430c049ba9c709b50e1317, type: 2}
m_ParentUI: {fileID: 0}
sourceAsset: {fileID: 9197481963319205126, guid: 340417dc6f0dc8a498146934d6f65e32, type: 3}
m_SortingOrder: 0
m_Position: 0
m_WorldSpaceSizeMode: 1
m_WorldSpaceWidth: 200
m_WorldSpaceHeight: 400
m_PivotReferenceSize: 0
m_Pivot: 0
m_WorldSpaceCollider: {fileID: 0}
--- !u!4 &430138089
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 430138086}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 1.07, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 965777006}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &457588460
GameObject:
m_ObjectHideFlags: 0
@@ -1761,7 +1829,8 @@ Transform:
m_LocalPosition: {x: 0, y: 1.287, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Children:
- {fileID: 430138089}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!65 &965777007
@@ -1811,9 +1880,11 @@ MonoBehaviour:
m_EditorClassIdentifier: Assembly-CSharp::TestScript
torqueForce: 1
lerpSpeed: 5
softPositionSpring: 300
softPositionSpring: 250
forceMode: 2
_pump: {fileID: 640614938}
pump: {fileID: 640614938}
debugUi: {fileID: 430138087}
timeScale: 0.1
--- !u!114 &965777010
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
public class TestScript : MonoBehaviour
{
@@ -11,10 +12,15 @@ public class TestScript : MonoBehaviour
private ConfigurableJoint _configurableJoint;
private float _smoothX;
// max is 0.63
// min is 0.15
private JointDrive _jointDrive;
private JointDrive _softJointDrive;
private double _maxDifference = 0;
[SerializeField]
private float torqueForce;
@@ -28,7 +34,13 @@ public class TestScript : MonoBehaviour
private ForceMode forceMode;
[SerializeField]
private Rigidbody _pump;
private Rigidbody pump;
[SerializeField]
private DebugUi debugUi;
[SerializeField]
private float timeScale = 1f;
private void Awake()
{
@@ -53,6 +65,23 @@ public class TestScript : MonoBehaviour
_moveAction.Enable();
_jumpAction.Enable();
_maxDifference = GetDifference();
debugUi.UpdateText($"Difference: {_maxDifference}");
Time.timeScale = timeScale;
}
private double GetDifference()
{
Vector3 axis = transform.up; // this transform's local Y in world space
Vector3 toOther = pump.transform.position - transform.position;
var val = Vector3.Dot(toOther, axis);
val = Mathf.Abs(val);
return Math.Round(val, 2); // signed distance along local Y
}
private void Update()
@@ -70,13 +99,27 @@ public class TestScript : MonoBehaviour
}
if (_jumpAction.WasReleasedThisFrame()) {
_pump.AddForce(transform.up * 10, ForceMode.Impulse);
float t = Mathf.InverseLerp(0.15f, 0.63f, (float)GetDifference()); // gives 0 when 0.63, 1 when 0.15
float forceMultiplier = Mathf.Lerp(30, 5, t);
debugUi.UpdateForceMultiplier(forceMultiplier.ToString());
pump.AddForce(transform.up * forceMultiplier, ForceMode.Impulse);
_configurableJoint.yDrive = _jointDrive;
}
if (GetDifference() < _maxDifference) {
_maxDifference = GetDifference();
}
debugUi.UpdateVelocity(_rigidbody.linearVelocity.y.ToString());
debugUi.UpdateText($"Difference: {_maxDifference}");
debugUi.UpdateCurrentText($"Current difference: {GetDifference()}");
}
private void FixedUpdate()
{
_rigidbody.AddTorque(new Vector3(0, 0, _smoothX) * torqueForce, forceMode);
// inverted to apply correct direction
_rigidbody.AddTorque(new Vector3(0, 0, -_smoothX) * torqueForce, forceMode);
}
}

20
Assets/UI Document.uxml Normal file
View File

@@ -0,0 +1,20 @@
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:VisualElement style="flex-grow: 1; justify-content: center;">
<ui:VisualElement style="flex-grow: 0; flex-direction: row;">
<ui:Label text="Spring:" style="justify-content: center;" />
<ui:Label text="Label" name="springDifferenceLabel" style="justify-content: center;" />
</ui:VisualElement>
<ui:VisualElement style="flex-grow: 0; flex-direction: row;">
<ui:Label text="Diff:" style="justify-content: center;" />
<ui:Label text="Label" name="currentDifferenceLabel" style="justify-content: center;" />
</ui:VisualElement>
<ui:VisualElement style="flex-grow: 0; flex-direction: row;">
<ui:Label text="Force:" style="justify-content: center;" />
<ui:Label text="Label" name="forceMultiplier" style="justify-content: center;" />
</ui:VisualElement>
<ui:VisualElement style="flex-grow: 0; flex-direction: row;">
<ui:Label text="Velocity:" style="justify-content: center;" />
<ui:Label text="Label" name="velocityLabel" style="justify-content: center;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 340417dc6f0dc8a498146934d6f65e32
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}

8
Assets/UI Toolkit.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9289a2e5543bb6b4083b9c5409001319
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 19101, guid: 0000000000000000e000000000000000, type: 0}
m_Name: PanelSettings
m_EditorClassIdentifier: UnityEngine.dll::UnityEngine.UIElements.PanelSettings
themeUss: {fileID: -4733365628477956816, guid: 2d6793b382537824485aa9845d1e2247, type: 3}
m_DisableNoThemeWarning: 0
m_TargetTexture: {fileID: 0}
m_RenderMode: 0
m_ColliderUpdateMode: 0
m_ColliderIsTrigger: 1
m_ScaleMode: 1
m_ReferenceSpritePixelsPerUnit: 100
m_PixelsPerUnit: 100
m_Scale: 1
m_ReferenceDpi: 96
m_FallbackDpi: 96
m_ReferenceResolution: {x: 1200, y: 800}
m_ScreenMatchMode: 0
m_Match: 0
m_SortingOrder: 0
m_TargetDisplay: 0
m_BindingLogLevel: 0
m_ClearDepthStencil: 1
m_ClearColor: 0
m_ColorClearValue: {r: 0, g: 0, b: 0, a: 0}
m_VertexBudget: 0
m_DynamicAtlasSettings:
m_MinAtlasSize: 64
m_MaxAtlasSize: 4096
m_MaxSubTextureSize: 64
m_ActiveFilters: -1
m_AtlasBlitShader: {fileID: 9101, guid: 0000000000000000f000000000000000, type: 0}
m_RuntimeShader: {fileID: 9100, guid: 0000000000000000f000000000000000, type: 0}
m_RuntimeWorldShader: {fileID: 9102, guid: 0000000000000000f000000000000000, type: 0}
m_SDFShader: {fileID: 19011, guid: 0000000000000000f000000000000000, type: 0}
m_BitmapShader: {fileID: 9001, guid: 0000000000000000f000000000000000, type: 0}
m_SpriteShader: {fileID: 19012, guid: 0000000000000000f000000000000000, type: 0}
m_ICUDataAsset: {fileID: 0}
forceGammaRendering: 0
textSettings: {fileID: 0}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ecc77eee43430c049ba9c709b50e1317
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 37e39b8c7fdb4d649b398c8d2d4a127f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1 @@
@import url("unity-theme://default");

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2d6793b382537824485aa9845d1e2247
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12388, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0