_TheStrongestSnail/TheStrongestSnail/Assets/common/base/base.cs

163 lines
4.7 KiB
C#

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
{
private List<string> LoadClassName = new List<string>() { "ImageLoader" , "Global" };//写入需要全局自动实例化的类
public Button retbutton;
public GameObject ClosureObj;
public static GameObject GlobalObj;
private void Awake()
{
Application.targetFrameRate = Mathf.RoundToInt(55f); // 设置目标帧率
initializeGlobal();
if (retbutton != null)
{
retbutton.onClick.AddListener(() => CancelOnClick(retbutton, ClosureObj));
}
}
/// <summary>
/// 初始化全局节点管理器
/// </summary>
private void initializeGlobal()
{
if (GlobalObj == null)
{
GlobalObj = new GameObject("GlobalObj");
DontDestroyOnLoad(GlobalObj);
foreach (string className in LoadClassName) {
CallClassByString(className);
}
}
}
/// <summary>
/// 用于通过类名字符串动态调用类和方法
/// </summary>
public void CallClassByString(string className)
{
// 获取类的 Type 对象
Type type = Type.GetType(className);
if (type != null)
{
GlobalObj.AddComponent(type);
}
else
{
Console.WriteLine("Class not found.");
}
}
/// <summary>
/// 播放按钮动画
/// </summary>
public async Task ButtonClickAnimationAsync(GameObject button,float max=1.25f,float timemultiple = 1)
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Sequence mySequence = DOTween.Sequence();
mySequence.Append(button.transform.DOScale(max, 0.1f* timemultiple)) // 第一个动画
.Append(button.transform.DOScale(1f, 0.2f* timemultiple)).OnComplete(() => {
// 动画播放完成后执行的代码
tcs.SetResult(true);
}); // 第二个动画
await tcs.Task;
}
/// <summary>
/// 添加上移通用弹窗
/// </summary>
public void addEventPopUp(string Details,float time = 5f)
{
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;
/// <summary>
/// 测试登录
/// </summary>
public async void testLogo()
{
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("测试登录成功");
testhead = new Dictionary<string, string>
{
{ "Authorization", TestserverResponse.data.token }
};
return;
}
addEventPopUp(response.message);
testhead = new Dictionary<string, string>();
}
/// <summary>
/// 判断必须为小数而且大于0
/// </summary>
public bool IsGreaterThanZeroDecimal(string text)
{
// 使用正则表达式匹配大于0的小数
string pattern = @"^(?!0(\.0+)?$)(\d+(\.\d+)?|\.\d+)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(text);
}
/// <summary>
/// 取消按钮
/// </summary>
/// <param name="button"></param>
/// <param name="my_gameObject"></param>
public async void CancelOnClick(Button button,GameObject my_gameObject = null)
{
await ButtonClickAnimationAsync(button.gameObject);
if (gameObject != null)
{
if (my_gameObject != null)
{
Destroy(my_gameObject);
return;
}
Destroy(gameObject);
}
}
/// <summary>
/// 给按钮绑定点击动画
/// </summary>
/// <param name="button"></param>
public void AddingAButtonEvent(Button button)
{
if (button != null)
{
button.onClick.AddListener(() => ButtonClickAnimationAsync(button.gameObject));
}
else {
Debug.Log("未找到按钮");
}
}
}