54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
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();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|