青乌子笔记UnityTool工具

来源与《设计模式与游戏完美开发》
UnityTool.cs

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值