2024-11-29 17:35:04 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
2024-12-04 23:54:19 +08:00
|
|
|
|
using Unity.Loader;
|
2024-10-24 16:16:57 +08:00
|
|
|
|
using UnityEngine;
|
2024-11-29 17:35:04 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
using Object = UnityEngine.Object;
|
2024-10-24 16:16:57 +08:00
|
|
|
|
|
|
|
|
|
namespace ZC
|
|
|
|
|
{
|
|
|
|
|
public static class CommonHelper
|
|
|
|
|
{
|
|
|
|
|
public static T FindChildDeep<T>(this Transform self, string name) where T : Object
|
|
|
|
|
{
|
|
|
|
|
Transform findDeep = null;
|
|
|
|
|
FindDeep(self, name, ref findDeep);
|
|
|
|
|
if (findDeep == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("未找到此组件");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var component = findDeep.GetComponent<T>();
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void FindDeep(Transform tran, string name, ref Transform transform)
|
|
|
|
|
{
|
|
|
|
|
if (tran.name == name)
|
|
|
|
|
{
|
|
|
|
|
transform = tran;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < tran.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
FindDeep(tran.GetChild(i), name, ref transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<T> FindChildDeeps<T>(this Transform self) where T : Object
|
|
|
|
|
{
|
|
|
|
|
List<T> list = new List<T>();
|
|
|
|
|
FindDeeps(self, ref list);
|
|
|
|
|
if (list.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("未找到此组件");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void FindDeeps<T>(Transform tran, ref List<T> list)
|
|
|
|
|
{
|
|
|
|
|
var component = tran.GetComponent<T>();
|
|
|
|
|
if (component != null)
|
|
|
|
|
list.Add(component);
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < tran.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
FindDeeps(tran.GetChild(i), ref list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断是否在某个物体范围内(范围型判断,超出返回false)
|
|
|
|
|
/// 基于MeshRender的实现方式
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tran"></param>
|
|
|
|
|
/// <param name="self"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool IsInRangeAutoFix(Transform tran, Transform self)
|
|
|
|
|
{
|
|
|
|
|
var meshRenderer = tran.GetComponent<MeshRenderer>();
|
|
|
|
|
|
|
|
|
|
Bounds rendererBounds = self.GetComponent<MeshRenderer>().bounds;
|
|
|
|
|
Bounds colliderBounds = meshRenderer.bounds;
|
|
|
|
|
bool rendererIsInsideBox = colliderBounds.Intersects(rendererBounds);
|
|
|
|
|
|
|
|
|
|
return rendererIsInsideBox;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-29 17:35:04 +08:00
|
|
|
|
public static T GetOrAddComponent<T>(this GameObject go) where T : Component
|
2024-10-24 16:16:57 +08:00
|
|
|
|
{
|
2024-11-29 17:35:04 +08:00
|
|
|
|
if (go.TryGetComponent(typeof(T), out var component))
|
|
|
|
|
{
|
|
|
|
|
return (T)component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return go.AddComponent<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async UniTask<SceneGameObjectBinding> GetSceneBindingAsync(
|
|
|
|
|
Action<SceneGameObjectBinding> callback = null)
|
|
|
|
|
{
|
|
|
|
|
SceneGameObjectBinding binding = null;
|
|
|
|
|
while (binding == null)
|
|
|
|
|
{
|
|
|
|
|
await UniTask.Yield();
|
2024-12-01 15:40:28 +08:00
|
|
|
|
Debug.Log("GetSceneBindingAsync");
|
2024-11-29 17:35:04 +08:00
|
|
|
|
binding = GameObject.FindObjectOfType<SceneGameObjectBinding>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback?.Invoke(binding);
|
|
|
|
|
return binding;
|
2024-10-24 16:16:57 +08:00
|
|
|
|
}
|
2024-12-04 23:54:19 +08:00
|
|
|
|
|
|
|
|
|
public static async UniTask OpenLoadingAsync()
|
|
|
|
|
{
|
|
|
|
|
var blackUI = Global.UIManager.GetUI<BlackUI>(UIType.BlackUI);
|
|
|
|
|
await blackUI.OpenAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async UniTask CloseLoadingAsync()
|
|
|
|
|
{
|
|
|
|
|
var blackUI = Global.UIManager.GetUI<BlackUI>(UIType.BlackUI);
|
|
|
|
|
await blackUI.CloseAsync();
|
|
|
|
|
}
|
2024-10-24 16:16:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|