XRoom_Unity/Assets/InternetChecker.cs
2025-05-31 10:20:20 +03:30

54 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;
public class InternetChecker : MonoBehaviour
{
/// <summary>
/// public GameObject statusText; // متنی برای نمایش خطا، در UI قرار دهید
/// </summary>
private void Start()
{
StartCoroutine(CheckInternetRoutine());
}
IEnumerator CheckInternetRoutine()
{
while (true)
{
yield return CheckConnection();
yield return new WaitForSeconds(5f); // هر ۵ ثانیه یکبار بررسی شود
}
}
IEnumerator CheckConnection()
{
UnityWebRequest request = new UnityWebRequest("https://www.google.com");
request.method = UnityWebRequest.kHttpVerbHEAD;
print("ERROR");
yield return request.SendWebRequest();
#if UNITY_2020_1_OR_NEWER
bool hasError = request.result != UnityWebRequest.Result.Success;
#else
bool hasError = request.isNetworkError || request.isHttpError;
#endif
if (hasError)
{
EN.instance.showError("اتصال ناموفق بود", "متأسفانه نتوانستیم به سرور متصل شویم. لطفاً بررسی کنید که به اینترنت متصل هستید.\r\n\r\nاگر قصد دارید از طریق فایروال اتصال برقرار کنید یا در شبکه داخلی هستید، لطفاً با پشتیبانی به آدرس ایمیل support@dadechin.com تماس بگیرید.");
}
else
{
//HideError();
}
}
}