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

184 lines
6.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ENMenu : MonoBehaviour
{
public GameObject itemPr;
public Transform menu;
public List<GameObject> items = new List<GameObject>();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Invoke("PlayWelcome", delay);
//for (int i = 0; i < EN.instance.users.Count; i++)
//{
//GameObject select= Instantiate(itemPr,menu);
// select.gameObject.SetActive(true);
// select.transform.Find("number").GetComponent<Text>().text = "."+i;
// select.transform.Find("name").GetComponent<RTLTextMeshPro>().text = EN.instance.users[i].name;
// select.transform.Find("job").GetComponent<RTLTextMeshPro>().text = EN.instance.users[i].job;
// select.name = i.ToString();
// items.Add(select);
//}
EN.instance.SelectedUser = EN.instance.users[1];
string user = PlayerPrefs.GetString("user","");
if (!string.IsNullOrEmpty(user))
{
User response = new User(user);
phone.text= PlayerPrefs.GetString("mobile_number", "");
code.text= PlayerPrefs.GetString("password","");
var data = new Dictionary<string, string>
{
{ "password",code.text.ToString() },
{ "mobile_number",phone.text.ToString() }
};
StartCoroutine(webservice.instance.LoginRequest(data, OnLoginSuccess));
//OnLoginSuccess(response);
//LoginButton.SetActive(false);
//GuestButton.SetActive(false);
//ExitButton.SetActive(true);
//EnterButton.SetActive(true);
}
else
{
LoginButton.SetActive(true);
GuestButton.SetActive(true);
ExitButton.SetActive(false);
EnterButton.SetActive(false);
}
}
public void exitAccount()
{
LoginButton.SetActive(true);
GuestButton.SetActive(true);
ExitButton.SetActive(false);
EnterButton.SetActive(false);
PlayerPrefs.SetString("user", "");
PlayerPrefs.SetString("mobile_number", "");
PlayerPrefs.SetString("password", "");
}
public GameObject tablet;
public void Login()
{
LoginMenu.gameObject.SetActive(false);
loading.gameObject.SetActive(true);
var data = new Dictionary<string, string>
{
{ "password",code.text.ToString() },
{ "mobile_number",phone.text.ToString() }
};
PlayerPrefs.SetString("mobile_number", phone.text.ToString());
PlayerPrefs.SetString("password", code.text.ToString());
StartCoroutine(webservice.instance.LoginRequest(data, OnLoginSuccess));
}
public Button button;
public GameObject loading;
public GameObject LoginMenu;
public GameObject Rooms;
void OnLoginSuccess(User response)
{
loading.gameObject.SetActive(false);
if (response != null)
{
EN.instance.Me = response;
Debug.Log("ورود موفقیت‌آمیز!");
Debug.Log("توکن: " + response.token);
LoginMenu.SetActive(false);
Rooms.SetActive(true);
LoginButton.SetActive(false);
GuestButton.SetActive(false);
ExitButton.SetActive(true);
EnterButton.SetActive(true);
}
else
{
switch (webservice.instance.lastErrorType)
{
case ErrorType.Authentication:
LoginMenu.gameObject.SetActive(true);
EN.instance.showError("خطا در ورود", webservice.instance.lastErrorResponse?.message ?? "نام کاربری یا رمز عبور اشتباه است");
break;
case ErrorType.Network:
EN.instance.showError("خطای اتصال", "لطفاً اتصال اینترنت خود را بررسی کنید و دوباره تلاش کنید");
break;
case ErrorType.Server:
EN.instance.showError("خطای سرور", "در حال حاضر سرور در دسترس نیست. لطفاً کمی بعد تلاش کنید");
break;
default:
EN.instance.showError("خطا", webservice.instance.lastError ?? "خطای ناشناخته رخ داده است");
break;
}
}
}
public float delay = 2;
public void PlayWelcome()
{
PlaySound("welcome");
}
public void PlaySound(string soundName, float volume = 1f)
{
// بارگذاری صدا از پوشه Resources
AudioClip clip = Resources.Load<AudioClip>("sounds/" + soundName);
if (clip != null)
{
// ایجاد یک GameObject جدید برای پخش صدا
GameObject audioObject = new GameObject("AudioSource_" + soundName);
AudioSource audioSource = audioObject.AddComponent<AudioSource>();
audioSource.volume = volume;
audioSource.spatialBlend = 0;
// تنظیم clip و پخش صدا
audioSource.clip = clip;
audioSource.Play();
// حذف GameObject بعد از پایان صدا
Destroy(audioObject, clip.length);
}
else
{
Debug.LogError("Sound not found: " + soundName);
}
}
public void Exit()
{
Application.Quit();
}
public void Select(GameObject i)
{
EN.instance.SelectedUser = EN.instance.users[int.Parse(i.name)];
}
public void SelectEnviroment(GameObject i)
{
Application.LoadLevel(int.Parse(i.name)+1);
}
public InputField phone, code;
// Update is called once per frame
void Update()
{
tablet.gameObject.SetActive(!EN.instance.Error.gameObject.activeSelf);
button.interactable = phone.text.Length>10&&code.text.Length>3;
if (Application.isEditor)
tablet.transform.parent = null;
}
public GameObject LoginButton,EnterButton,ExitButton,GuestButton;
}