UnityCommon/base/base.cs

157 lines
4.9 KiB
C#
Raw Normal View History

2024-12-04 02:09:23 +08:00
using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class Base : MonoBehaviour
{
2024-12-11 16:51:55 +08:00
private List<string> LoadClassName = new List<string>() { "ImageLoader" , "Global", "gameGlobal" };//д<><D0B4><EFBFBD><EFBFBD>Ҫȫ<D2AA><C8AB><EFBFBD>Զ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-04 05:50:39 +08:00
[Header("<22>رմ<D8B1><D5B4>ڵİ<DAB5>ť")] public Button retbutton;
[Header("<22><>Ҫ<EFBFBD>رյĴ<D5B5><C4B4><EFBFBD>")] public GameObject ClosureObj;
2024-12-04 02:09:23 +08:00
public static GameObject GlobalObj;
/// <summary>
2024-12-04 02:27:59 +08:00
///<2F><>ʼ<EFBFBD><CABC>
2024-12-04 02:09:23 +08:00
/// </summary>
private void Awake()
{
2024-12-04 05:50:39 +08:00
if (Application.isPlaying)
2024-12-04 02:09:23 +08:00
{
2024-12-04 05:50:39 +08:00
Application.targetFrameRate = Mathf.RoundToInt(55f); // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>֡<EFBFBD><D6A1>
initializeGlobal();
if (retbutton != null)
{
retbutton.onClick.AddListener(() => CancelOnClick(retbutton, ClosureObj));
}
2024-12-04 02:09:23 +08:00
}
2024-12-04 05:50:39 +08:00
2024-12-04 02:09:23 +08:00
}
/// <summary>
/// <20><>ʼ<EFBFBD><CABC>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
/// </summary>
private void initializeGlobal()
{
if (GlobalObj == null)
{
GlobalObj = new GameObject("GlobalObj");
DontDestroyOnLoad(GlobalObj);
foreach (string className in LoadClassName) {
CallClassByString(className);
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>
/// </summary>
/// <param name="className"></param>
public void CallClassByString(string className)
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> Type <20><><EFBFBD><EFBFBD>
Type type = Type.GetType(className);
if (type != null)
{
GlobalObj.AddComponent(type);
}
else
{
Console.WriteLine("Class not found.");
}
}
/// <summary>
/// <20><>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD>С
/// </summary>
/// <param name="button">ʵʩĿ<CAA9><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="max"><3E>Ŵ<EFBFBD><C5B4><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="timemultiple"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><E4B1B6></param>
/// <returns></returns>
public async Task ButtonClickAnimationAsync(GameObject button,float max=1.25f,float timemultiple = 1)//<2F><>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Sequence mySequence = DOTween.Sequence();
mySequence.Append(button.transform.DOScale(max, 0.1f* timemultiple)) // <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.Append(button.transform.DOScale(1f, 0.2f* timemultiple)).OnComplete(() => {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD>ִ<EFBFBD>еĴ<D0B5><C4B4><EFBFBD>
tcs.SetResult(true);
}); // <20>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
await tcs.Task;
}
2024-12-04 02:27:59 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>
/// </summary>
/// <param name="Details"></param>
/// <param name="time"></param>
2024-12-04 02:09:23 +08:00
public void addEventPopUp(string Details,float time = 5f)//<2F><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>
{
GameObject prefab = Resources.Load<GameObject>("base/EventPopUp");
prefab.GetComponent<EventPopUp>().time = time;
Canvas canvas = GetComponentInParent<Canvas>();
prefab.GetComponent<EventPopUp>().text = Details;
Instantiate(prefab, canvas.transform);
}
//logoPanel.ServerResponse TestserverResponse;
//protected Dictionary<string, string> testhead;
//public async void testLogo()//<2F><><EFBFBD>Ե<EFBFBD>¼
//{
// LoginAndGetToken.loginbody body = new LoginAndGetToken.loginbody
// {
// userName = "15151658596",
// password = "123456",
// verifyCode = 111111
// };
// string loginResponse = await web.SendRequest(web.URL + "/snail/user/login", "POST", JsonUtility.ToJson(body));
// logoPanel.ServerResponse response = JsonUtility.FromJson<logoPanel.ServerResponse>(loginResponse);
// if (response != null && response.code == 200 && response.data != null)
// {
// TestserverResponse = response;
// addEventPopUp("<22><><EFBFBD>Ե<EFBFBD>¼<EFBFBD>ɹ<EFBFBD>");
// testhead = new Dictionary<string, string>
// {
// { "Authorization", TestserverResponse.data.token }
// };
// return;
// }
// addEventPopUp(response.message);
// testhead = new Dictionary<string, string>();
//}
2024-12-04 02:27:59 +08:00
/// <summary>
/// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ΪС<CEAA><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="text"><3E>жϵ<D0B6><CFB5>ַ<EFBFBD><D6B7><EFBFBD></param>
/// <returns></returns>
2024-12-04 02:09:23 +08:00
public bool IsGreaterThanZeroDecimal(string text)//<2F>жϱ<D0B6><CFB1><EFBFBD>ΪС<CEAA><D0A1><EFBFBD><EFBFBD><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD>0
{
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽƥ<CABD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>С<EFBFBD><D0A1>
string pattern = @"^(?!0(\.0+)?$)(\d+(\.\d+)?|\.\d+)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(text);
}
2024-12-04 02:27:59 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رս<D8B1><D5BD><EFBFBD>
/// </summary>
/// <param name="button"></param>
/// <param name="my_gameObject"></param>
2024-12-04 02:09:23 +08:00
public async void CancelOnClick(Button button,GameObject my_gameObject = null)//ȡ<><C8A1><EFBFBD><EFBFBD>ť
{
await ButtonClickAnimationAsync(button.gameObject);
if (my_gameObject != null)
{
Destroy(my_gameObject);
return;
}
Destroy(gameObject);
}
}