怎么获取一个gamefindobjectoftype中的某个script中的变量

5480人阅读
Unity(5)
&&&&&& 因为项目中难免要多次进行获取子对象或者子对象的集合,所以同事之前写了一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 transform 或者 gameobject& 调用这些方法就可以快速的得到这些数据,而并不需要自己在每个单独的类里面都写上一遍。
&&&&&& 不想偷懒的程序员不是好的程序员。代码如下:
using System.Collections.G
using System.L
using System.T
using UnityE
public static partial class ExtentionMethod
/// &summary&
/// 获取子对象变换集合
/// &/summary&
/// &param name=&obj&&&/param&
/// &returns&&/returns&
public static List&Transform& GetChildCollection(this Transform obj)
List&Transform& list = new List&Transform&();
for (int i = 0; i & obj.childC i++)
list.Add(obj.GetChild(i));
/// &summary&
/// 获取子对象集合
/// &/summary&
/// &param name=&obj&&&/param&
/// &returns&&/returns&
public static List&GameObject& GetChildCollection(this GameObject obj)
var list = obj.transform.GetChildCollection();
return list.ConvertAll(T =& T.gameObject);
public static Transform GetRootParent(this Transform obj)
Transform Root = obj.
while(Root.parent != null)
//Root = Root.
//transform.root,方法可以直接获取最上父节点。
Root = Root.
/// &summary&
/// 把源对象身上的所有组件,添加到目标对象身上
/// &/summary&
/// &param name=&origin&&源对象&/param&
/// &param name=&target&&目标对象&/param&
public static void CopyComponent(GameObject origin, GameObject target)
var originComs = origin.GetComponents&Component&();
foreach (var item in originComs)
target.AddComponent(item.GetType());
/// &summary&
/// 改变游戏脚本
/// &/summary&
/// &param name=&origin&&&/param&
/// &param name=&target&&&/param&
public static void ChangeScriptTo(this MonoBehaviour origin, MonoBehaviour target)
target.enabled =
origin.enabled =
/// &summary&
/// 从当前对象的子对象中查找,返回一个用tag做标识的活动的游戏物体的链表.如果没有找到则为空.
/// &/summary&
/// &param name=&obj&&对象Transform&/param&
/// &param name=&tag&&标签&/param&
/// &param name=&transList&&结果Transform集合&/param& // 对一个父对象进行递归遍历,如果有子对象的tag和给定tag相符合时,则把该子对象存到 链表数组中
public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref List&Transform& transList)
foreach (var item in obj.transform.GetChildCollection())
// 如果子对象还有子对象,则再对子对象的子对象进行递归遍历
if (item.childCount & 0)
item.FindGameObjectsWithTagRecursive(tag, ref transList);
if (item.tag == tag)
transList.Add(item);
public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref List&GameObject& objList)
List&Transform& list = new List&Transform&();
obj.transform.FindGameObjectsWithTagRecursive(tag, ref list);
objList.AddRange(list.ConvertAll(T =& T.gameObject));
/// &summary&
/// 从父对象中查找组件
/// &/summary&
/// &typeparam name=&T&&组件类型&/typeparam&
/// &param name=&com&&物体组件&/param&
/// &param name=&parentLevel&&向上查找的级别,使用 1 表示与本对象最近的一个级别&/param&
/// &param name=&searchDepth&&查找深度&/param&
/// &returns&查找成功返回相应组件对象,否则返回null&/returns&
public static T GetComponentInParent&T&(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) where T : Component
searchDepth--;
if (com != null && searchDepth & 0)
var component = com.transform.parent.GetComponent&T&();
if (component != null)
parentLevel--;
if (parentLevel == 0)
return com.transform.parent.GetComponentInParent&T&(parentLevel, searchDepth);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:16998次
排名:千里之外
(1)(4)(2)(1)unity(4)
调用其他实例的脚本是一种很常用的需求,在Unity3D中实现起来也有多种方法,假设在ScriptB有一个DoSomething函数,如下所示:
using UnityE
public class ScriptB : MonoBehaviour {
public void DoSomething() {
Debug.Log(&Hi there&);
在ScriptA中如果要访问某个GameObject的ScriptB脚本中的DoSomething函数:
using UnityE
public class ScriptA : MonoBehaviour {
public ScriptB
void Update() {
other.DoSomething();
注意ScriptA有一个ScriptB类型的公共变量,这是一个绝妙的小技巧,如此一来你便可以通过编辑器拖拽任何含有ScriptB这一脚本的GameObject至该变量上(因为是public的,所以暴露于编辑器界面中),Unity会自动识别你需要赋值的不是那个GameObject,而是它所包含的ScriptB脚本。
除此之外,如果你不喜欢这种在编辑器中直接赋值的办法,你可以通过GameObject.Find(“somename”) 来获取GameObject:
GameObject go = GameObject.Find(&somegameobjectname&);
ScriptB other = (ScriptB) go.GetComponent(typeof(ScriptB));
other.DoSomething();
不过需要注意的是GameObject.Find()的执行效率非常低,所以如果可能的话还是尽可能使用“直接赋值法”吧。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:34008次
积分:1454
积分:1454
排名:千里之外
原创:94篇
转载:109篇
(3)(15)(18)(12)(17)(26)(52)(28)(8)(6)(3)(4)(1)(2)(4)(2)(3)本帖子已过去太久远了,不再提供回复功能。}

我要回帖

更多关于 findobjectoftype 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信