什么方法可以inode 无法找到场景景下所有的GameObject

遍历父物体下所有子物体的几种方式
1、通过标签来寻找
void ForeachObjs()
//GameObject objs[] = GameObject
objs = GameObject.FindGameObjectsWithTag(&Cube_00&);
foreach (GameObject obj in objs)
obj.AddComponent(&OnTiggerEnterSendMessage&);
}2、通过Transform来寻找并附加脚本
&foreach (Transform child in gameObject.transform)
child.gameObject.AddComponent(&OnTiggerEnterSendMessage&);
}3、另外一种方式(转)
//把这个函数放到你的代码中 check代表你要查询的物体 name为名称 如return GetTransform(transform,&bone12&);
Transform GetTransform(Transform check,string name)
foreach (Transform t in check.GetComponentsInChildren&Transform&())
if(t.name==name){}
GetTransform(t,name);
}4、(转)
很多高级游戏代码不仅仅控制单一物体。Unity脚本接口有很多方法获得和访问其他游戏物体和组件。下面我们假设有一个名为OtherScript.js的脚本附属于场景中的游戏物体。
var foo = 5;
function DoSomething ( param : String) {
print(param + & with foo: & + foo);
}
1.使用检视视图指定参数。
你能通过检视视图为一些游戏类型指定变量:
// Translate the object dragged on the target slot
var target : T
function Update () {
target.Translate(0, 1, 0);
}
也能显示参数在其他物体检视视图。下面你能在Inspector面板中拖拽包含OtherScript的游戏物体到target上。
// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherS
function Update () {
// Set foo variable of the target object
target.foo = 2;
// Call do something on the target
target.DoSomething(&Hello&);
}
2.通过继承结构
你能通过Transform组件获得一个子和父物体到现在的物体。
// Find the child &Hand& of the game object
// we attached the script to
transform.Find(&Hand&).Translate(0, 1, 0);
你只要在hierarchy面板建立了transform,你就能用GetComponent去获取other scripts。
// Find the child named &Hand&.
// On the OtherScript attached to it, set foo to 2.
transform.Find(&Hand&).GetComponent(OtherScript).foo = 2;
// Find the child named &Hand&.
// Call DoSomething on the OtherScript attached to it.
transform.Find(&Hand&).GetComponent(OtherScript).DoSomething(&Hello&);
// Find the child named &Hand&.
// Then apply a force to the rigidbody attached to the hand.
transform.Find(&Hand&).rigidbody.AddForce(0, 10, 0);
你可以循环到它所有的子物体。
// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.Translate(0, 1, 0);
}
查看文档Transform类 可以获得更多信息。
3.通过名字或标签
你能用确定的标签搜索物体,使用GameObject.FindWithTag 和GameObject.FindGameObjectsWithTag。使用GameObject.Find通过名字获得游戏物体。
function Start ()
{
// By name
var go = GameObject.Find(&SomeGuy&);
go.transform.Translate(0, 1, 0);
// By tag
var player = GameObject.FindWithTag(&Player&);
player.transform.Translate(0, 1, 0);
}
你能使用GetComponent获得脚本或组件在找到的游戏物体上。
function Start ()
{
// By name
var go = GameObject.Find(&SomeGuy&);
go.GetComponent(OtherScript).DoSomething();
// By tag
var player = GameObject.FindWithTag(&Player&);
player.GetComponent(OtherScript).DoSomething();
}
一些特殊对象如主摄像机有快捷方式,可以使用Camera.main。
4.传递参数
一些事件中包含详细的事件。例如:扳机事件传递(碰撞)Collider组件到碰撞物体的handler函数。
OnTriggerStay给我们一个碰撞参数.从碰撞体我们能给他赋一个刚体。
function OnTriggerStay( other : Collider ) {
// If the other collider also has a rigidbody
// apply a force to it!
if (other.rigidbody) {
other.rigidbody.AddForce(0, 2, 0);
}
}
或者我们可以获取游戏中碰撞者包含的任意组件。
function OnTriggerStay( other : Collider ) {
// If the other collider has a OtherScript attached
// call DoSomething on it.
// Most of the time colliders won't have this script attached,
// so we need to check first to avoid null reference exceptions.
if (other.GetComponent(OtherScript)) {
other.GetComponent(OtherScript).DoSomething();
}
}
注意上面的例子,使用其他变量上的后缀,你能获取一些碰撞物体里的组件。
5.某个类型的所有脚本
从一个类找到任意对象或脚本,用Object.FindObjectsOfType或获得某个类型的第一个物体使用Object.FindObjectOfType.
function Start ()
{
// Find the OtherScript which is attached to any game object in the scene.
var other : OtherScript = FindObjectOfType(OtherScript);
other.DoSomething();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5、自己写的深度优先遍历,找物体的孩子并做些什么事情
using UnityE
using System.C
using UnityE
public class ChangeMeshRenderQuality : Editor
[MenuItem(&GameObject/ChangeMeshRenderQuality&)]
static void FindChildMeshRender()
Debug.LogWarning(&Change Now!!!&);
Transform[] selection =
Selection.GetTransforms( SelectionMode.TopLevel | SelectionMode.Editable);
foreach(Transform t in selection)
ChangeQuality(t);
static void ChangeQuality(Transform T)
for(int i = 0 ; i & T.childC i ++)
Transform childTransform = T.GetChild(i);
if(childTransform.GetComponent&SkinnedMeshRenderer&() != null)
childTransform.GetComponent&SkinnedMeshRenderer&().quality = SkinQuality.Bone4;
ChangeQuality(childTransform);
// Disable the menu if there is nothing selected
// 如果什么都没有选择将禁用菜单功能
[MenuItem (&GameObject/ChangeMeshRenderQuality&, true)]
static bool ValidateSelection()
return Selection.activeGameObject !=
共1条评分,
常用的功能。
要评论请先&或者&
常用的功能。
感谢分享!这些代码段够实用!
:常用的功能。 谢谢鹰大的关注~~~
不错的总结,顶起
其实可以考虑用插件来传值
Mark &学习
感谢分享,正在做个多级菜单不知道有什么好方法= =Unity3d中如何查找一个脚本被挂在那些预设上面? - 知乎12被浏览3658分享邀请回答41 条评论分享收藏感谢收起[MenuItem("Assets/Tool/GetReference")]
static void GetReference()
string target = "";
if (Selection.activeObject != null)
target = AssetDatabase.GetAssetPath(Selection.activeObject);
if (string.IsNullOrEmpty(target))
string[] files = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);
string[] scene = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);
List&Object& filelst = new List&Object&();
for (int i = 0; i & files.Length; i++)
string[] source = AssetDatabase.GetDependencies(new string[] { files[i].Replace(Application.dataPath, "Assets") });
for (int j = 0; j & source.Length; j++)
if (source[j] == target)
filelst.Add(AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets")));
for (int i = 0; i & scene.Length; i++)
string[] source = AssetDatabase.GetDependencies(new string[] { scene[i].Replace(Application.dataPath, "Assets") });
for (int j = 0; j & source.Length; j++)
if (source[j] == target)
filelst.Add(AssetDatabase.LoadMainAssetAtPath(scene[i].Replace(Application.dataPath, "Assets")));
Selection.objects = filelst.ToArray();
0添加评论分享收藏感谢收起> Gameobject Find
Unity合并网格和贴图,最近项目中由于场景中的小物件比较多导致在进入场景的时候DrawCall
做战舰转向如果转向的速度是缓慢的。可以使用Quaternion Slerp 但是这个转向插& 20540;,是无
在上一篇中,我们介绍了各种游戏对象的功能及类的集成关系,现在我们来看看GameObject的
transform是你所需要找的物体gameobject的transform。这个for循环就相当于遍历了它的子对象,此
1 想找到层级面板中某个物体,并销毁,利用下面的代码:GameObject obj = GameObject Find(&所
OPPO Find5:[2]如何设置定时开关机,第一,打开手机,在系统菜单中找到【设置】,大多数
Could not find class ‘Ice InitializationData’, referenced from method com zeroc hello HelloApp initializeCommunicato
Linux基础命令-find常见用法示例。
【leetcode】combination-sum,题目描述:Given a set of candidate numbers (C) and a target number (T), find all un
继续探讨linux文件权限的问题以及find指令的选项。
linux find命令详解。find命令功能强大,所以他的选项众多,他消耗资源多,所以他的工作效
OPPO旗舰大招:OPPO神机Find9今年终于要来了。尽管人们对于OPPO的更多印象是以广告、渠道制
locate,grep,find命令。locate 命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来
win7安装docker报错:error during connect: Get http
the system cannot find the file specified。因为是win7
查看OPPO Find 7手机固件版本号的方法:点击“设置”。在“常规”,向上滑屏,找到“关于
jquery中的find()方法。如果想要在某个元素下寻找特定的元素,就必须对 $( " id ") find( " chi
每天一个 Linux 命令(21):find命令之xargs。
cmd关闭系统进程:打开命令提示符,输入“netstat -an | find “8080””(不带引号输入),发
论find_in_set(str,strlist)参数单引号的重要性,数据中有guestId 为0001这条记录,但用mysql 命令死
Unable to find vcvarsall bat的错误的解决办法,实测可用,先安装好wheel: pip install wheel,实测可
Leetcode题解:513
Find Bottom Left Tree Value
Linux中find常见用法示例。$find path -option [ -print ] [ -exec -ok command ] {}。
linux运维知识之命令学习。find命令是一个功能非常强大的搜索命令,能按照文件的名字,所
Find QTime Limit: 2000HDU 5907 Find Q(水) —— BestCoder Round
88。 1000 MS (Java Others)Memory Lim
linux常用命令-find基本用法。find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [
关于cocos2dx加载不到libcocos2dcpp so:有时候打包是发现cound not find libcocos2dcpp so库。这是因为
浅谈MongoDB中几种不同查询方法。1 find,MongoDB使用find来进行查询 查询就是返回一个集合中
linux find详解,在文件树种查找文件,并作出相应的处理find详细信息 find -atime -2查找指定时
PAT甲级 1002
A+B for Polynomials (25)。This time, you are supposed to find A+B where A and B are two polynomials
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) fr
热门文章热门标签
07月26日 |
07月26日 |
07月26日 |
07月26日 |
07月26日 |
07月26日 |
07月26日 |
07月26日 |

我要回帖

更多关于 天河山所有场景图片 的文章

 

随机推荐