using System.Collections.Generic;
using UnityEngine;
namespace Miss
{
public static class GameUtilits
{
public static bool IsNull<T>(this T target, bool isShowLog = false)
{
if (null == target)
{
if (isShowLog)
{
Debug.LogErrorFormat($"该类型对象为空,请检查!!! {typeof(T)}");
}
return true;
}
return false;
}
public static bool ListIsNull<T>(this List<T> list, bool isShowLog = false)
{
if (null == list || list.Count == 0)
{
if (isShowLog)
{
Debug.LogError($"该列表对象为空or长度为0,请检查!!!");
}
return true;
}
return false;
}
public static bool ArrayIsNull<T>(this T[] array, bool isShowLog = false)
{
if (null == array || array.Length == 0)
{
if (isShowLog)
{
Debug.LogError($"该列表对象为空or长度为0,请检查!!!");
}
return true;
}
return false;
}
public static GameObject FindGameObj(string rootName, string relativePath)
{
if (string.IsNullOrEmpty(rootName) || string.IsNullOrEmpty(relativePath))
{
Debug.LogError("rootName 或 relativePath 为空");
return null;
}
GameObject root = GameObject.Find(rootName);
if (root.IsNull(true))
{
return null;
}
GameObject target = root.transform.Find(relativePath).gameObject;
if (target.IsNull(true))
{
return null;
}
return target;
}
public static Vector3 SwitchStrToV3(string argStr)
{
Vector3 pos = Vector3.zero;
if (string.IsNullOrEmpty(argStr))
{
Debug.LogError("argStr为空,请检查");
return pos;
}
string[] posArray = argStr.Split(',');
if (posArray.Length == 3)
{
pos = new Vector3(float.Parse(posArray[0]), float.Parse(posArray[1]), float.Parse(posArray[2]));
}
else
{
Debug.LogError("posArray长度错误,请检查");
}
return pos;
}
public static List<Vector3> SwitchStrToV3List(string argStr)
{
List<Vector3> v3List = new List<Vector3>();
if (string.IsNullOrEmpty(argStr))
{
Debug.LogError("argStr为空,请检查");
return v3List;
}
string[] posArray = argStr.Split('|');
for (int i = 0; i < posArray.Length; i++)
{
string[] argArray = posArray[i].Split(',');
if (argArray.Length != 3)
{
Debug.LogError("argArray.Length!=3 请检查");
return v3List;
}
Vector3 temp = new Vector3(float.Parse(argArray[0]), float.Parse(argArray[1]), float.Parse(argArray[2]));
v3List.Add(temp);
}
return v3List;
}
public static Color32 SwitchStrToColor32(string str)
{
Color32 color = new Color32(0, 0, 0, 0);
if (string.IsNullOrEmpty(str))
{
Debug.LogError("arg为空");
return color;
}
string[] argArray = str.Split(',');
if (argArray.Length == 4)
{
color = new Color32(byte.Parse(argArray[0]), byte.Parse(argArray[1]), byte.Parse(argArray[2]), byte.Parse(argArray[3]));
}
else
{
Debug.LogError("argArray.Length!=4");
}
return color;
}
public static Transform FindSceneObjByFullPath(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
{
Debug.LogError("fullPath为空");
return null;
}
Transform target = null;
string rootName = "";
string realPath = "";
string[] arrayStr = fullPath.Split('/');
rootName = arrayStr[0];
if (arrayStr.Length > 1)
{
realPath = fullPath.Substring(fullPath.IndexOf('/') + 1);
}
GameObject root = GameObject.Find(rootName);
if (root == null)
{
Debug.LogError($"rootName根节点在场景中不存在 {rootName}");
return null;
}
if (!string.IsNullOrEmpty(realPath))
{
target = root.transform.Find(realPath);
}
else
{
target = root.transform;
}
return target;
}
public static Vector3 Bezier(Vector3 startPoint, Vector3 centerPoint, Vector3 endPoint, float t)
{
Vector3 a = startPoint;
Vector3 b = centerPoint;
Vector3 c = endPoint;
Vector3 aa = a + (b - a) * t;
Vector3 bb = b + (c - b) * t;
return aa + (bb - aa) * t;
}
}
}