C# GameObject

GameObject 游戏对象

Unity场景里面所有实体的基类

GameObject.activeInHierarchy 在层级激活

场景中的游戏对象是否激活

bool activeInHierarchy;

GameObject.activeSelf 激活自己

该游戏对象的局部激活状态

bool activeSelf;

GameObject.AddComponent 添加组件

添加一个名称为className的组件到游戏对象

public SphereCollider sc;
void Example() {
    sc = gameObject.AddComponent("SphereCollider") as SphereCollider;
}

GameObject.animation 动画

附加于这个游戏对象上的动画

public GameObject other;
void Example() {
    other.animation.Play();
}

GameObject.audio 音频

附加于这个游戏对象上的音频资源

public GameObject other;
void Example() {
    other.audio.Play();
}

GameObject.BroadcastMessage 广播消息

对此游戏对象及其子对象的所有MonoBehaviour中调用名称为methodName的方法

void ApplyDamage(float damage) {
    print(damage);
}
void Example() {
    gameObject.BroadcastMessage("ApplyDamage", 5.0F);
}

GameObject.camera 摄像机

附加于这个游戏物体上的相机

public GameObject other;
void Example() {
    other.camera.fieldOfView = 45;
}

GameObject.collider 碰撞器

附加于这个游戏物体上的碰撞体

public GameObject other;
void Example() {
    other.collider.material.dynamicFriction = 1;
}

GameObject.collider2D 二维碰撞器

附加于此对象的2D碰撞器组件

Collider2D collider2D;

GameObject.CompareTag 比较标签

此游戏对象是否被标记为tag标签

void OnTriggerEnter(Collider other) {
    if (other.gameObject.CompareTag("Player"))
        Destroy(other.gameObject);
}

GameObject.constantForce 恒力

附加于这个游戏对象上的恒定的力

public GameObject other;
void Example() {
    other.constantForce.relativeForce = new Vector3(0, 0, 1);
}

GameObject.CreatePrimitive 创建原型对象

创建一个带有原型网格渲染器和适当的碰撞器的游戏对象

void Start() {
    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = new Vector3(0, 0.5F, 0);
    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    sphere.transform.position = new Vector3(0, 1.5F, 0);
    GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    capsule.transform.position = new Vector3(2, 1, 0);
    GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    cylinder.transform.position = new Vector3(-2, 1, 0);
}

GameObject.Find 查找

找到并返回一个名字为name的游戏物体

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");
}

GameObject.FindGameObjectsWithTag 通过标签查找游戏对象列表

返回具体tag标签的激活的游戏对象列表,如果没有找到则为空

public GameObject respawnPrefab;
public Object respawns;
void Start() {
    if (respawns == null)
        respawns = GameObject.FindGameObjectsWithTag("Respawn");
    foreach (Object respawn in respawns) {
        Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
    }
}

GameObject.FindWithTag 查找标签

返回标记为tag的一个游戏对象,如果没有找到对象则为空

public GameObject respawnPrefab;
public GameObject respawn;
void Start() {
    if (respawn == null)
        respawn = GameObject.FindWithTag("Respawn");
    Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
}

GameObject.GameObject 游戏对象

创建一个新的游戏物体,命名为name

public GameObject player;
void Example() {
    player = new GameObject("Player");
    player.AddComponent("Rigidbody");
    player.AddComponent("BoxCollider");
}

GameObject.GetComponent 获取组件

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

public HingeJoint hinge;
void Example() {
    hinge = gameObject.GetComponent<HingeJoint>();
    hinge.useSpring = false;
}

GameObject.GetComponentInChildren 获取子对象组件

返回此游戏对象或者它的所有子对象上(深度优先)的类型为type的组件

public HingeJoint hinge;
void Example() {
    hinge = gameObject.GetComponentInChildren<HingeJoint>();
    hinge.useSpring = false;
}

GameObject.GetComponentInParent 获取父对象组件

从父对象查找组件

public HingeJoint hinge;
void Example() {
    hinge = gameObject.GetComponentInParent<HingeJoint>();
    hinge.useSpring = false;
}

