123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- namespace kt.Utility
- {
- public static class UnityUtility
- {
- public static GameObject GetChildObject(Transform transform, string hierarchyPath)
- {
- if (transform == null)
- {
- return GameObject.Find(hierarchyPath);
- }
- return (!transform.Find(hierarchyPath)) ? null : transform.Find(hierarchyPath).gameObject;
- }
- public static GameObject GetChildObject(GameObject obj, string hierarchyPath)
- {
- return UnityUtility.GetChildObject((!obj) ? null : obj.transform, hierarchyPath);
- }
- public static GameObject GetChildObject(string hierarchyPath)
- {
- return UnityUtility.GetChildObject(null, hierarchyPath);
- }
- public static T GetChildObject<T>(Transform transform, string hierarchyPath) where T : Component
- {
- GameObject childObject = UnityUtility.GetChildObject(transform, hierarchyPath);
- return (!childObject) ? ((T)((object)null)) : childObject.GetComponent<T>();
- }
- public static T GetChildObject<T>(GameObject obj, string hierarchyPath) where T : Component
- {
- return UnityUtility.GetChildObject<T>((!obj) ? null : obj.transform, hierarchyPath);
- }
- public static List<Transform> GetAllChildren(Transform transform, bool include_self = false)
- {
- List<Transform> list = new List<Transform>();
- if (include_self)
- {
- list.Add(transform);
- }
- for (int i = 0; i < transform.childCount; i++)
- {
- Transform child = transform.GetChild(i);
- list.Add(child);
- list.AddRange(UnityUtility.GetAllChildren(child, false));
- }
- return list;
- }
- public static Transform SearchChild(this Transform transform, Predicate<Transform> match)
- {
- if (match(transform))
- {
- return transform;
- }
- for (int i = 0; i < transform.childCount; i++)
- {
- Transform transform2 = transform.GetChild(i).SearchChild(match);
- if (transform2)
- {
- return transform2;
- }
- }
- return null;
- }
- public static Transform SearchChildByName(this Transform transform, string search_name)
- {
- return transform.SearchChild((Transform ch) => ch.name == search_name);
- }
- public static string GetObjectTreePath(GameObject obj)
- {
- string text = string.Empty;
- if (obj == null)
- {
- return text;
- }
- text += obj.name;
- while (obj.transform.parent != null)
- {
- obj = obj.transform.parent.gameObject;
- text = obj.name + "/" + text;
- }
- return text;
- }
- public static string GetHierarchyPath(Transform trans, Transform root_obj = null, bool include_rootobj_path = true)
- {
- if (trans == root_obj)
- {
- return string.Empty;
- }
- string text = trans.gameObject.name;
- Transform parent = trans.parent;
- while (parent != root_obj && parent != null)
- {
- text = parent.name + "/" + text;
- parent = parent.parent;
- }
- if (include_rootobj_path)
- {
- text = root_obj.name + "/" + text;
- }
- return text;
- }
- public static bool IsChild(Transform parent, Transform child)
- {
- Func<Transform, Transform, Transform> Search = null;
- Search = delegate(Transform checkParent, Transform target)
- {
- if (checkParent == target)
- {
- return target;
- }
- for (int i = 0; i < checkParent.childCount; i++)
- {
- Transform transform = Search(checkParent.GetChild(i), checkParent);
- if (transform != null)
- {
- return transform;
- }
- }
- return null;
- };
- return Search(parent, child) == child;
- }
- public static string SaveImage(Texture f_texSrc, string f_strFileName = null)
- {
- if (f_texSrc is Texture2D)
- {
- return UnityUtility.SaveImage(f_texSrc as Texture2D, f_strFileName);
- }
- if (f_texSrc is RenderTexture)
- {
- return UnityUtility.SaveImage(f_texSrc as RenderTexture, f_strFileName, TextureFormat.ARGB32);
- }
- return string.Empty;
- }
- public static string SaveImage(RenderTexture f_rtSrc, string f_strFileName = null, TextureFormat f_TexFormat = TextureFormat.ARGB32)
- {
- if (f_rtSrc == null)
- {
- return string.Empty;
- }
- string text = f_strFileName;
- if (string.IsNullOrEmpty(text))
- {
- text = "img" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + ".png";
- }
- Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
- RenderTexture active = RenderTexture.active;
- RenderTexture.active = f_rtSrc;
- texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
- RenderTexture.active = active;
- byte[] bytes = texture2D.EncodeToPNG();
- File.WriteAllBytes(text, bytes);
- UnityEngine.Object.Destroy(texture2D);
- return text;
- }
- public static string SaveImage(Texture2D f_texSrc, string f_strFileName = null)
- {
- if (f_texSrc == null)
- {
- return string.Empty;
- }
- RenderTexture renderTexture = new RenderTexture(f_texSrc.width, f_texSrc.height, 0, RenderTextureFormat.ARGB32);
- RenderTexture active = RenderTexture.active;
- Graphics.Blit(f_texSrc, renderTexture);
- RenderTexture.active = active;
- string result = UnityUtility.SaveImage(renderTexture, f_strFileName, TextureFormat.ARGB32);
- UnityEngine.Object.Destroy(renderTexture);
- return result;
- }
- public static Sprite CreateSpriteFromImageFile(string pngFilePath)
- {
- if (string.IsNullOrEmpty(pngFilePath))
- {
- return null;
- }
- if (!File.Exists(pngFilePath))
- {
- return null;
- }
- Texture2D texture2D = null;
- try
- {
- byte[] array = File.ReadAllBytes(pngFilePath);
- if (array == null || array.Length == 0)
- {
- return null;
- }
- texture2D = new Texture2D(32, 32);
- texture2D.LoadImage(array);
- }
- catch (Exception ex)
- {
- Debug.LogError(ex.Message + "\n\n" + ex.StackTrace);
- return null;
- }
- if (texture2D == null || texture2D.width == 0 || texture2D.height == 0)
- {
- return null;
- }
- Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
- sprite.name = Path.GetFileName(pngFilePath);
- return sprite;
- }
- public static Texture2D CreateTexture2DFromRenderTexture(RenderTexture render_tex)
- {
- Texture2D texture2D = new Texture2D(render_tex.width, render_tex.height, TextureFormat.ARGB32, false);
- RenderTexture active = RenderTexture.active;
- RenderTexture.active = render_tex;
- texture2D.ReadPixels(new Rect(0f, 0f, (float)render_tex.width, (float)render_tex.height), 0, 0);
- texture2D.Apply();
- RenderTexture.active = active;
- return texture2D;
- }
- public static Color SetColorR(Color target, float r)
- {
- Color result = target;
- result.r = r;
- return result;
- }
- public static Color SetColorG(Color target, float g)
- {
- Color result = target;
- result.g = g;
- return result;
- }
- public static Color SetColorB(Color target, float b)
- {
- Color result = target;
- result.b = b;
- return result;
- }
- public static Color SetAlpha(Color target, float alpha)
- {
- Color result = target;
- result.a = alpha;
- return result;
- }
- public static Quaternion InverseTransformRotation(this Transform transform, Quaternion rot)
- {
- return Quaternion.Inverse(transform.rotation) * rot;
- }
- public static void RemoveAllComponents<T>(this GameObject gameObject) where T : MonoBehaviour
- {
- T[] components = gameObject.GetComponents<T>();
- if (components != null)
- {
- foreach (T t in components)
- {
- UnityEngine.Object.DestroyImmediate(t);
- }
- }
- }
- public class PosRotScale
- {
- public PosRotScale()
- {
- }
- public PosRotScale(UnityUtility.PosRotScale prs)
- {
- this.position = prs.position;
- this.scale = prs.scale;
- this.rotation = prs.rotation;
- }
- public Vector3 position;
- public Vector3 scale;
- public Quaternion rotation;
- }
- }
- }
|