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(Transform transform, string hierarchyPath) where T : Component { GameObject childObject = UnityUtility.GetChildObject(transform, hierarchyPath); return (!childObject) ? ((T)((object)null)) : childObject.GetComponent(); } public static T GetChildObject(GameObject obj, string hierarchyPath) where T : Component { return UnityUtility.GetChildObject((!obj) ? null : obj.transform, hierarchyPath); } public static List GetAllChildren(Transform transform, bool include_self = false) { List list = new List(); 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 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 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(this GameObject gameObject) where T : MonoBehaviour { T[] components = gameObject.GetComponents(); 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; } } }