Ive been trying to use unity networking for multiplayer development but im always getting the same error (have tried different APIs to connect).
I also cant connect to the services, but the package manager works just fine…weird
Have in mind that im using a VPN
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text.RegularExpressions;
using UnityEngine.Networking;
public class WorldTimeAPI : MonoBehaviour
{
#region Create Singleton
public static WorldTimeAPI instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(gameObject);
}
}
#endregion
struct TimeData
{
public string dateTime;
}
const string API_URL = "http://worldtimeapi.org/api/ip";
[HideInInspector] public bool IsTimeLoaded = false;
private DateTime currentDateTime = DateTime.Now;
private void Start()
{
StartCoroutine(GetRealDateTimeFromAPI());
}
public DateTime GetCurrentDateTime()
{
return currentDateTime.AddSeconds(Time.realtimeSinceStartup);
}
IEnumerator GetRealDateTimeFromAPI()
{
UnityWebRequest webRequest = UnityWebRequest.Get(API_URL);
Debug.Log("getting real datetime...");
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Error: " + webRequest.error);
}
else
{
TimeData timeData = JsonUtility.FromJson<TimeData>(webRequest.downloadHandler.text);
currentDateTime = ParseDateTime(timeData.dateTime);
IsTimeLoaded = true;
Debug.Log("Success");
}
DateTime ParseDateTime (string dateTime)
{
string date = Regex.Match(dateTime, @"^\d{4}-\d{2}-\d{2}").Value;
string time = Regex.Match(dateTime, @"\d{2}:\d{2}:\d{2}").Value;
return DateTime.Parse(string.Format("{0} {1}", date, time));
}
}
}