zoukankan      html  css  js  c++  java
  • Unity3D_06_根据Transform、GameObject和Tag获取子对象集合

    导引:

    因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 transform 或者 gameobject 调用这些方法就可以快速的得到这些数据,而并不需要自己在每个单独的类里面都写上一遍。

    代码如下:

      1 using System;  
      2 using System.Collections.Generic;  
      3 using System.Linq;  
      4 using System.Text;  
      5 using UnityEngine;  
      6 
      7 
      8 public static partial class ExtentionMethod  
      9 {  
     10     /// <summary>  
     11     /// 获取子对象变换集合  
     12     /// </summary>  
     13     /// <param name="obj"></param>  
     14     /// <returns></returns>  
     15     public static List<Transform> GetChildCollection(this Transform obj)  
     16     {  
     17         List<Transform> list = new List<Transform>();  
     18         for (int i = 0; i < obj.childCount; i++)  
     19         {  
     20             list.Add(obj.GetChild(i));  
     21         }  
     22         return list;  
     23     }  
     24 
     25     /// <summary>  
     26     /// 获取子对象集合  
     27     /// </summary>  
     28     /// <param name="obj"></param>  
     29     /// <returns></returns>  
     30     public static List<GameObject> GetChildCollection(this GameObject obj)  
     31     {  
     32         var list = obj.transform.GetChildCollection();  
     33         return list.ConvertAll(T => T.gameObject);  
     34     }  
     35 
     36     public static Transform GetRootParent(this Transform obj)  
     37     {  
     38         Transform Root = obj.parent;  
     39         while(Root.parent != null)  
     40         {  
     41             //Root = Root.root;   //transform.root,方法可以直接获取最上父节点。  
     42             Root = Root.parent;  
     43         }  
     44         return Root;  
     45     }  
     46 
     47     /// <summary>  
     48     /// 把源对象身上的所有组件,添加到目标对象身上  
     49     /// </summary>  
     50     /// <param name="origin">源对象</param>  
     51     /// <param name="target">目标对象</param>  
     52     public static void CopyComponent(GameObject origin, GameObject target)  
     53     {  
     54         var originComs = origin.GetComponents<Component>();  
     55         foreach (var item in originComs)  
     56         {  
     57             target.AddComponent(item.GetType());  
     58         }  
     59     }  
     60 
     61     /// <summary>  
     62     /// 改变游戏脚本  
     63     /// </summary>  
     64     /// <param name="origin"></param>  
     65     /// <param name="target"></param>  
     66     public static void ChangeScriptTo(this MonoBehaviour origin, MonoBehaviour target)  
     67     {  
     68         target.enabled = true;  
     69         origin.enabled = false;  
     70     }  
     71 
     72 
     73     /// <summary>  
     74     /// 从当前对象的子对象中查找,返回一个用tag做标识的活动的游戏物体的链表.如果没有找到则为空.   
     75     /// </summary>  
     76     /// <param name="obj">对象Transform</param>  
     77     /// <param name="tag">标签</param>  
     78     /// <param name="transList">结果Transform集合</param> // 对一个父对象进行递归遍历,如果有子对象的tag和给定tag相符合时,则把该子对象存到 链表数组中  
     79     public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref List<Transform> transList)  
     80     {  
     81         foreach (var item in obj.transform.GetChildCollection())  
     82         {  
     83             // 如果子对象还有子对象,则再对子对象的子对象进行递归遍历  
     84             if (item.childCount > 0)  
     85             {  
     86                 item.FindGameObjectsWithTagRecursive(tag, ref transList);  
     87             }  
     88 
     89             if (item.tag == tag)  
     90             {  
     91                 transList.Add(item);  
     92             }  
     93         }  
     94     }  
     95 
     96     public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref List<GameObject> objList)  
     97     {  
     98         List<Transform> list = new List<Transform>();  
     99         obj.transform.FindGameObjectsWithTagRecursive(tag, ref list);  
    100 
    101         objList.AddRange(list.ConvertAll(T => T.gameObject));  
    102     }  
    103 
    104     /// <summary>  
    105     /// 从父对象中查找组件  
    106     /// </summary>  
    107     /// <typeparam name="T">组件类型</typeparam>  
    108     /// <param name="com">物体组件</param>  
    109     /// <param name="parentLevel">向上查找的级别,使用 1 表示与本对象最近的一个级别</param>  
    110     /// <param name="searchDepth">查找深度</param>  
    111     /// <returns>查找成功返回相应组件对象,否则返回null</returns>  
    112     public static T GetComponentInParent<T>(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) where T : Component  
    113     {  
    114         searchDepth--;  
    115 
    116         if (com != null && searchDepth > 0)  
    117         {  
    118             var component = com.transform.parent.GetComponent<T>();  
    119             if (component != null)  
    120             {  
    121                 parentLevel--;  
    122                 if (parentLevel == 0)  
    123                 {  
    124                     return component;  
    125                 }  
    126             }  
    127 
    128             return com.transform.parent.GetComponentInParent<T>(parentLevel, searchDepth);  
    129         }  
    130 
    131         return null;  
    132     }      
    133 }  

    补充:Unity中三种调用其他脚本函数的方法

    第一种:被调用脚本函数为static类型,调用时直接用 脚本名.函数名()。很不实用~

    第二种:GameObject.Find(“脚本所在物体名”).SendMessage(“函数名”); 此种方法可以调用public和private类型函数

    第三种:GameObject.Find(“脚本所在物体名”).GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数

  • 相关阅读:
    观《逻辑思维,如何成为一个高手》
    第十八周 最后冲刺
    第十六周 个人项目开发流程
    个人作业
    第十四周个人总结
    排球比赛计分程序功能说明书
    Spring实战(1)
    java基础知识(三)
    JAVA中框架总结
    java基础知识(一)
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/8967976.html
Copyright © 2011-2022 走看看