125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
using DG.Tweening;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class ToolFuncManager : MonoBehaviour
|
||
{
|
||
//在根节点下查找所有包含T组件的物体,InActice的也行
|
||
static public List<T> FindChildrenWithComponent<T>(Transform parentTF) where T : Component
|
||
{
|
||
List<T> result = new List<T>();
|
||
FindChildrenWithComponentRecursively<T>(parentTF, result);
|
||
return result;
|
||
}
|
||
|
||
static public T FindChildWithComponent<T>(Transform parentTF) where T : Component
|
||
{
|
||
T result = parentTF.GetComponent<T>();
|
||
if (result == null)
|
||
{
|
||
foreach (Transform childTF in parentTF)
|
||
result = FindChildWithComponent<T>(childTF);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static private void FindChildrenWithComponentRecursively<T>(Transform parentTF, List<T> result) where T : Component
|
||
{
|
||
for (int i = 0; i < parentTF.childCount; i++)
|
||
{
|
||
Transform childTF = parentTF.GetChild(i);
|
||
GameObject childGO = childTF.gameObject;
|
||
|
||
// 检查当前子物体是否包含目标组件
|
||
if (childGO.TryGetComponent<T>(out T component))
|
||
{
|
||
result.Add(childGO.GetComponent<T>());
|
||
}
|
||
|
||
// 递归查找子物体的子物体
|
||
FindChildrenWithComponentRecursively<T>(childTF, result);
|
||
}
|
||
}
|
||
|
||
//在根节点下查找名称为childName物体,InActice的也行
|
||
static public Transform GetChild(Transform parentTF, string childName)
|
||
{
|
||
//在子物体中查找
|
||
childName = childName.Trim();
|
||
Transform childTF = parentTF.Find(childName);
|
||
if (childTF != null) return childTF;
|
||
//将问题交给子物体
|
||
for (int i = 0; i < parentTF.childCount; i++)
|
||
{
|
||
childTF = GetChild(parentTF.GetChild(i), childName);
|
||
if (childTF != null) return childTF;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算两个list的交集数量
|
||
/// </summary>
|
||
/// <param name="L1"></param>
|
||
/// <param name="L2"></param>
|
||
/// <returns></returns>
|
||
public static int CountMatches(List<string> L1, List<string> L2)
|
||
{
|
||
int count = 0;
|
||
foreach (string item in L2)
|
||
{
|
||
if (L1.Contains(item))
|
||
{
|
||
count++;
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
|
||
public static void ActiveEmbededTip(GameObject tip, Tween tween = null)//激活界面中已经嵌入的提示
|
||
{
|
||
if (tween != null)
|
||
tween.Kill(true);
|
||
tip.gameObject.SetActive(true);
|
||
Image image = tip.GetComponent<Image>();
|
||
image.enabled = true;
|
||
if (tween != null)
|
||
{
|
||
tween = DOVirtual.Float(1, 0.3f, 0.5f, t =>
|
||
{
|
||
Color finalColor = image.color;
|
||
finalColor.a = t;
|
||
image.color = finalColor;
|
||
}).SetLoops(-1, LoopType.Yoyo);//HQB 原始数值:1,0.
|
||
}
|
||
}
|
||
|
||
public static Vector3 Parse(string vectorString)
|
||
{
|
||
if (vectorString.StartsWith("(") && vectorString.EndsWith(")"))
|
||
{
|
||
vectorString = vectorString.Substring(1, vectorString.Length - 2);
|
||
}
|
||
|
||
// 分割字符串,假设格式是 "x,y,z"
|
||
string[] parts = vectorString.Split(',');
|
||
|
||
// 确保字符串包含三个部分
|
||
if (parts.Length != 3)
|
||
{
|
||
throw new FormatException("Input string is not in a valid Vector3 format.");
|
||
}
|
||
|
||
// 解析浮点数
|
||
float x = float.Parse(parts[0].Trim());
|
||
float y = float.Parse(parts[1].Trim());
|
||
float z = float.Parse(parts[2].Trim());
|
||
|
||
// 返回新的Vector3对象
|
||
return new Vector3(x, y, z);
|
||
}
|
||
}
|