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

61 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Unity.Netcode;
using UnityEngine.SceneManagement;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using System;
using System.Net.Sockets;
//this scripts is the startpoint of the classroom scene, it starts the host or creates the connection to it
public class onStart : NetworkBehaviour
{
// Start is called before the first frame update
private float starttime;
[SerializeField] private UnityTransport hostIPAdress;
//stores if the connection establishment check has already been performed
private bool roomCheck = true;
void Start()
{
//value to measure the time since connection attempt (for timeout)
starttime = Time.time;
if (PlayerPrefs.GetString("status") == "client")
{
try
{
hostIPAdress.ConnectionData.Address = PlayerPrefs.GetString("hostadress");
NetworkManager.Singleton.StartClient();
}
catch (NullReferenceException)
{
//on error load the room creattion scene
SceneManager.LoadScene("RoomCreation");
}
}
else if (PlayerPrefs.GetString("status") == "host")
{
//set address to 0.0.0.0 to accept connection from all networks
hostIPAdress.ConnectionData.Address = "0.0.0.0";
NetworkManager.Singleton.StartHost();
}
}
public void Update()
{
//UnityEngine.Debug.Log((Time.time - starttime));
if ((Time.time - starttime) > 15 && roomCheck)
{
roomCheck = false;
//if there is no camera in the scene, the network join was not successfull XD
if ( !(GameObject.Find("Main Camera")))
{
SceneManager.LoadScene("RoomCreation");
}
}
}
}