using System.Collections; using System.Collections.Generic; using UnityEngine; public class UnityTool { private List<int> a; /// <summary> /// 完成GameObject附加的功能,传入父 子 localPosition /// </summary> /// <param name="parentObj">父物体 被附着对象</param> /// <param name="childObj">子物体 待附着</param> /// <param name="pos">附着之后的localPosition</param> public static void Attach(GameObject parentObj, GameObject childObj, Vector3 pos) { childObj.transform.parent = parentObj.transform; childObj.transform.localPosition = pos; } /// <summary> /// 完成GameObject附着在父物体的参考点上的功能(无参考点则Attach) /// </summary> /// <param name="parentObj">父物体</param> /// <param name="childObj">子物体</param> /// <param name="refPointName">被附着点的名字 </param> /// <param name="pos">附着之后的localPosition</param> public static void AttachToRefPos(GameObject parentObj, GameObject childObj, string refPointName, Vector3 pos) { //Search bool isFinded = false; Transform[] child = parentObj.transform.GetComponentsInChildren<Transform>(); Transform refTransform = null; for (int i = 0; i < child.Length; i++) { if (child[i].name == refPointName)//找到参考点 { if (isFinded == true) { Debug.LogWarning(parentObj.name+"内有两个以上的参考点["+refPointName+"]"); continue; } isFinded = true; refTransform = child[i]; } } if (!isFinded) { Debug.LogWarning(parentObj.name+"内没有找到参考点["+refPointName+"]"); Attach(parentObj,childObj,pos); return; } //使用参考点作为附着点 childObj.transform.parent = refTransform; childObj.transform.localPosition = pos; childObj.transform.localScale = Vector3.one; childObj.transform.localRotation = Quaternion.Euler(Vector3.zero); } /// <summary> /// 传入string,在场景中Find /// </summary> /// <param name="gameObjectName">要找的物体名字</param> /// <returns>GameObject</returns> public static GameObject FindGameObject(string gameObjectName) //通过名字找到场景中的物体 { GameObject p = GameObject.Find(gameObjectName); if (p != null) return p; Debug.LogWarning("场景内无法找到名字为["+gameObjectName+"]的物体"); return null; } /// <summary> /// 在父物体下 通过名字找到子物体 /// </summary> /// <param name="container">父物体</param> /// <param name="gameObjectName">要找的物体名字</param> /// <returns>GameObject</returns> public static GameObject FindChildGameObject(GameObject container, string gameObjectName) { if (container == null) { Debug.LogWarning("父物体为空,无法找子物体" + gameObjectName); return null; } Transform p = null; if (container.name == gameObjectName) p = container.transform; else { Transform[] children = container.transform .GetComponentsInChildren<Transform>(); //container.GetComponentsInChildren<Transform>(); for (var i = 0; i < children.Length; i++) { //Debug.Log(children[i].name); if (children[i].name == gameObjectName) { if (p == null) p = children[i]; else Debug.LogWarning(container.name + "下找到重复的元件::" + gameObjectName); } } } if (p == null) { Debug.LogError(container.name+"下找不到元件:"+gameObjectName); return null; } return p.gameObject; } }
讯享网
UITool
讯享网using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UITool { private static GameObject m_Canvas = null; public static void ReleaseCanvas() { m_Canvas = null; } /// <summary> /// 找Canvas画布下的界面(子物体) /// </summary> /// <param name="uiName">物体名称</param> /// <returns>GameObject</returns> public static GameObject FindUIGameObject(string uiName) { if (m_Canvas == null) { m_Canvas = UnityTool.FindGameObject("Canvas"); } if (m_Canvas == null) { return null; } return UnityTool.FindChildGameObject(m_Canvas, uiName); } /// <summary> /// 获得UI原件 /// </summary> /// <param name="container">容器</param> /// <param name="uiName">带查找物体名字</param> /// <typeparam name="T">ComponentT</typeparam> /// <returns>组件T</returns> public static T GetChildUIComponent<T>(GameObject container, string uiName) where T : UnityEngine.Component { //找子物体 GameObject obj = UnityTool.FindChildGameObject(container, uiName); if (obj == null) { return null; } T componenT = obj.GetComponent<T>(); if (componenT == null) { Debug.LogWarning("[UITool]获得UI元件出错,名为["+uiName+"]并没有("+typeof(T)+")组件"); return null; } return componenT; } /// <summary> /// 在Canvas下通过名字找到Button /// </summary> /// <param name="btnName">按钮名称</param> /// <returns>Button</returns> public static Button GetButton(string btnName) { GameObject uiRoot = GameObject.Find("Canvas"); if (uiRoot == null) { Debug.LogWarning("[UITool]场景中没有UI Canvas"); return null; } //找对应的Button Transform[] children = uiRoot.transform.GetComponentsInChildren<Transform>(); for (int i = 0; i < children.Length; i++) { //Debug.Log(children[i].name); if (children[i].name == btnName) { Button btn = children[i].GetComponent<Button>(); if (btn == null) { Debug.LogWarning(btnName+"不属于一个Button,因为找不到组件"); } return btn; } } Debug.LogWarning("[UITool]画布下没有名为["+btnName+"]的Button存在"); return null; } /// <summary> /// 在Canvas下取得组件 /// </summary> /// <param name="uiName">物体名字</param> /// <typeparam name="T">组件类型T</typeparam> /// <returns>组件T</returns> public static T GetComponent<T>(string uiName) where T : UnityEngine.Component { //取得画布 GameObject uiRoot = GameObject.Find("Canvas"); if (uiRoot == null) { Debug.LogWarning("[UITool]场景中没有UI Canvas"); return null; } return GetChildUIComponent<T>(uiRoot, uiName); } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/129650.html