unityzgy/Assets/Scripts/ZFramwork/Extend/UnityExtentionMethod.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

121 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 基于Unity某些类的扩展
/// </summary>
namespace XFramework
{
/// <summary>
/// 扩展方法
/// </summary>
public static class UnityExtentionMethod
{
//public static TaskAwaiter<UnityWebRequestAsyncOperation> GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
//{
// var
// return getwa;
//}
//static IEnumerator ReturnSelf<T>(TaskAwaiter<T> awaiter ,T instruction)
//{
// yield return instruction;
// awaiter.OnCompleted(instruction);
//}
public static TaskAwaiter GetAwaiter(this AsyncOperation asyncOp)
{
var tcs = new TaskCompletionSource<object>();
asyncOp.completed += obj => { tcs.SetResult(null); };
return ((Task)tcs.Task).GetAwaiter();
}
/// <summary>
/// 通过名称查找子对象
/// </summary>
/// <param name="obj"></param>
/// <param name="childName">名称</param>
/// <returns></returns>
public static Transform FindChildTrans(this Transform trans, string childName)
{
Transform child = trans.Find(childName);
if (child != null)
return child;
Transform tr;
for (int i = 0; i < trans.childCount; i++)
{
child = trans.GetChild(i);
tr = FindChildTrans(child, childName);
if (tr != null)
return tr;
}
//Debug.LogWarning($"{trans.name}里找不到名为{childName}的子对象");
return null;
}
public static T[] GetComponentsInRealChildren<T>(this Transform trans, bool includeInactive = false) where T : Component
{
List<T> TList = trans.GetComponentsInChildren<T>(includeInactive).ToList();
TList.Remove(trans.GetComponent<T>());//移除自身的
return TList.ToArray();
}
/// <summary>
/// 获取当前对象的某组件
/// 如获取失败则添加该组件
/// </summary>
/// <typeparam name="T">组件类型</typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T GetOrAddComponent<T>(this Transform t) where T : Component
{
T component = t.GetComponent<T>();
if (component == null)
component = t.gameObject.AddComponent<T>();
return component;
}
/// <summary>
/// 获取一个子对象的某组件
/// 如获取失败则添加
/// </summary>
/// <typeparam name="T">组件类型</typeparam>
/// <param name="t"></param>
/// <param name="childName">子对象名称</param>
/// <returns></returns>
public static T GetOrAddComponentInChildren<T>(this Transform t, string childName) where T : Component
{
Transform childObj = t.FindChildTrans(childName);
if (childObj == null)
return null;
return childObj.GetOrAddComponent<T>();
}
/// <summary>
/// 控制一个面板的外观
/// </summary>
/// <param name="t"></param>
/// <param name="on_off">显示则为true</param>
/// <param name="active">是否为活动对象</param>
public static void PanelAppearance(this Transform t, bool on_off, bool active = false)
{
CanvasGroup group = t.GetOrAddComponent<CanvasGroup>();
int value = on_off == true ? 1 : 0;
//射线检测
group.blocksRaycasts = on_off;
//交互
group.interactable = on_off;
//透明度
group.alpha = value;
t.gameObject.SetActive(on_off || active);
}
}
}