遍历父物体下所有子物体的几种方式
来源:互联网 发布:淘宝的服务器有多大 编辑:程序博客网 时间:2024/05/22 14:43
1、通过标签来寻找
复制代码2、通过Transform来寻找并附加脚本
复制代码3、另外一种方式(转)
复制代码4、Unity 访问其他游戏物体的几种方法(转)
很多高级游戏代码不仅仅控制单一物体。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 : Transform;
function Update () {
target.Translate(0, 1, 0);
}
也能显示参数在其他物体检视视图。下面你能在Inspector面板中拖拽包含OtherScript的游戏物体到target上。
// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherScript;
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、自己写的深度优先遍历,找物体的孩子并做些什么事情
复制代码
复制代码
1
2
3
4
5
6
7
8
9
void
ForeachObjs()
{
//GameObject objs[] = GameObject
objs = GameObject.FindGameObjectsWithTag(
"Cube_00"
);
foreach
(GameObject obj
in
objs)
{
obj.AddComponent(
"OnTiggerEnterSendMessage"
);
}
}
复制代码
1
2
3
4
foreach
(Transform child
in
gameObject.transform)
{
child.gameObject.AddComponent(
"OnTiggerEnterSendMessage"
);
}
复制代码
1
2
3
4
5
6
7
8
9
10
11
//把这个函数放到你的代码中 check代表你要查询的物体 name为名称 如return GetTransform(transform,"bone12");
Transform GetTransform(Transform check,
string
name)
{
foreach
(Transform t
in
check.GetComponentsInChildren<Transform>())
{
if
(t.name==name){
return
t;}
GetTransform(t,name);
}
return
null
;
}
很多高级游戏代码不仅仅控制单一物体。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 : Transform;
function Update () {
target.Translate(0, 1, 0);
}
也能显示参数在其他物体检视视图。下面你能在Inspector面板中拖拽包含OtherScript的游戏物体到target上。
// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherScript;
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、自己写的深度优先遍历,找物体的孩子并做些什么事情
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using
UnityEngine;
using
System.Collections;
using
UnityEditor;
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.childCount; 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 !=
null
;
}
}
0 0
- 遍历父物体下所有子物体的几种方式
- 遍历父物体下所有子物体的几种方式
- 【Unity3D自学记录】遍历父物体下所有子物体
- Unity3D清除父物体下的所有子物体
- 【Unity】删除所有子物体保留父物体的2种方式
- Unity删除所有子物体保留父物体的2种方式
- 删除所有子物体保留父物体的2种方式
- 遍历所有子物体的方法
- unity获取物体下的所有子物体
- Unity中遍历所有子物体
- 遍历物体所有子级(递归)
- 旋转物体的几种方式
- 【新手学习笔记】我总结的unity遍历所有子物体的两种简单方法
- 获取父物体下面的子物体
- 控制unity对象下所有子物体的显示状态
- 运用递归查找对象下的所有子物体
- Unity3d获取物体的所有子物体对象(child)
- Unity3D获取物体的所有子物体对象(child)
- [leetcode] 386. Lexicographical Numbers
- 百度地图Sdk集成的使用
- 前后台切换时闪屏处理
- 推广配置表
- 初学Python,从认识Python、安装Python开始
- 遍历父物体下所有子物体的几种方式
- 无聊写个手势插件
- 矩阵
- 函数式编程与高阶函数
- 实现FreeBSD 与Linux Mint 多系统共存的安装
- IPython 交互式调试器
- Windows:Windows桌面显示计算机、回收站、用户文件等
- AndroidStudio将logcat打印到指定文件
- 【学习相关】:研究生报考简单资料整理,会更新