Files
virtualclassroomdhbw/Assets/Scripts/Classroom/ExitFunction.cs
T
2024-10-29 17:16:17 +01:00

59 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//this script handles functionality if leaving the classroom scene
public class ExitFunction : NetworkBehaviour
{
UnityEngine.SceneManagement.Scene curScene;
private string sceneName;
void Start()
{
curScene = SceneManager.GetActiveScene();
sceneName = curScene.name;
GetComponent<Button>().onClick.AddListener(() =>
{
LeaveTheWorld();
});
}
//the rpcs are called if the host tries to leave the scene (try to kick every client out of the scene)
//clients need to be kicked because without a host the gameplay would not work anymore (work very limited)
[ServerRpc]
public void TryKickUserServerRPC()
{
Debug.Log("Server Site");
KickMeClientRPC();
}
[ClientRpc]
public void KickMeClientRPC()
{
Debug.Log("Client Site");
//NetworkManager.Singleton.Shutdown();
SceneManager.LoadScene("StartMenu");
}
private void LeaveTheWorld()
{
//check if "leaver" is a host --> clients must be also kicked
//or is a client --> can just leave without any impact to others
if(sceneName.Equals("ClassRoom") && PlayerPrefs.GetString("status").Equals("host"))
{
TryKickUserServerRPC();
NetworkManager.Singleton.Shutdown();
}
else if (sceneName.Equals("ClassRoom"))
{
NetworkManager.Singleton.Shutdown();
SceneManager.LoadScene("StartMenu");
}
}
}