UnityCommon/base/base.cs

159 lines
5.1 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
{
2025-01-08 13:03:55 +08:00
[HideInInspector]
public string description = "";
2025-01-08 19:08:25 +08:00
private List<string> LoadClassName = new List<string>() { "gameGlobal" };//写入需要全局自动实例化的类
2025-01-08 13:03:55 +08:00
// [Header("关闭窗口的按钮")] public Button retbutton;
// [Header("需要关闭的窗口")] public GameObject ClosureObj;
2024-12-04 02:09:23 +08:00
public static GameObject GlobalObj;
/// <summary>
2025-01-08 13:03:55 +08:00
///初始化
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
{
2025-01-08 13:03:55 +08:00
Application.targetFrameRate = Mathf.RoundToInt(55f); // 设置目标帧率
2024-12-04 05:50:39 +08:00
initializeGlobal();
2025-01-08 13:03:55 +08:00
// 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>
2025-01-08 13:03:55 +08:00
/// 初始化全局类
2024-12-04 02:09:23 +08:00
/// </summary>
private void initializeGlobal()
{
if (GlobalObj == null)
{
GlobalObj = new GameObject("GlobalObj");
DontDestroyOnLoad(GlobalObj);
foreach (string className in LoadClassName) {
CallClassByString(className);
}
}
}
/// <summary>
2025-01-08 13:03:55 +08:00
/// 用于通过类名字符串动态调用类和方法
2024-12-04 02:09:23 +08:00
/// </summary>
/// <param name="className"></param>
public void CallClassByString(string className)
{
2025-01-08 13:03:55 +08:00
// 获取类的 Type 对象
2024-12-04 02:09:23 +08:00
Type type = Type.GetType(className);
if (type != null)
{
GlobalObj.AddComponent(type);
}
else
{
Console.WriteLine("Class not found.");
}
}
/// <summary>
2025-01-08 13:03:55 +08:00
/// 按钮动画,点击后会放大缩小
2024-12-04 02:09:23 +08:00
/// </summary>
2025-01-08 13:03:55 +08:00
/// <param name="button">实施目标对象</param>
/// <param name="max">放大倍率</param>
/// <param name="timemultiple">动画持续时间倍率</param>
2024-12-04 02:09:23 +08:00
/// <returns></returns>
2025-01-08 13:03:55 +08:00
public async Task ButtonClickAnimationAsync(GameObject button,float max=1.25f,float timemultiple = 1)//按钮动画
2024-12-04 02:09:23 +08:00
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Sequence mySequence = DOTween.Sequence();
2025-01-08 13:03:55 +08:00
mySequence.Append(button.transform.DOScale(max, 0.1f* timemultiple)) // 第一个动画
2024-12-04 02:09:23 +08:00
.Append(button.transform.DOScale(1f, 0.2f* timemultiple)).OnComplete(() => {
2025-01-08 13:03:55 +08:00
// 动画播放完成后执行的代码
2024-12-04 02:09:23 +08:00
tcs.SetResult(true);
2025-01-08 13:03:55 +08:00
}); // 第二个动画
2024-12-04 02:09:23 +08:00
await tcs.Task;
}
2024-12-04 02:27:59 +08:00
/// <summary>
2025-01-08 13:03:55 +08:00
/// 添加通用上移弹窗
2024-12-04 02:27:59 +08:00
/// </summary>
/// <param name="Details"></param>
/// <param name="time"></param>
2025-01-08 13:03:55 +08:00
public void addEventPopUp(string Details,float time = 5f)//添加弹窗
2024-12-04 02:09:23 +08:00
{
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;
2025-01-08 13:03:55 +08:00
//public async void testLogo()//测试登录
2024-12-04 02:09:23 +08:00
//{
// 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;
2025-01-08 13:03:55 +08:00
// addEventPopUp("测试登录成功");
2024-12-04 02:09:23 +08:00
// 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>
2025-01-08 13:03:55 +08:00
/// 判读是否为小数,并且必须大于零
2024-12-04 02:27:59 +08:00
/// </summary>
2025-01-08 13:03:55 +08:00
/// <param name="text">判断的字符串</param>
2024-12-04 02:27:59 +08:00
/// <returns></returns>
2025-01-08 13:03:55 +08:00
public bool IsGreaterThanZeroDecimal(string text)//判断必须为小数而且大于0
2024-12-04 02:09:23 +08:00
{
2025-01-08 13:03:55 +08:00
// 使用正则表达式匹配大于0的小数
2024-12-04 02:09:23 +08:00
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>
2025-01-08 13:03:55 +08:00
/// 点击后关闭界面
2024-12-04 02:27:59 +08:00
/// </summary>
/// <param name="button"></param>
/// <param name="my_gameObject"></param>
2025-01-08 13:03:55 +08:00
public async void CancelOnClick(Button button,GameObject my_gameObject = null)//取消按钮
2024-12-04 02:09:23 +08:00
{
await ButtonClickAnimationAsync(button.gameObject);
if (my_gameObject != null)
{
Destroy(my_gameObject);
return;
}
Destroy(gameObject);
}
}