GameObject本身没有功能,是Unity场景里所有组件的基类,但很多时候我们需要在脚本中操作GameObject。先讲一下GameObject类包含哪些内容,其中常用的用红色标出了

Variables 变量

activeInHierarchy Is the GameObject active in the scene?
场景中的游戏对象是否激活?
activeSelf The local active state of this GameObject. (Read Only)

该游戏对象的局部激活状态。(只读)
isStatic Editor only API that specifies if a game
object is static.
如果一个游戏对象是静态仅在编辑器API指定。
layer The layer the game object is in. A layer is in the range [0…31].

游戏对象所在的层,层的范围是在[0…31]之间。
scene Scene that the GameObject is part of.
场景物体。
tag The tag of this game object.
这个游戏对象的标签。
transform The Transform attached to this GameObject. (null if there is none
attached).
附加于这个游戏对象上的变换。(如果没有则为空)

Constructors 构造器

GameObject Creates a new game object, named name.
创建一个新的游戏物体,命名为name。

Functions 函数

AddComponent Adds a component class named /className/ to the game object.

添加一个名称为className的组件到游戏对象。
BroadcastMessage Calls the method named /methodName/ on every MonoBehaviour in
this game object or any of its children.

对此游戏对象及其子对象的所有MonoBehaviour中调用名称为methodName的方法。
CompareTag Is this game object tagged with /tag/?

此游戏对象是否被标记为tag标签?
GetComponent Returns the component of Type /type/ if the game object has one
attached, null if it doesn't.
如果这个游戏对象附件了一个类型为type的组件,则返回该组件,否则为空。
GetComponentInChildren Returns the component of Type /type/ in the GameObject or any of
its children using depth first search.
返回此游戏对象或者它的所有子对象上(深度优先)的类型为type的组件。
GetComponentInParent Finds component in the parent.
从父对象查找组件。
GetComponents Returns all components of Type /type/ in the GameObject.

返回该游戏对象所有type类型的组件列表。
GetComponentsInChildren Returns all components of Type /type/ in the GameObject or any of
its children.
返回此游戏对象与其子对象所有type类型的组件。
GetComponentsInParent Returns all components of Type /type/ in the GameObject or any of
its parents.
返回此游戏对象与其父对象所有type类型的组件。
SampleAnimation Samples an animation at a given time for any animated properties.

用于任何动画剪辑在给定的时间采样动画。
SendMessage Calls the method named /methodName/ on every MonoBehaviour in
this game object.
在这个游戏物体上的所有MonoBehaviour上调用名称为methodName的方法。
SendMessageUpwards Calls the method named /methodName/ on every MonoBehaviour in
this game object and on every ancestor of the behaviour.

在这个游戏物体及其祖先物体的所有MonoBehaviour中调用名称为methodName的方法。
SetActive Activates/Deactivates the GameObject.
激活/停用此游戏对象。

Static Functions 静态函数

CreatePrimitive Creates a game object with a primitive mesh renderer and appropriate collider.
创建一个带有原型网格渲染器和适当的碰撞器的游戏对象。
Find Finds a game object by /name/ and returns it. 
找到并返回一个名字为name的游戏物体。
FindGameObjectsWithTag Returns a list of active GameObjects tagged /tag/. Returns empty array if no GameObject was found. 
返回具体tag标签的激活的游戏对象列表,如果没有找到则为空。
FindWithTag Returns one active GameObject tagged /tag/. Returns null if no GameObject was found. 
返回标记为tag的一个游戏对象,如果没有找到对象则为空。

 
下面举几个例子
 
  public static GameObject Find(string name)
    Find找到并返回一个名字为name的游戏物体,如果以name为名字的游戏物体没有被找到,则返回空.。  如果name中包含'/'字符,这个名称将被视作一个hierarchy中的路径名,比如GameObject.Find("animals/cat")就只会查到父物体名字为animals的cat物体。.这个函数只返回活动的游戏物体。  
  除非迫不得已,建议不要在每一帧中使用这个函数。因为其效率比较低,一般的做法是,在Awake或Start方法中通过Find找到,然后保存到成员变量中。
  注:Find查找时,只要是当前场景下的物体默认都可以查找到,而且不管是将脚本挂在那个对象上都可以查找到
    如果有多个同名对象,只能查找到一个
 
  那么如何查找active为false的物体呢?
    1、不要使用Find,直接在代码中通过public的变量关联物体。
    2、不要设置物体的active为false,先在游戏最开始时通过代码找到物体,然后通过代码设为false
    3、通过transform.Find
 
  下面例子演示怎么查找到名为Hand的GameObject物体
using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

    public GameObject hand;

    void Example() {

        hand = GameObject.Find("Hand");

        hand = GameObject.Find("/Hand");

        hand = GameObject.Find("/Monster/Arm/Hand");

        hand = GameObject.Find("Monster/Arm/Hand");

    }

}

  FindWithTag用法与此类似,这里就不举例了

  public Component GetComponent(Type type)

  type 表示检索数组的类型

  如果这个游戏对象附件了一个类型为type的组件,则返回该组件,否则为空。

using UnityEngine;

public class GetComponentExample : MonoBehaviour
{
void Start()
{
HingeJoint hinge = gameObject.GetComponent(typeof(HingeJoint)) as HingeJoint; if (hinge != null)
hinge.useSpring = false;
}
}

  

  public T GetComponent()   

