57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Text.RegularExpressions;
|
|
using System.Net.Sockets;
|
|
|
|
|
|
|
|
public class NetworkJoinFunction : MonoBehaviour
|
|
|
|
{
|
|
[SerializeField] private Button enterButton;
|
|
[SerializeField] private TMP_Text inputText;
|
|
|
|
[SerializeField] private TMP_Text infoText;
|
|
// Start is called before the first frame update
|
|
public void Start()
|
|
{
|
|
enterButton.onClick.AddListener(() =>
|
|
{
|
|
//check if there ip entered is valid
|
|
//for any reason the keyboard of blackwhale studios creates zero width whitespaces, this must be cleared!
|
|
//https://en.wikipedia.org/wiki/Zero-width_space
|
|
if (IsValidIPv4(inputText.text.Replace("\u200B", "")))
|
|
{
|
|
setNetworkData(inputText.text);
|
|
SceneManager.LoadScene("ClassRoom");
|
|
}
|
|
else
|
|
{
|
|
infoText.text = "Die Eingabe scheint keine valide IPv4-Adresse zu sein.\n Bitte �berpr�fe deine Eingabe und achte drauf, die Zieladresse wie folgt anzugeben \n 127.0.0.1";
|
|
}
|
|
});
|
|
}
|
|
public bool IsValidIPv4(string ipAddress)
|
|
{
|
|
//the pattern that a valid ipv4 must match
|
|
string pattern = @"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
|
Regex regex = new Regex(pattern);
|
|
|
|
return regex.IsMatch(ipAddress);
|
|
}
|
|
|
|
//just make the current client a client (no server/host etc.)
|
|
public void setNetworkData(string hostadress)
|
|
{
|
|
PlayerPrefs.SetString("hostadress", hostadress.Replace("\u200B", ""));
|
|
PlayerPrefs.SetString("status", "client");
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
|
|
}
|