系统模块:常用工具类

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;
        }

        /// <summary>
        /// 查找游戏对象
        /// </summary>
        /// <param name="rootName">根节点名称</param>
        /// <param name="relativePath">相对路径</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 将字符串转化为v3坐标
        /// </summary>
        /// <param name="argStr">分割符为,</param>
        /// <returns></returns>
        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(int.Parse(posArray[0]), int.Parse(posArray[1]), int.Parse(posArray[2]));
                pos = new Vector3(float.Parse(posArray[0]), float.Parse(posArray[1]), float.Parse(posArray[2]));
            }
            else
            {
                Debug.LogError("posArray长度错误,请检查");
            }
            return pos;
        }

        /// <summary> 
        /// v3字符串转化为v3列表
        /// </summary>
        /// <param name="argStr">坐标之间用|间隔,参数之间用,间隔</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 将字符串转化为Color32类型
        /// </summary>
        /// <param name="arg">四个参数用,隔开</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 根据全路径获取场景对象
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        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;
        }


        /// <summary>
        /// 二阶贝塞尔
        /// </summary>
        /// <param name="t">时间参数,范围0~1</param>
        /// <returns></returns>
        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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值