This commit is contained in:
2026-04-29 02:31:15 +07:00
parent 21c999a904
commit ed86fface3
12 changed files with 433 additions and 79 deletions

View File

@@ -61,11 +61,32 @@ namespace Hallucinate.UI
private float _trailOpacity = 1f;
private bool _isSettingsOpen = false;
private const string UI_SCALE_KEY = "UIScale";
private void Awake()
{
Instance = this;
_uiDocument = GetComponent<UIDocument>();
UnityEngine.Cursor.visible = false;
ApplySavedUIScale();
}
public void SetUIScale(float scale)
{
if (_uiDocument == null || _uiDocument.panelSettings == null) return;
// Unity UI Toolkit dùng panelSettings để điều khiển tỉ lệ
// Chúng ta thay đổi scale multiplier
_uiDocument.panelSettings.scale = scale;
PlayerPrefs.SetFloat(UI_SCALE_KEY, scale);
PlayerPrefs.Save();
}
private void ApplySavedUIScale()
{
float savedScale = PlayerPrefs.GetFloat(UI_SCALE_KEY, 1.0f);
SetUIScale(savedScale);
}
private void Start()
@@ -213,6 +234,13 @@ namespace Hallucinate.UI
for (int i = 0; i < trailLength * trailSpacing + 1; i++) _posHistory.Add(startPos);
}
private float GetCurrentScale()
{
if (_uiDocument != null && _uiDocument.panelSettings != null)
return _uiDocument.panelSettings.scale;
return 1.0f;
}
private void OnGlobalClick(PointerDownEvent evt)
{
if (!enableRipples || _cursorLayer == null) return;
@@ -238,6 +266,7 @@ namespace Hallucinate.UI
ripple.style.borderLeftWidth = 2;
ripple.style.borderRightWidth = 2;
// PointerDownEvent.localPosition đã được Unity tự động scale theo Panel
ripple.style.left = evt.localPosition.x;
ripple.style.top = evt.localPosition.y;
ripple.pickingMode = PickingMode.Ignore;
@@ -271,7 +300,10 @@ namespace Hallucinate.UI
}
_cursorLayer.style.display = DisplayStyle.Flex;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - mousePos.y);
// QUAN TRỌNG: Chia tọa độ pixel cho scale của UI để có tọa độ local chính xác
float scale = GetCurrentScale();
Vector2 uiPos = new Vector2(mousePos.x / scale, (Screen.height - mousePos.y) / scale);
float mouseSpeed = Vector2.Distance(uiPos, _lastMousePos);
_lastMousePos = uiPos;