Update
This commit is contained in:
@@ -47,36 +47,15 @@ namespace OnlyScove.Scripts
|
||||
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
||||
|
||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Runner != null && Runner.IsRunning && Object != null)
|
||||
return NetworkedCameraRotation;
|
||||
|
||||
if (Cam != null)
|
||||
return Cam.PlanarRotation;
|
||||
|
||||
return transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
[Networked] public Vector2 NetworkedMoveInput { get; set; }
|
||||
[Networked] public float NetworkedSpeed { get; set; }
|
||||
[Networked] public Vector3 NetworkedPosition { get; set; }
|
||||
|
||||
[Header("Player Stats")]
|
||||
[Networked, OnChangedRender(nameof(OnHealthChangedRender))]
|
||||
public float Health { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnHealthChangedRender))] public float Health { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))] public float Stamina { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))] public float NoiseLevel { get; set; } = 0f;
|
||||
|
||||
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))]
|
||||
public float Stamina { get; set; } = 100f;
|
||||
|
||||
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))]
|
||||
public float NoiseLevel { get; set; } = 0f;
|
||||
|
||||
// Sự kiện để UI lắng nghe
|
||||
public event System.Action<float> OnHealthChanged;
|
||||
public event System.Action<float> OnStaminaChanged;
|
||||
public event System.Action<float> OnNoiseLevelChanged;
|
||||
@@ -87,19 +66,26 @@ namespace OnlyScove.Scripts
|
||||
public float VelocityY { get; set; }
|
||||
public bool IsGrounded { get; private set; }
|
||||
public bool WasGrounded { get; private set; }
|
||||
|
||||
private List<IInteractable> interactablesNearby = new List<IInteractable>();
|
||||
private int currentInteractableIndex = 0;
|
||||
|
||||
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
|
||||
|
||||
public static PlayerStateMachine Local { get; private set; }
|
||||
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Runner != null && Runner.IsRunning && Object != null) return NetworkedCameraRotation;
|
||||
return Cam != null ? Cam.PlanarRotation : transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerBaseState currentState;
|
||||
private bool hasControl = true;
|
||||
private bool hasSpeedParam;
|
||||
private bool hasVelocityXParam;
|
||||
private bool hasVelocityZParam;
|
||||
private List<IInteractable> interactablesNearby = new List<IInteractable>();
|
||||
private int currentInteractableIndex = 0;
|
||||
private float localAnimatorSpeed;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
@@ -125,41 +111,30 @@ namespace OnlyScove.Scripts
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
InitializePlayer();
|
||||
}
|
||||
if (Runner == null || !Runner.IsRunning) InitializePlayer();
|
||||
}
|
||||
|
||||
public override void Spawned()
|
||||
{
|
||||
InitializePlayer();
|
||||
|
||||
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
|
||||
{
|
||||
if (Controller != null) Controller.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Callbacks từ attribute OnChangedRender
|
||||
void OnHealthChangedRender() => OnHealthChanged?.Invoke(Health);
|
||||
void OnStaminaChangedRender() => OnStaminaChanged?.Invoke(Stamina);
|
||||
void OnNoiseLevelChangedRender() => OnNoiseLevelChanged?.Invoke(NoiseLevel);
|
||||
|
||||
private void InitializePlayer()
|
||||
{
|
||||
if (currentState == null)
|
||||
{
|
||||
SwitchState(new PlayerIdleState(this));
|
||||
}
|
||||
if (currentState == null) SwitchState(new PlayerIdleState(this));
|
||||
|
||||
bool isOffline = Runner == null || !Runner.IsRunning;
|
||||
bool hasAuthority = Object != null && Object.HasInputAuthority;
|
||||
|
||||
if (isOffline || hasAuthority)
|
||||
if (isOffline || (Object != null && Object.HasInputAuthority))
|
||||
{
|
||||
Local = this;
|
||||
|
||||
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
|
||||
if (cameraController != null)
|
||||
{
|
||||
@@ -167,26 +142,17 @@ namespace OnlyScove.Scripts
|
||||
Cam.followTarget = transform;
|
||||
Cam.inputReader = Input;
|
||||
}
|
||||
|
||||
Input.OnNextInteractEvent += OnNextInteract;
|
||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||
|
||||
if (Controller != null) Controller.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private float localAnimatorSpeed;
|
||||
|
||||
public void Rotate(Vector3 moveDirection, float deltaTime)
|
||||
{
|
||||
if (moveDirection == Vector3.zero) return;
|
||||
|
||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
||||
transform.rotation = Quaternion.RotateTowards(
|
||||
transform.rotation,
|
||||
targetRot,
|
||||
RotationSpeed * deltaTime
|
||||
);
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, RotationSpeed * deltaTime);
|
||||
}
|
||||
|
||||
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
|
||||
@@ -197,40 +163,23 @@ namespace OnlyScove.Scripts
|
||||
if (Controller != null && Controller.enabled)
|
||||
{
|
||||
Controller.Move(velocity * deltaTime);
|
||||
if (Object != null && Runner != null && Runner.IsRunning)
|
||||
{
|
||||
NetworkedPosition = transform.position;
|
||||
}
|
||||
if (Object != null && Runner != null && Runner.IsRunning) NetworkedPosition = transform.position;
|
||||
}
|
||||
|
||||
localAnimatorSpeed = animatorSpeed;
|
||||
|
||||
if (Object != null && Object.HasStateAuthority)
|
||||
{
|
||||
NetworkedSpeed = animatorSpeed;
|
||||
NetworkedMoveInput = MoveInput;
|
||||
}
|
||||
|
||||
UpdateAnimator(deltaTime);
|
||||
}
|
||||
|
||||
private void UpdateAnimator(float deltaTime)
|
||||
{
|
||||
if (Anim == null) return;
|
||||
|
||||
float speedValue;
|
||||
Vector2 inputVector;
|
||||
|
||||
if (Runner == null || !Runner.IsRunning || Object.HasInputAuthority)
|
||||
{
|
||||
speedValue = localAnimatorSpeed;
|
||||
inputVector = MoveInput;
|
||||
}
|
||||
else
|
||||
{
|
||||
speedValue = NetworkedSpeed;
|
||||
inputVector = NetworkedMoveInput;
|
||||
}
|
||||
float speedValue = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? localAnimatorSpeed : NetworkedSpeed;
|
||||
Vector2 inputVector = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? MoveInput : NetworkedMoveInput;
|
||||
|
||||
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
|
||||
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
|
||||
@@ -242,14 +191,11 @@ namespace OnlyScove.Scripts
|
||||
bool isRunning = Runner != null && Runner.IsRunning;
|
||||
if (Object == null && isRunning) return;
|
||||
|
||||
if (isRunning && NetworkedPosition != Vector3.zero)
|
||||
if (isRunning && NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
|
||||
{
|
||||
if (Controller != null && !Object.HasInputAuthority)
|
||||
{
|
||||
Controller.enabled = false;
|
||||
transform.position = NetworkedPosition;
|
||||
Controller.enabled = true;
|
||||
}
|
||||
Controller.enabled = false;
|
||||
transform.position = NetworkedPosition;
|
||||
Controller.enabled = true;
|
||||
}
|
||||
|
||||
if (GetInput(out PlayerInputData data))
|
||||
@@ -269,52 +215,35 @@ namespace OnlyScove.Scripts
|
||||
IsSprintHeld = false;
|
||||
}
|
||||
|
||||
bool isSimulating = !isRunning || Object.HasInputAuthority || Runner.IsServer;
|
||||
|
||||
if (!isSimulating)
|
||||
if (!isRunning || Object.HasInputAuthority || Runner.IsServer)
|
||||
{
|
||||
if (hasControl)
|
||||
{
|
||||
WasGrounded = IsGrounded;
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
currentState?.Tick(isRunning ? Runner.DeltaTime : Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateAnimator(Runner.DeltaTime);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasControl) return;
|
||||
|
||||
WasGrounded = IsGrounded;
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
|
||||
float dt = isRunning ? Runner.DeltaTime : Time.fixedDeltaTime;
|
||||
currentState?.Tick(dt);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
FixedUpdateNetwork();
|
||||
}
|
||||
if (Runner == null || !Runner.IsRunning) FixedUpdateNetwork();
|
||||
}
|
||||
|
||||
private void CheckGround()
|
||||
{
|
||||
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
||||
}
|
||||
private void CheckGround() => IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
||||
|
||||
private void UpdateInteractablesList()
|
||||
{
|
||||
interactablesNearby.Clear();
|
||||
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
interactablesNearby.Add(target);
|
||||
OnInteractableTargetChanged?.Invoke(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnInteractableTargetChanged?.Invoke(null);
|
||||
}
|
||||
|
||||
if (target != null) interactablesNearby.Add(target);
|
||||
OnInteractableTargetChanged?.Invoke(target);
|
||||
currentInteractableIndex = 0;
|
||||
}
|
||||
|
||||
@@ -328,22 +257,13 @@ namespace OnlyScove.Scripts
|
||||
private void OnPreviousInteract()
|
||||
{
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex--;
|
||||
if (currentInteractableIndex < 0) currentInteractableIndex = interactablesNearby.Count - 1;
|
||||
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
|
||||
OnInteractableTargetChanged?.Invoke(GetInteractable());
|
||||
}
|
||||
|
||||
public IInteractable GetInteractable()
|
||||
{
|
||||
if (interactablesNearby.Count == 0) return null;
|
||||
return interactablesNearby[currentInteractableIndex];
|
||||
}
|
||||
public IInteractable GetInteractable() => interactablesNearby.Count == 0 ? null : interactablesNearby[currentInteractableIndex];
|
||||
|
||||
public void SetGroundCheck(float radius, Vector3 offset)
|
||||
{
|
||||
GroundCheckRadius = radius;
|
||||
GroundCheckOffset = offset;
|
||||
}
|
||||
public void SetGroundCheck(float radius, Vector3 offset) { GroundCheckRadius = radius; GroundCheckOffset = offset; }
|
||||
|
||||
public void SwitchState(PlayerBaseState newState)
|
||||
{
|
||||
|
||||
@@ -23,7 +23,6 @@ namespace UI
|
||||
|
||||
var root = hudDocument.rootVisualElement;
|
||||
|
||||
// Tìm các thành phần UI theo Name (Bạn cần đặt tên này trong UXML)
|
||||
_healthFill = root.Q<VisualElement>("health-fill");
|
||||
_staminaFill = root.Q<VisualElement>("stamina-fill");
|
||||
_healthText = root.Q<Label>("health-text");
|
||||
|
||||
50
Assets/Scripts/UI/LobbyController.cs
Normal file
50
Assets/Scripts/UI/LobbyController.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LobbyController : MonoBehaviour
|
||||
{
|
||||
private UIDocument _doc;
|
||||
private Button _btnLeave;
|
||||
private Button _btnStart;
|
||||
private ScrollView _playerList;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_doc = GetComponent<UIDocument>();
|
||||
var root = _doc.rootVisualElement;
|
||||
|
||||
// BINDING: Tìm element theo tên (Name) đã đặt trong UXML
|
||||
_btnLeave = root.Q<Button>("btn-leave");
|
||||
_btnStart = root.Q<Button>("btn-start");
|
||||
_playerList = root.Q<ScrollView>("player-list");
|
||||
|
||||
// ĐĂNG KÝ SỰ KIỆN:
|
||||
if (_btnLeave != null)
|
||||
_btnLeave.clicked += OnLeaveClicked;
|
||||
|
||||
if (_btnStart != null)
|
||||
_btnStart.clicked += OnStartClicked;
|
||||
}
|
||||
|
||||
private void OnLeaveClicked()
|
||||
{
|
||||
Debug.Log("User clicked LEAVE room!");
|
||||
UIManager.Instance.ShowScreen("MainMenu");
|
||||
}
|
||||
|
||||
private void OnStartClicked()
|
||||
{
|
||||
Debug.Log("Host clicked START GAME!");
|
||||
UIManager.Instance.ShowScreen("HUD"); // Chuyển vào HUD khi game bắt đầu
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// Hủy đăng ký để tránh memory leak
|
||||
if (_btnLeave != null) _btnLeave.clicked -= OnLeaveClicked;
|
||||
if (_btnStart != null) _btnStart.clicked -= OnStartClicked;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/LobbyController.cs.meta
Normal file
2
Assets/Scripts/UI/LobbyController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c37c552a9c18a242bcc8860a0a5212f
|
||||
69
Assets/Scripts/UI/LocalizationManager.cs
Normal file
69
Assets/Scripts/UI/LocalizationManager.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LocalizationManager : MonoBehaviour
|
||||
{
|
||||
public static LocalizationManager Instance { get; private set; }
|
||||
|
||||
private Dictionary<string, string> _localizedText;
|
||||
private string _currentLanguage = "en";
|
||||
|
||||
public event Action OnLanguageChanged;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
LoadLanguage(_currentLanguage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadLanguage(string langCode)
|
||||
{
|
||||
TextAsset targetFile = Resources.Load<TextAsset>($"Localization/{langCode}");
|
||||
if (targetFile != null)
|
||||
{
|
||||
// Simple JSON parsing (For production, consider using a proper JSON library like Newtonsoft)
|
||||
string json = targetFile.text;
|
||||
_localizedText = ParseJson(json);
|
||||
_currentLanguage = langCode;
|
||||
OnLanguageChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public string Get(string key)
|
||||
{
|
||||
if (_localizedText != null && _localizedText.ContainsKey(key))
|
||||
return _localizedText[key];
|
||||
return $"[{key}]";
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ParseJson(string json)
|
||||
{
|
||||
// Dummy parser for demonstration, replace with JsonUtility if using wrapper class
|
||||
// or Newtonsoft for direct dictionary parsing
|
||||
var dict = new Dictionary<string, string>();
|
||||
string[] lines = json.Split(new[] { ',', '{', '}', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
string[] parts = line.Split(':');
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string key = parts[0].Trim(' ', '"');
|
||||
string val = parts[1].Trim(' ', '"');
|
||||
dict[key] = val;
|
||||
}
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/LocalizationManager.cs.meta
Normal file
2
Assets/Scripts/UI/LocalizationManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c17a3f09ee49ff48a0e3e2b45080257
|
||||
23
Assets/Scripts/UI/MainMenuController.cs
Normal file
23
Assets/Scripts/UI/MainMenuController.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class MainMenuController : MonoBehaviour
|
||||
{
|
||||
private UIDocument _doc;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_doc = GetComponent<UIDocument>();
|
||||
var root = _doc.rootVisualElement;
|
||||
|
||||
// Nối dây các nút bấm
|
||||
root.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby"));
|
||||
root.Q<Button>("btn-join")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby"));
|
||||
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ToggleSettings());
|
||||
root.Q<Button>("btn-profile")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Profile"));
|
||||
root.Q<Button>("btn-exit")?.RegisterCallback<ClickEvent>(ev => Application.Quit());
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/MainMenuController.cs.meta
Normal file
2
Assets/Scripts/UI/MainMenuController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 691980524acfc544f9660cfc35ce3616
|
||||
14
Assets/Scripts/UI/ProfileController.cs
Normal file
14
Assets/Scripts/UI/ProfileController.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class ProfileController : MonoBehaviour
|
||||
{
|
||||
private void OnEnable()
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("MainMenu"));
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/ProfileController.cs.meta
Normal file
2
Assets/Scripts/UI/ProfileController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdea16b110511ef45889ed832b63560b
|
||||
110
Assets/Scripts/UI/SettingsController.cs
Normal file
110
Assets/Scripts/UI/SettingsController.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using OnlyScove.Scripts;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class SettingsController : MonoBehaviour
|
||||
{
|
||||
private UIDocument _doc;
|
||||
private CameraController _cameraController;
|
||||
|
||||
// Tabs Content
|
||||
private VisualElement _contentGeneral, _contentGraphics, _contentAudio;
|
||||
private Button _tabGeneral, _tabGraphics, _tabAudio, _tabControls;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_doc = GetComponent<UIDocument>();
|
||||
_cameraController = Object.FindFirstObjectByType<CameraController>();
|
||||
|
||||
var root = _doc.rootVisualElement;
|
||||
|
||||
// Query Tabs
|
||||
_tabGeneral = root.Q<Button>("tab-general");
|
||||
_tabGraphics = root.Q<Button>("tab-graphics");
|
||||
_tabAudio = root.Q<Button>("tab-audio");
|
||||
_tabControls = root.Q<Button>("tab-controls");
|
||||
|
||||
// Query Content
|
||||
_contentGeneral = root.Q<VisualElement>("content-general");
|
||||
_contentGraphics = root.Q<VisualElement>("content-graphics");
|
||||
_contentAudio = root.Q<VisualElement>("content-audio");
|
||||
|
||||
// Events
|
||||
_tabGeneral.clicked += () => SwitchTab(_contentGeneral, _tabGeneral);
|
||||
_tabGraphics.clicked += () => SwitchTab(_contentGraphics, _tabGraphics);
|
||||
_tabAudio.clicked += () => SwitchTab(_contentAudio, _tabAudio);
|
||||
|
||||
root.Q<Button>("btn-back").clicked += () => UIManager.Instance.ToggleSettings();
|
||||
|
||||
// Camera Binding (FOV)
|
||||
var fovSlider = root.Q<Slider>("setting-fov");
|
||||
if (fovSlider != null)
|
||||
{
|
||||
fovSlider.RegisterValueChangedCallback(evt => {
|
||||
// Cần expose hoặc tạo hàm SetFOV trong CameraController
|
||||
Debug.Log($"Setting FOV to: {evt.newValue}");
|
||||
});
|
||||
}
|
||||
|
||||
// Language Binding
|
||||
var langDropdown = root.Q<DropdownField>("setting-language");
|
||||
if (langDropdown != null)
|
||||
{
|
||||
langDropdown.RegisterValueChangedCallback(evt => {
|
||||
string code = evt.newValue == "English" ? "en" : "vi";
|
||||
LocalizationManager.Instance.LoadLanguage(code);
|
||||
});
|
||||
}
|
||||
|
||||
// Lắng nghe sự kiện đổi ngôn ngữ để cập nhật Text
|
||||
if (LocalizationManager.Instance != null)
|
||||
{
|
||||
LocalizationManager.Instance.OnLanguageChanged += UpdateTexts;
|
||||
UpdateTexts();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[SettingsController] LocalizationManager Instance not found in scene!");
|
||||
}
|
||||
}
|
||||
|
||||
private void SwitchTab(VisualElement targetContent, Button targetTab)
|
||||
{
|
||||
if (targetContent == null || targetTab == null) return;
|
||||
|
||||
// Ẩn tất cả (Thêm null check)
|
||||
if (_contentGeneral != null) _contentGeneral.style.display = DisplayStyle.None;
|
||||
if (_contentGraphics != null) _contentGraphics.style.display = DisplayStyle.None;
|
||||
if (_contentAudio != null) _contentAudio.style.display = DisplayStyle.None;
|
||||
|
||||
if (_tabGeneral != null) _tabGeneral.RemoveFromClassList("active-tab");
|
||||
if (_tabGraphics != null) _tabGraphics.RemoveFromClassList("active-tab");
|
||||
if (_tabAudio != null) _tabAudio.RemoveFromClassList("active-tab");
|
||||
|
||||
// Hiện cái được chọn
|
||||
targetContent.style.display = DisplayStyle.Flex;
|
||||
targetTab.AddToClassList("active-tab");
|
||||
}
|
||||
|
||||
private void UpdateTexts()
|
||||
{
|
||||
if (LocalizationManager.Instance == null) return;
|
||||
|
||||
var root = _doc.rootVisualElement;
|
||||
|
||||
// Dùng null-conditional operator (?.) để cực kỳ an toàn
|
||||
var titleLabel = root.Q<Label>("title");
|
||||
if (titleLabel != null) titleLabel.text = LocalizationManager.Instance.Get("settings_title");
|
||||
|
||||
if (_tabGeneral != null) _tabGeneral.text = LocalizationManager.Instance.Get("settings_general");
|
||||
if (_tabGraphics != null) _tabGraphics.text = LocalizationManager.Instance.Get("settings_graphics");
|
||||
if (_tabAudio != null) _tabAudio.text = LocalizationManager.Instance.Get("settings_audio");
|
||||
if (_tabControls != null) _tabControls.text = LocalizationManager.Instance.Get("settings_controls");
|
||||
|
||||
var btnBack = root.Q<Button>("btn-back");
|
||||
if (btnBack != null) btnBack.text = LocalizationManager.Instance.Get("settings_back");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/SettingsController.cs.meta
Normal file
2
Assets/Scripts/UI/SettingsController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5534bcf4869df944883c6fd2a17a6a5a
|
||||
@@ -1,92 +1,144 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
[ExecuteAlways]
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public static UIManager Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class ScreenData
|
||||
{
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isActive;
|
||||
public bool isActive; // Thêm lại để tương thích với Editor gốc
|
||||
public bool isOverlay;
|
||||
public Texture2D customCursor;
|
||||
}
|
||||
|
||||
[Header("Screens Management")]
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
|
||||
[Header("Live Preview (Editor Only)")]
|
||||
[Range(0, 1)] public float globalOpacity = 1f;
|
||||
[Header("Default Settings")]
|
||||
public Texture2D defaultCursor;
|
||||
|
||||
private VisualElement _currentScreenRoot;
|
||||
private string _currentScreenName;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
SyncScreens(); // Đồng bộ trạng thái ban đầu
|
||||
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
if (screen.document != null && screen.screenName == "Settings")
|
||||
screen.document.sortingOrder = 999;
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
ShowScreen("MainMenu");
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
// Tự động cập nhật giao diện ngay khi thay đổi thông số trong Inspector (không cần Play)
|
||||
SyncScreens();
|
||||
// Chạy trong Editor để cập nhật UI ngay lập tức khi check vào ô isActive
|
||||
if (!Application.isPlaying) SyncScreens();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
private void Update()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
SetupEvents();
|
||||
ToggleSettings();
|
||||
}
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
public void SyncScreens()
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
if (screen.document == null) continue;
|
||||
|
||||
var root = screen.document.rootVisualElement;
|
||||
if (root == null) continue;
|
||||
|
||||
// Bật tắt display dựa trên biến isActive
|
||||
root.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
root.style.opacity = globalOpacity;
|
||||
if (screen != null && screen.document != null && screen.document.rootVisualElement != null)
|
||||
{
|
||||
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Hàm tiện ích để bật duy nhất 1 màn hình từ Code hoặc Button
|
||||
public void ShowOnly(string name)
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
ShowScreen(name);
|
||||
}
|
||||
|
||||
public void ShowScreen(string name)
|
||||
{
|
||||
if (_currentScreenName == name) return;
|
||||
StartCoroutine(TransitionRoutine(name));
|
||||
}
|
||||
|
||||
private IEnumerator TransitionRoutine(string nextScreenName)
|
||||
{
|
||||
// Tắt các màn hình khác
|
||||
foreach (var s in screens)
|
||||
{
|
||||
screen.isActive = (screen.screenName == name);
|
||||
if (s.screenName != nextScreenName && !s.isOverlay && s.isActive)
|
||||
{
|
||||
var root = s.document.rootVisualElement.Q<VisualElement>();
|
||||
root.AddToClassList("hidden");
|
||||
s.isActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
private void SetupEvents()
|
||||
{
|
||||
// Logic đăng ký event thông minh ở đây (tương tự như trước nhưng linh hoạt hơn)
|
||||
var mainMenu = GetDocument("MainMenu");
|
||||
if (mainMenu != null)
|
||||
// Bật màn hình mới
|
||||
var nextData = screens.Find(s => s.screenName == nextScreenName);
|
||||
if (nextData != null && nextData.document != null)
|
||||
{
|
||||
mainMenu.rootVisualElement.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(e => ShowOnly("Settings"));
|
||||
mainMenu.rootVisualElement.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(e => ShowOnly("Lobby"));
|
||||
nextData.isActive = true;
|
||||
_currentScreenRoot = nextData.document.rootVisualElement.Q<VisualElement>();
|
||||
_currentScreenName = nextScreenName;
|
||||
|
||||
_currentScreenRoot.style.display = DisplayStyle.Flex;
|
||||
_currentScreenRoot.AddToClassList("hidden");
|
||||
|
||||
yield return null;
|
||||
_currentScreenRoot.RemoveFromClassList("hidden");
|
||||
|
||||
ApplyCursor(nextData.customCursor != null ? nextData.customCursor : defaultCursor);
|
||||
}
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
{
|
||||
var settingsData = screens.Find(s => s.screenName == "Settings");
|
||||
if (settingsData == null) return;
|
||||
|
||||
settingsData.isActive = !settingsData.isActive;
|
||||
settingsData.document.rootVisualElement.style.display = settingsData.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
var settings = GetDocument("Settings");
|
||||
settings?.rootVisualElement.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(e => ShowOnly("MainMenu"));
|
||||
if (settingsData.isActive)
|
||||
{
|
||||
UnityEngine.Cursor.visible = true;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
||||
ApplyCursor(settingsData.customCursor != null ? settingsData.customCursor : defaultCursor);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
}
|
||||
|
||||
public UIDocument GetDocument(string name)
|
||||
private void ApplyCursor(Texture2D texture)
|
||||
{
|
||||
return screens.Find(s => s.screenName == name)?.document;
|
||||
}
|
||||
|
||||
// Thêm hàm để Update Text/Image nhanh từ Inspector hoặc Script khác
|
||||
public void SetElementText(string screenName, string elementName, string newText)
|
||||
{
|
||||
var doc = GetDocument(screenName);
|
||||
var label = doc?.rootVisualElement.Q<Label>(elementName);
|
||||
if (label != null) label.text = newText;
|
||||
UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user