123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- using DG.Tweening;
- using System;
- using System.Text;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- using UnityEngine.Video;
- namespace XRTool.Util
- {
- /// <summary>
- /// 工具类,各种常用算法的集合脚本
- /// </summary>
- public static class UnityUtil
- {
- #if UNITY_EDITOR
- /// <summary>
- /// 尝试得到一个未命名的名称
- /// 下标从0开始检索,如果不存在此名字的文字,则返回此名字
- /// 如果存在,下标++
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="path"></param>
- /// <param name="suffix"></param>
- /// <returns></returns>
- public static string TryGetName<T>(string path, string suffix = ".asset")
- {
- int index = 0;
- string confName = "";
- UnityEngine.Object obj = null;
- do
- {
- confName = path + "/" + typeof(T).Name + "_" + index + suffix;
- obj = AssetDatabase.LoadAssetAtPath(confName, typeof(T));
- index++;
- } while (obj);
- return confName;
- }
- public static string TryGetName(Type T,string path, string suffix = ".asset")
- {
- int index = 0;
- string confName = "";
- UnityEngine.Object obj = null;
- do
- {
- confName = path + "/" + T.Name + "_" + index + suffix;
- obj = AssetDatabase.LoadAssetAtPath(confName, T);
- index++;
- } while (obj);
- return confName;
- }
- #endif
- /// <summary>
- /// 获取指定名称类型的对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public static T GetChild<T>(Transform target, string childName)
- {
- var child = target.Find(childName);
- if (child)
- {
- return child.GetComponent<T>();
- }
- return default(T);
- }
- /// <summary>
- /// 深度优先搜索查找子物体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public static T GetDepthChild<T>(Transform transform, string childName)
- {
- Transform target = FindDepthTransf(transform, childName);
- if (target)
- return GetT<T>(target.gameObject);
- return default(T);
- }
- /// <summary>
- /// 广度优先查找子物体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public static T GetBreadthChild<T>(Transform transform, string childName)
- {
- Transform target = FindBreadthTransf(transform, childName);
- if (target)
- return GetT<T>(target.gameObject);
- return default(T);
- }
- public static GameObject GetBreadthChild(Transform transform, string childName)
- {
- Transform target = FindBreadthTransf(transform, childName);
- if (target)
- return target.gameObject;
- return null;
- }
- public static T GetParent<T>(Transform transform)
- {
- return transform.GetComponentInParent<T>();
- }
- public static T GetChild<T>(Transform trans)
- {
- return trans.GetComponentInChildren<T>();
- }
- public static T GetT<T>(GameObject target)
- {
- return target.GetComponent<T>();
- }
- /// <summary>
- /// 深度优先检索子物体
- /// </summary>
- /// <param name="check"></param>
- /// <param name="childName"></param>
- /// <returns></returns>
- public static Transform FindDepthTransf(Transform check, string childName)
- {
- if (check.name == childName) return check;
- for (int i = 0; i < check.childCount; i++)
- {
- Transform obj = FindDepthTransf(check.GetChild(i), childName);
- if (obj)
- return obj;
- }
- return null;
- }
- /// <summary>
- /// 广度优先检索子物体
- /// </summary>
- /// <param name="check"></param>
- /// <param name="childName"></param>
- /// <returns></returns>
- public static Transform FindBreadthTransf(Transform check, string childName)
- {
- Transform forreturn = check.Find(childName);
- if (forreturn)
- {
- return forreturn;
- }
- if (check.childCount > 0)
- {
- for (int i = 0; i < check.childCount; i++)
- {
- var target = FindBreadthTransf(check.GetChild(i), childName);
- if (target)
- {
- return target;
- }
- }
- }
- return forreturn;
- }
- public static void SetParent(Transform parent, Transform child)
- {
- child.SetParent(parent);
- child.localPosition = Vector3.zero;
- child.localRotation = Quaternion.identity;
- child.localScale = Vector3.one;
- }
- /// <summary>
- /// 去除文件bom头后的字符
- /// </summary>
- /// <param name="buffer"></param>
- /// <returns></returns>
- public static string GetUTF8String(byte[] buffer)
- {
- if (buffer == null)
- return null;
- if (buffer.Length <= 3)
- {
- return Encoding.UTF8.GetString(buffer);
- }
- byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
- if (buffer[0] == bomBuffer[0]
- && buffer[1] == bomBuffer[1]
- && buffer[2] == bomBuffer[2])
- {
- return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3);
- }
- return Encoding.UTF8.GetString(buffer);
- }
- /// <summary>
- /// 获取距离放歌最近的数字
- /// 四舍五入
- /// </summary>
- /// <param name="num"></param>
- /// <param name="cell"></param>
- /// <returns></returns>
- public static float GetNearst(float num, float cell)
- {
- int dir = num < 0 ? -1 : 1;
- return ((int)(num + cell / 2 * dir) / (int)cell) * cell;
- }
- /// <summary>
- /// 判断两个矩形是否相交
- /// 如果两个矩形中心点在x、y轴的投影距离分别小于矩形的边长之和,则此矩形相交
- /// </summary>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <returns></returns>
- public static bool IsCrossLine(Rect a, Rect b)
- {
- Vector2 dis = b.center - a.center;
- if (((int)a.width + (int)b.width) / 2 > Math.Abs(dis.x) && ((int)a.height + (int)b.height) / 2 > Math.Abs(dis.y))
- {
- return true;
- }
- return false;
- }
- public static void ChangeMateColor(Renderer render, Color color, string name = "_Color")
- {
- if (render)
- {
- MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetColor(name, color);
- render.SetPropertyBlock(propertyBlock);
- //ChangeMateValue(render, propertyBlock);
- propertyBlock = null;
- }
- }
- public static void ChangeMateColor(MaterialPropertyBlock propertyBlock, Renderer render, Color color, string name = "_Color")
- {
- if (render)
- {
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetColor(name, color);
- render.SetPropertyBlock(propertyBlock);
- //ChangeMateValue(render, propertyBlock);
- //propertyBlock = null;
- }
- }
- public static void ChangeMateTexture2D(Renderer render, Texture2D img, string name = "_MainTex")
- {
- if (render)
- {
- MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetTexture(name, img);
- render.SetPropertyBlock(propertyBlock);
- propertyBlock = null;
- }
- }
- public static void ChangeMateVideo(GameObject obj, VideoClip video, string name = "_MainTex")
- {
- if (obj)
- {
- VideoPlayer vp = obj.GetComponent<VideoPlayer>();
- vp.clip = video;
- }
- }
- public static void ChangeMateTexture(Renderer render, Texture img, string name = "_MainTex")
- {
- if (render)
- {
- MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetTexture(name, img);
- render.SetPropertyBlock(propertyBlock);
- propertyBlock = null;
- }
- }
- public static void ChangeMateValue(Renderer render, float att, string name = "_Smoothness")
- {
- if (render)
- {
- MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetFloat(name, att);
- render.SetPropertyBlock(propertyBlock);
- //ChangeMateValue(render, propertyBlock);
- propertyBlock = null;
- }
- }
- public static void ChangeMateValue(Renderer render, Vector4 att, string name = "_Smoothness")
- {
- if (render)
- {
- MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- propertyBlock.SetVector(name, att);
- render.SetPropertyBlock(propertyBlock);
- //ChangeMateValue(render, propertyBlock);
- propertyBlock = null;
- }
- }
- public static void ChangeMateValue(Renderer render, MaterialPropertyBlock propertyBlock)
- {
- if (render)
- {
- //MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
- render.GetPropertyBlock(propertyBlock);
- //propertyBlock.SetVector(name, att);
- render.SetPropertyBlock(propertyBlock);
- }
- }
- /// <summary>
- /// 材质复制
- /// </summary>
- /// <param name="resMate"></param>
- /// <param name="targetMate"></param>
- public static void CopyMate(Material resMate, Material targetMate)
- {
- if (resMate && resMate != targetMate)
- {
- resMate.shader = targetMate.shader;
- resMate.CopyPropertiesFromMaterial(targetMate);
- }
- }
- ///// <summary>
- /////
- ///// </summary>
- ///// <returns></returns>
- //public static string ArtTransferInfo(ArtInfo info)
- //{
- // Vector3 pos = GameSession.Instance.GetHeadForwadPos(info.Distance);
- // if (GameNode.Instance)
- // {
- // pos = GameNode.Instance.transform.InverseTransformPoint(pos);
- // }
- // var angle = Vector3.zero;
- // var scale = Vector3.one * info.Size;
- // return TransferToString(pos, angle, scale, 2);
- //}
- public static string TransferToString(Transform tranfer, int state)
- {
- return TransferToString(tranfer.localPosition, tranfer.localEulerAngles, tranfer.localScale, state);
- }
- public static int maxTransfer = 1000;
- public static string TransferToString(Vector3 pos, Vector3 ang, Vector3 sca, int state)
- {
- string info = "";
- pos *= maxTransfer;
- ang *= maxTransfer;
- sca *= maxTransfer;
- string position = (int)(pos.x) + "," + (int)(pos.y) + "," + (int)(pos.z);
- string angle = (int)(ang.x) + "," + (int)(ang.y) + "," + (int)(ang.z);
- string scale = (int)(sca.x) + "," + (int)(sca.y) + "," + (int)(sca.z);
- info = position + "|" + angle + "|" + scale;
- //if (state == 2)
- //{
- // info = pos + "|" + angle + "|" + scale;
- //}
- //else if (state == 1)
- //{
- // info = pos + "|" + angle;
- //}
- //else if (state == 1)
- //{
- // info = pos;
- //}
- return info;
- }
- //public static Posture GetPosture(Transform info)
- //{
- // Posture posture = new Posture();
- // posture.position = info.localPosition;
- // posture.angle = info.localEulerAngles;
- // posture.scale = info.localScale;
- // posture.count = 2;
- // return posture;
- //}
- //public static void SetPosture(Transform info, Posture posture, float time = -1)
- //{
- // if (time <= 0)
- // {
- // info.localPosition = posture.position;
- // info.localEulerAngles = posture.angle;
- // info.localScale = posture.scale;
- // }
- // else
- // {
- // info.DOKill();
- // if (posture.count >= 0)
- // {
- // info.DOLocalMove(posture.position, time);
- // }
- // if (posture.count >= 1)
- // {
- // info.DOLocalRotate(posture.angle, time);
- // }
- // if (posture.count >= 2)
- // {
- // info.DOScale(posture.scale, time);
- // }
- // }
- //}
- //public static Posture GetPosture(string info)
- //{
- // Posture posture = new Posture();
- // string[] arr = info.Split('|');
- // for (int i = 0; i < arr.Length; i++)
- // {
- // if (string.IsNullOrEmpty(arr[i]))
- // {
- // continue;
- // }
- // string[] v = arr[i].Split(',');
- // Vector3 vector = Vector3.zero;
- // vector.x = float.Parse(v[0]);
- // vector.y = float.Parse(v[1]);
- // vector.z = float.Parse(v[2]);
- // //for (int j = 0; j < v.Length; j++)
- // //{
- // // float value = float.Parse(v[j]);
- // // if (j == 0)
- // // {
- // // vector.x = value;
- // // }
- // // else if (j == 1)
- // // {
- // // vector.y = value;
- // // }
- // // else if (j == 2)
- // // {
- // // vector.z = value;
- // // }
- // //}
- // vector /= maxTransfer;
- // if (i == 0)
- // {
- // posture.position = vector;
- // }
- // else if (i == 1)
- // {
- // posture.angle = vector;
- // }
- // else if (i == 2)
- // {
- // posture.scale = vector;
- // }
- // posture.count = i;
- // }
- // return posture;
- //}
- //public static Posture GetPosture(string info)
- //{
- // Posture posture = new Posture();
- // string[] arr = info.Split('|');
- // for (int i = 0; i < arr.Length; i++)
- // {
- // if (string.IsNullOrEmpty(arr[i]))
- // {
- // continue;
- // }
- // string[] v = arr[i].Split(',');
- // Vector3 vector = Vector3.zero;
- // for (int j = 0; j < v.Length; j++)
- // {
- // float value = float.Parse(v[j]);
- // if (j == 0)
- // {
- // vector.x = value;
- // }
- // else if (j == 1)
- // {
- // vector.y = value;
- // }
- // else if (j == 2)
- // {
- // vector.z = value;
- // }
- // }
- // vector *= maxTransfer;
- // if (i == 0)
- // {
- // posture.position = vector;
- // }
- // else if (i == 1)
- // {
- // posture.angle = vector;
- // }
- // else if (i == 2)
- // {
- // posture.scale = vector;
- // }
- // posture.count = i;
- // }
- // return posture;
- //}
- public static Vector3 StringToVector3(string v)
- {
- Vector3 z = Vector3.zero;
- v = v.Replace("(", "").Replace(")", "");
- string[] s = v.Split(',');
- if (s.Length > 2)
- {
- z.x = float.Parse(s[0]);
- z.y = float.Parse(s[1]);
- z.z = float.Parse(s[2]);
- z /= maxTransfer;
- }
- return z;
- }
- public static string Vector3ToString(Vector3 v)
- {
- return v.ToString();
- }
- public static string QuaterToString(Quaternion q)
- {
- q.x *= maxTransfer;
- q.y *= maxTransfer;
- q.z *= maxTransfer;
- q.w *= maxTransfer;
- return q.ToString();
- }
- public static Quaternion StringToQuater(string v)
- {
- Quaternion q = Quaternion.identity;
- v = v.Replace("(", "").Replace(")", "");
- string[] s = v.Split(',');
- if (s.Length > 3)
- {
- q.x = float.Parse(s[0]) / maxTransfer;
- q.y = float.Parse(s[1]) / maxTransfer;
- q.z = float.Parse(s[2]) / maxTransfer;
- q.w = float.Parse(s[3]) / maxTransfer;
- }
- return q;
- }
- public static Vector3 RealForward(Transform body)
- {
- Vector3 right = body.right;
- right.y = 0;
- return Vector3.Cross(right, Vector3.up);
- }
- public static string CurTimeString
- {
- get { return DateTime.Now.ToString("MMddHHmmssf"); }
- }
- }
- }
|