XRoom_Unity/Assets/ENMenu.cs

219 lines
6.6 KiB
C#
Raw Normal View History

2025-05-31 06:50:20 +00:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2025-06-01 11:23:35 +00:00
using System.Collections;
using UnityEngine.Networking;
using Siccity.GLTFUtility;
2025-06-01 11:23:35 +00:00
using BNG;
using Fusion.XR.Host.Rig;
using Fusion;
using System.Threading;
using System.Linq;
2025-05-31 06:50:20 +00:00
2025-06-01 11:23:35 +00:00
public class ENMenu : NetworkBehaviour
2025-05-31 06:50:20 +00:00
{
SceneDownloader SceneDownloader;
2025-05-31 06:50:20 +00:00
public GameObject itemPr;
public Transform menu;
2025-06-01 11:23:35 +00:00
public List<GameObject> items = new List<GameObject>();
2025-05-31 06:50:20 +00:00
2025-06-01 11:23:35 +00:00
public GameObject tablet;
public UnityEngine.UI.Button button;
public UnityEngine.UI.Button buttonLoadSpace;
2025-06-01 11:23:35 +00:00
public GameObject loading;
public GameObject LoginMenu;
public GameObject Rooms;
public InputField phone, code;
public GameObject LoginButton, EnterButton, ExitButton, GuestButton;
public NetworkObject glbDownloaderPrefab;
public GameObject playerPref;
private bool isRunnerReady = false;
public float delay = 2;
public override void Spawned()
{
isRunnerReady = true;
}
2025-05-31 06:50:20 +00:00
void Start()
{
SceneDownloader=GetComponent<SceneDownloader>();
2025-05-31 06:50:20 +00:00
Invoke("PlayWelcome", delay);
2025-06-01 11:23:35 +00:00
EN.instance.SelectedUser = EN.instance.users.Count > 0 ? EN.instance.users[0] : null;
2025-06-01 11:23:35 +00:00
string user = PlayerPrefs.GetString("user", "");
2025-05-31 06:50:20 +00:00
if (!string.IsNullOrEmpty(user))
{
User response = new User(user);
2025-06-01 11:23:35 +00:00
phone.text = PlayerPrefs.GetString("mobile_number", "");
code.text = PlayerPrefs.GetString("password", "");
2025-05-31 06:50:20 +00:00
var data = new Dictionary<string, string>
2025-06-01 11:23:35 +00:00
{
{ "password", code.text },
{ "mobile_number", phone.text }
2025-06-01 11:23:35 +00:00
};
2025-05-31 06:50:20 +00:00
StartCoroutine(webservice.instance.LoginRequest(data, OnLoginSuccess));
}
else
{
LoginButton.SetActive(true);
GuestButton.SetActive(true);
ExitButton.SetActive(false);
EnterButton.SetActive(false);
}
}
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
public void exitAccount()
{
LoginButton.SetActive(true);
GuestButton.SetActive(true);
ExitButton.SetActive(false);
EnterButton.SetActive(false);
PlayerPrefs.SetString("user", "");
2025-06-01 11:23:35 +00:00
PlayerPrefs.SetString("mobile_number", "");
PlayerPrefs.SetString("password", "");
2025-05-31 06:50:20 +00:00
}
public void Login()
{
LoginMenu.SetActive(false);
loading.SetActive(true);
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
var data = new Dictionary<string, string>
{
{ "password", code.text },
{ "mobile_number", phone.text }
2025-05-31 06:50:20 +00:00
};
2025-06-01 11:23:35 +00:00
PlayerPrefs.SetString("mobile_number", phone.text);
PlayerPrefs.SetString("password", code.text);
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
StartCoroutine(webservice.instance.LoginRequest(data, OnLoginSuccess));
}
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
void OnLoginSuccess(User response)
{
loading.SetActive(false);
2025-05-31 06:50:20 +00:00
if (response != null)
{
EN.instance.Me = response;
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
LoginMenu.SetActive(false);
Rooms.SetActive(true);
LoginButton.SetActive(false);
GuestButton.SetActive(false);
ExitButton.SetActive(true);
EnterButton.SetActive(true);
2025-06-01 11:23:35 +00:00
Debug.Log("Login successful");
// After login success, load user info
2025-06-01 11:23:35 +00:00
StartCoroutine(webservice.instance.GetInfoRequest(OnGetInfoSuccess));
// Also, load meetings and spaces
StartCoroutine(webservice.instance.GetUserMeetings(EN.instance.OnMeetingsLoaded));
StartCoroutine(webservice.instance.GetSpaces(EN.instance.OnSpacesLoaded));
2025-05-31 06:50:20 +00:00
}
else
{
switch (webservice.instance.lastErrorType)
{
case ErrorType.Authentication:
LoginMenu.SetActive(true);
2025-05-31 06:50:20 +00:00
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;
}
}
}
2025-06-01 11:23:35 +00:00
void OnGetInfoSuccess(GetInfoResponse response)
{
if (response != null)
{
EN.instance.Me.characterUrl = response.customer.profile_glb;
EN.instance.Me.character = playerPref;
2025-06-01 11:23:35 +00:00
Debug.Log("GetInfo successful - Character URL: " + response.customer.profile_glb);
}
else
{
Debug.LogError("Failed to get user info: " + webservice.instance.lastError);
EN.instance.showError("خطا", "در دریافت اطلاعات کاربر مشکلی پیش آمده است");
}
}
2025-05-31 06:50:20 +00:00
public void PlayWelcome()
{
PlaySound("welcome");
}
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
public void PlaySound(string soundName, float volume = 1f)
{
AudioClip clip = Resources.Load<AudioClip>("sounds/" + soundName);
if (clip != null)
{
GameObject audioObject = new GameObject("AudioSource_" + soundName);
AudioSource audioSource = audioObject.AddComponent<AudioSource>();
audioSource.volume = volume;
audioSource.spatialBlend = 0;
audioSource.clip = clip;
audioSource.Play();
Destroy(audioObject, clip.length);
}
else
{
Debug.LogError("Sound not found: " + soundName);
}
}
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
public void Exit()
{
Application.Quit();
}
2025-06-01 11:23:35 +00:00
2025-05-31 06:50:20 +00:00
public void Select(GameObject i)
{
if (int.TryParse(i.name, out int index) && index >= 0 && index < EN.instance.users.Count)
{
EN.instance.SelectedUser = EN.instance.users[index];
}
2025-06-01 11:23:35 +00:00
}
public void SelectEnvironment(GameObject i)
2025-05-31 06:50:20 +00:00
{
if (int.TryParse(i.name, out int index))
{
Application.LoadLevel(index + 1);
}
2025-05-31 06:50:20 +00:00
}
void Update()
{
tablet.SetActive(!EN.instance.Error.activeSelf);
2025-06-01 11:23:35 +00:00
button.interactable = phone.text.Length > 10 && code.text.Length > 3;
2025-05-31 06:50:20 +00:00
if (Application.isEditor)
tablet.transform.parent = null;
}
}