Implement hand menu features
This commit is contained in:
@@ -26,6 +26,8 @@ public class GameManager : NetworkBehaviour
|
||||
[SerializeField]
|
||||
private bool autoConnectOrHost = true;
|
||||
|
||||
public bool IsMultiplayer => networkManager.IsHost || networkManager.IsClient;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
networkManager.OnClientConnectedCallback += OnClientConnectedCallback;
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Zenject;
|
||||
|
||||
@@ -36,13 +37,6 @@ public class SceneManager : NetworkBehaviour
|
||||
[ReadOnly]
|
||||
private string forgeSceneName;
|
||||
|
||||
//enum Scene
|
||||
//{
|
||||
// Lobby = 0,
|
||||
// Entrance = 1,
|
||||
// Forge = 2
|
||||
//}
|
||||
|
||||
[Inject]
|
||||
[ReadOnly]
|
||||
[SerializeField]
|
||||
@@ -56,10 +50,25 @@ public class SceneManager : NetworkBehaviour
|
||||
[SerializeField]
|
||||
private Scene loadedScene;
|
||||
|
||||
public Scene LoadedScene => loadedScene;
|
||||
|
||||
public bool IsInLobby => loadedScene.name == lobbySceneName;
|
||||
|
||||
public UnityEvent SceneLoaded;
|
||||
|
||||
private bool isMultiplayer => gameManager.IsMultiplayer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
loadedScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
|
||||
|
||||
Debug.Log("network spawn");
|
||||
|
||||
base.OnNetworkSpawn();
|
||||
}
|
||||
|
||||
@@ -76,6 +85,7 @@ public class SceneManager : NetworkBehaviour
|
||||
{
|
||||
// Keep track of the loaded scene, you need this to unload it
|
||||
loadedScene = sceneEvent.Scene;
|
||||
SceneLoaded.Invoke();
|
||||
}
|
||||
|
||||
Debug.Log($"Loaded the {sceneEvent.SceneName} scene on " +
|
||||
@@ -135,19 +145,31 @@ public class SceneManager : NetworkBehaviour
|
||||
[Button]
|
||||
public void SwitchToLobbyLevel()
|
||||
{
|
||||
StartCoroutine(SwitchToSceneMultiplayer(lobbySceneName));
|
||||
SwitchToScene(lobbySceneName);
|
||||
}
|
||||
|
||||
[Button]
|
||||
public void SwitchToEntranceLevel()
|
||||
{
|
||||
StartCoroutine(SwitchToSceneMultiplayer(entranceSceneName));
|
||||
SwitchToScene(entranceSceneName);
|
||||
}
|
||||
|
||||
[Button]
|
||||
public void SwitchToForgeLevel()
|
||||
{
|
||||
StartCoroutine(SwitchToSceneMultiplayer(forgeSceneName));
|
||||
SwitchToScene(forgeSceneName);
|
||||
}
|
||||
|
||||
private void SwitchToScene(string sceneName)
|
||||
{
|
||||
if (isMultiplayer)
|
||||
{
|
||||
StartCoroutine(SwitchToSceneMultiplayer(sceneName));
|
||||
}
|
||||
else
|
||||
{
|
||||
StartCoroutine(SwitchToSceneSolo(sceneName));
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SwitchToSceneMultiplayer(string scene)
|
||||
@@ -176,29 +198,27 @@ public class SceneManager : NetworkBehaviour
|
||||
//gameManager.LocalPlayer.FadeScreen(0, fadeDuration);
|
||||
}
|
||||
|
||||
//private IEnumerator SwitchToSceneSolo(Scene scene)
|
||||
//{
|
||||
// gameManager.LocalPlayer.FadeScreen(1, fadeDuration);
|
||||
private IEnumerator SwitchToSceneSolo(string scene)
|
||||
{
|
||||
gameManager.LocalPlayer.FadeScreen(1, fadeDuration);
|
||||
|
||||
// //NetworkManager.SceneManager.LoadScene()
|
||||
var operation = UnityEngine.SceneManagement.SceneManager
|
||||
.LoadSceneAsync(scene);
|
||||
|
||||
// var operation = UnityEngine.SceneManagement.SceneManager
|
||||
// .LoadSceneAsync((int)scene);
|
||||
operation.allowSceneActivation = false;
|
||||
|
||||
// operation.allowSceneActivation = false;
|
||||
|
||||
// float timer = 0;
|
||||
float timer = 0;
|
||||
|
||||
// while (timer <= fadeDuration && !operation.isDone)
|
||||
// {
|
||||
// timer += Time.deltaTime;
|
||||
// yield return null;
|
||||
// }
|
||||
while (timer <= fadeDuration && !operation.isDone)
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// operation.allowSceneActivation = true;
|
||||
operation.allowSceneActivation = true;
|
||||
|
||||
// this.scene = scene;
|
||||
gameManager.LocalPlayer.FadeScreen(0, fadeDuration);
|
||||
|
||||
// gameManager.LocalPlayer.FadeScreen(0, fadeDuration);
|
||||
//}
|
||||
loadedScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Zenject;
|
||||
|
||||
public class HandMenuUI : MonoBehaviour
|
||||
@@ -13,47 +14,147 @@ public class HandMenuUI : MonoBehaviour
|
||||
[ReadOnly]
|
||||
private HVRInputModule uiInput;
|
||||
|
||||
[Inject]
|
||||
[ReadOnly]
|
||||
private SceneManager sceneManager;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject anchor;
|
||||
|
||||
[SerializeField]
|
||||
[ReadOnly]
|
||||
private float forwardOffset = 0.1f;
|
||||
private Transform lookAt;
|
||||
|
||||
[SerializeField]
|
||||
[ReadOnly]
|
||||
private Canvas canvas;
|
||||
|
||||
private float smoothTime = 0.3F;
|
||||
|
||||
private Vector3 velocity = Vector3.zero;
|
||||
private float forwardOffset = 0.5f;
|
||||
|
||||
[SerializeField]
|
||||
private TextMeshProUGUI titleText;
|
||||
|
||||
[SerializeField]
|
||||
private TextMeshProUGUI roomCodeText;
|
||||
|
||||
[SerializeField]
|
||||
private Button restartButton;
|
||||
|
||||
[SerializeField]
|
||||
private Button quitButton;
|
||||
|
||||
[SerializeField]
|
||||
[ReadOnly]
|
||||
private TextMeshProUGUI quitButtonText;
|
||||
|
||||
[SerializeField]
|
||||
private Button closeButton;
|
||||
|
||||
[SerializeField]
|
||||
private Button settingsButton;
|
||||
|
||||
[SerializeField]
|
||||
private Button startButton;
|
||||
|
||||
[SerializeField]
|
||||
private Button backButton;
|
||||
|
||||
[SerializeField]
|
||||
[ReadOnly]
|
||||
private bool isSettingsVisible = false;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject mainMenu;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject settings;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
canvas = GetComponent<Canvas>();
|
||||
if (uiInput == null) return;
|
||||
|
||||
canvas = GetComponent<Canvas>();
|
||||
uiInput?.AddCanvas(canvas);
|
||||
|
||||
canvas.enabled = false;
|
||||
|
||||
quitButtonText = quitButton.GetComponentInChildren<TextMeshProUGUI>();
|
||||
|
||||
UpdateTextsAndButtons();
|
||||
|
||||
sceneManager.SceneLoaded.AddListener(() => UpdateTextsAndButtons());
|
||||
closeButton.onClick.AddListener(() => ToggleVisibility());
|
||||
quitButton.onClick.AddListener(() => QuitClicked());
|
||||
backButton.onClick.AddListener(() => BackClicked());
|
||||
settingsButton.onClick.AddListener(() => SettingsClicked());
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdatePosition();
|
||||
//UpdatePosition();
|
||||
CheckInput();
|
||||
}
|
||||
|
||||
private void UpdatePosition()
|
||||
{
|
||||
var rotation = Quaternion.Euler(0, anchor.transform.eulerAngles.y - 180, 0);
|
||||
var targetPosition = anchor.transform.position - anchor.transform.forward * forwardOffset;
|
||||
var smoothPosition = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
|
||||
transform.SetPositionAndRotation(smoothPosition, rotation);
|
||||
var targetPosition = lookAt.position + (lookAt.forward * forwardOffset);
|
||||
transform.position = new Vector3(targetPosition.x, lookAt.position.y, targetPosition.z);
|
||||
|
||||
var lookAtPositon = new Vector3(lookAt.position.x, transform.position.y, lookAt.position.z);
|
||||
transform.rotation = Quaternion.LookRotation(transform.position - lookAtPositon);
|
||||
}
|
||||
|
||||
private void CheckInput()
|
||||
{
|
||||
if (HVRInputManager.Instance.LeftController.PrimaryButtonState.JustActivated)
|
||||
{
|
||||
canvas.enabled = !canvas.enabled;
|
||||
ToggleVisibility();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTextsAndButtons()
|
||||
{
|
||||
titleText.text = sceneManager.LoadedScene.name;
|
||||
quitButtonText.text = sceneManager.IsInLobby ? "Quit" : "Give Up";
|
||||
backButton.gameObject.SetActive(isSettingsVisible);
|
||||
mainMenu.SetActive(!isSettingsVisible);
|
||||
settings.SetActive(isSettingsVisible);
|
||||
restartButton.gameObject.SetActive(!sceneManager.IsInLobby);
|
||||
startButton.gameObject.SetActive(!sceneManager.IsInLobby);
|
||||
}
|
||||
|
||||
private void ToggleVisibility()
|
||||
{
|
||||
canvas.enabled = !canvas.enabled;
|
||||
|
||||
if (canvas.enabled)
|
||||
{
|
||||
UpdatePosition();
|
||||
}
|
||||
}
|
||||
|
||||
private void BackClicked()
|
||||
{
|
||||
isSettingsVisible = false;
|
||||
UpdateTextsAndButtons();
|
||||
}
|
||||
|
||||
private void SettingsClicked()
|
||||
{
|
||||
isSettingsVisible = true;
|
||||
UpdateTextsAndButtons();
|
||||
}
|
||||
|
||||
private void QuitClicked()
|
||||
{
|
||||
if (sceneManager.IsInLobby)
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneManager.SwitchToLobbyLevel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user