GameObject.GetComponents 获取组件列表

返回该游戏对象所有type类型的组件列表

public HingeJoint[] hingeJoints;
void Example() {
    hingeJoints = gameObject.GetComponents<HingeJoint>();
    foreach (HingeJoint joint in hingeJoints) {
        joint.useSpring = false;
    }
}

GameObject.GetComponentsInChildren 获取子对象组件列表

返回此游戏对象与其子对象所有type类型的组件

public HingeJoint[] hingeJoints;
void Example() {
    hingeJoints = gameObject.GetComponentsInChildren<HingeJoint>();
    foreach (HingeJoint joint in hingeJoints) {
        joint.useSpring = false;
    }
}

GameObject.GetComponentsInParent 获取父对象组件列表

返回此游戏对象与其父对象所有type类型的组件

public HingeJoint[] hingeJoints;
void Example() {
    hingeJoints = gameObject.GetComponentsInParent(typeof(HingeJoint));
    foreach (HingeJoint joint in hingeJoints) {
        joint.useSpring = false;
    }
}

GameObject.guiText 界面文本

附加于这个游戏对象上的GUIText

public GameObject other;
void Example() {
    other.guiText.text = "HelloWorld";
}

GameObject.guiTexture 界面纹理

附加于这个游戏对象上的GUITexture

void Example() {
    guiTexture.color = Color.blue;
}

GameObject.hingeJoint 铰链关节

附加于这个游戏对象上的铰链关节

public GameObject other;
void Example() {
    other.hingeJoint.spring.targetPosition = 70;
}

GameObject.isStatic 是否静态

如果一个游戏对象是静态仅在编辑器API指定

bool isStatic;

GameObject.layer 层

游戏对象所在的层,层的范围是在[0…31]之间

void Example() {
    gameObject.layer = 2;
}

GameObject.light 灯光

附加于这个游戏物体上的灯光

public GameObject other;
void Example() {
    other.light.range = 10;
}

GameObject.networkView 网络视图

附加于这个游戏对象上的网络视图

public GameObject other;
void Example() {
    other.networkView.RPC("MyFunction", RPCMode.All, "someValue");
}

GameObject.particleEmitter 粒子发射器

附加于这个游戏对象上的粒子发射器

public GameObject other;
void Example() {
    other.particleEmitter.emit = true;
}

GameObject.particleSystem 粒子系统

附加于这个游戏对象上的粒子系统

public GameObject other;
void Example() {
    other.particleSystem.Emit(5);
}

GameObject.renderer 渲染器

附加于这个游戏对象上的渲染器

public GameObject other;
void Example() {
    other.renderer.material.color = Color.green;
}

GameObject.rigidbody 刚体

附加于这个游戏对象上的刚体

public GameObject other;
void Example() {
    other.rigidbody.AddForce(1, 1, 1);
}

GameObject.SampleAnimation 采样动画

用于任何动画剪辑在给定的时间采样动画

public AnimationClip clip;
void Update() {
    gameObject.SampleAnimation(clip, clip.length - Time.time);
}

GameObject.scene 场景

场景物体

public SceneManagement.Scene scene;

GameObject.SendMessage 发送消息

在此游戏对象所有MonoBehaviour上调用名称为methodName的方法

void ApplyDamage(float damage) {
    print(damage);
}
void Example() {
    gameObject.SendMessage("ApplyDamage", 5.0F);
}

GameObject.SendMessageUpwards 向上发送消息

调用此游戏对象及其behaviour的父对象上所有MonoBehaviour名为methodName的方法

void ApplyDamage(float damage) {
    print(damage);
}
void Example() {
    gameObject.SendMessageUpwards("ApplyDamage", 5.0F);
}

GameObject.SetActive 设置激活

激活/停用此游戏对象

void SetActive(bool value);

GameObject.tag 标签

这个游戏对象的标签

void Example() {
    gameObject.tag = "Player";
}

GameObject.transform 变换

附加于这个游戏对象上的变换

public GameObject other;
void Example() {
    other.transform.Translate(1, 1, 1);
}
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值