using UnityEngine;

public class GetComponentGenericExample : MonoBehaviour
{
void Start()
{
HingeJoint hinge = gameObject.GetComponent<HingeJoint>(); if (hinge != null)
hinge.useSpring = false;
}
}

  public Component GetComponent(string type)

    type 表示检索数组的类型

       如果这个游戏对象附件了一个类型为type的组件,则返回该组件,否则为空。

using UnityEngine;

public class GetComponentNonPerformantExample : MonoBehaviour
{
void Start()
{
HingeJoint hinge = gameObject.GetComponent("HingeJoint") as HingeJoint; if (hinge != null)
hinge.useSpring = false;
}
}

  

  当你把脚本挂在一个物体上之后,脚本中的gameObject代表该物体,例如可以通过gameObject.name来获取脚本挂载对象的名字

 
 

U3D GameObject 解读的更多相关文章

  1. U3D中GameObject.Find无法找到元件

    U3D中GameObject.Find 如果某元件SetActive(false)了,Find()无法找到 因为Find()只会帮你找出正在活动中的物件,所以在将物件关闭前,我们必须将此物件放至预先定 ...

  2. u3d一个GameObject绑定两个AudioSource

    u3d 一个GameObject绑定两个AudioSource  ,使他们分别播放,并控制 using UnityEngine; using System.Collections; public cl ...

  3. U3D NGUI改变GameObject Activity闪烁的问题

    不是关闭再激活GameObject会闪烁,而是再激活时,NGUI渲染步骤不一致导致的闪烁. 并且文字激活后渲染要慢一帧,如果延迟一帧处理,又会导致精灵图片快一帧,图片重叠.这个测试结果不一定准确,先记 ...

  4. U3D——Unity3D的脚本-script入门

     Unity3D的基本操作非常easy就能掌握了,接下来就是游戏系统的核心部分:脚本. 什么是Script(脚本)?简而言之,就是使用代码来运行一系列动作命令的特殊文本,它须要编译器来从新解读.U ...

  5. u3d中的INput

    属性 属性: 功能: 轴 (Axes) 包含当前工程的所有定义的输入轴:数目 (Size) 该工程中不同输入轴的数量,元素 0.1.... 是要修改的特定的轴. 名称 (Name) 在游戏启动器中以及 ...

  6. U3D笔记11:47 2016/11/30-15:15 2016/12/19

    11:47 2016/11/30Before you can load a level you have to add it to the list of levels used in the gam ...

  7. U3D杂记

    1,点击UI上的登录按钮,从脚本发出ioo.netmanager.SendConnet->进入CS->soketclient.sendconnet...->netmanager调用 ...

  8. [U3D 导出Xcode工程包,用Xcode给U3D脚本传递参数]

    1.导出Xcode工程 File->Building and setting,导出IOS工程(有错误会失败) 2.运行Xcode工程,在Classes文件夹的UI文件夹里,早到UnityAppC ...

  9. [U3D 画起重机,绑脚本和控制它运动的基本操作]

    之前在学习Unity3D,不知为何网上的教学资源真是少啊...我某段时间还卡在不知如何让物体绑个脚本自动运动.. 之所以要学习U3D是因为导师让我做的IOS项目里有个需要模拟起重机,从而控制真实起重机 ...

随机推荐

  1. 学习爬虫Scrapy遇到的错误

    1. 这个问题是由于路径中含有中文,导致编码格式出错的问题, 查看错误方法,进入到ntpath.py文件中去,找到85行,然后,print 一下result_path,因为后面报出的错误,就是因为这段 ...

  2. Nodejs之路:异步I/O的过程

    对于Node中的异步I/O调用,从发出调用到回调执行,看起来像普通的js异步,但是流程却和普通js那些消息队列完全不同,整个过程经历了哪些? 下面以Windows平台下为例: 一,异步调用第一阶段: ...

  3. 2019.02.16 spoj Query on a tree IV(链分治)

    传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...

  4. mysql8.0 linux 安装

    1.下载 mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz 2.解压 tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.ta ...

  5. 简单易用的图像库stb_image

    最近又回到了选择图像库的老问题上,之前用过FreeImage,DevIL,libpng. FreeImage的问题是他的Licence有点迷,双证书,除了GPL还有个自己的没读懂,不商用的话随便折腾, ...

  6. Hibernate 基础入门

    概述: JDBC:将java和数据库连接的驱动程序加载到项目中,在代码里面,注册驱动,创建链接,创建satement对象,发送并执行sql之类,关闭连接. hibernate :数据持久化一个框架,对 ...

  7. php流程控制

    return 例子一: <?php function add($a,$b){ echo "echo"; return $a+$b; //return 一般用于function ...

  8. 【repost】H5总结

    1.新增的语义化标签: <nav>: 导航 <header>: 页眉 <footer>: 页脚 <section>:区块 <article> ...

  9. Python学习第四章

    1.类和对象: 类对象支持两种操作:属性引用和实例化. 属性引用:obj.name 构造方法:类会定义一个名为__int__()的特殊方法如下 def  __init__(self):       s ...

  10. Vuejs(14)——在v-for中,利用index来对第一项添加class

    版权声明:出处http://blog.csdn.net/qq20004604 (1)在v-for中,利用index来对第一项添加class <a class="list-group-i ...