Unity Gameobject类测试

Unity 学习笔记汇总
官方API使用文档

文章目录

  • 1. 测试1
    • 1.1. 前台
    • 1.2. 代码
    • 1.3. 结果
  • 2. 测试2
    • 2.1. 前台
    • 2.2. 代码
    • 2.3. 结果
  • 3. 测试3
    • 3.1. 前台
    • 3.2. 代码
    • 3.3. 效果
  • 4. 举一反三
    • 4.1. 前台
    • 4.2. 代码
    • 4.3. 运行结果

1. 测试1

1.1. 前台

新建3D Object中的CubeSphereCapsule,其中它们的位置及父子关系如图所示
Unity Gameobject类测试_第1张图片

1.2. 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update

    public GameObject cube, capsule;

    void Start()
    {
        //Test1
        cube = GameObject.Find("Cube");
        /*
         * Finds a game object by name and returns it.
         * If no game object with name can be found, null is returned. If name contains a '/' character it will traverse the hierarchy like a path name. This function only returns active gameobjects. 
         */
        Debug.Log(cube);
        capsule = GameObject.Find("Capsule");
        Debug.Log(capsule);
        capsule = GameObject.Find("Sphere/Capsule");
        Debug.Log(capsule);
        capsule = GameObject.Find("Sphere/Capsule");
        Debug.Log(capsule);
        capsule = GameObject.Find("Cube/Sphere/Capsule");
        Debug.Log(capsule);
        //Debug.Log("Start");
    }

    // Update is called once per frame
    void Update()
    {
        capsule.transform.Rotate(0, 100 * Time.deltaTime, 0);
        //Debug.Log("Update");
    }
}

1.3. 结果

Unity Gameobject类测试_第2张图片

2. 测试2

2.1. 前台

手动拖拽Cube到T2脚本所对应的Cube里
Unity Gameobject类测试_第3张图片

2.2. 代码

T2

using UnityEngine;

public class T2 : MonoBehaviour
{
    public GameObject cube;
    public MeshRenderer meshRenderer;
    public BoxCollider boxCollider;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(cube);
        //获取组件
        //meshRenderer = cube.gameObject.GetComponent();
        //boxCollider = cube.gameObject.GetComponent();

        //添加一个组件
        cube.gameObject.AddComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

2.3. 结果

由于绑定了刚体,所以会看到这三个物体进行了下落(受重力作用)Unity Gameobject类测试_第4张图片

3. 测试3

3.1. 前台

Unity Gameobject类测试_第5张图片

3.2. 代码

using UnityEngine;

public class T2 : MonoBehaviour
{
    public GameObject cube, cube1;
    public MeshRenderer meshRenderer;
    public BoxCollider boxCollider;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(cube);
        //获取组件
        //meshRenderer = cube.gameObject.GetComponent();
        //boxCollider = cube.gameObject.GetComponent();

        //添加一个组件
        //cube.gameObject.AddComponent();

        //初始化cube1
        cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube1.transform.position = new Vector3(10, 0, 1);
        cube1.GetComponent<Renderer>().material.color = Color.yellow;

    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.W))
        {
                cube.SetActive(false);
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
                cube.SetActive(true);
        }
    }
}

3.3. 效果

会看到有一个黄色的Cube
Unity Gameobject类测试_第6张图片
按W键白色的全消失,再按S键又重新恢复
Unity Gameobject类测试_第7张图片

4. 举一反三

主要演示以下两种方法的用法:

Static Methods Description
FindGameObjectsWithTag Returns an array of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTag Returns one active GameObject tagged tag. Returns null if no GameObject was found.

在Unity中一个物体只能有一个Tag,但是一个Tag可以被多个物体使用。

4.1. 前台

给Cube添加一个自定义的标签CubeTag
Unity Gameobject类测试_第8张图片

4.2. 代码

using UnityEngine;

public class T2 : MonoBehaviour
{
    public GameObject cube, cube1, go;
    GameObject[] gos = new GameObject[10];
    public MeshRenderer meshRenderer;
    public BoxCollider boxCollider;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(cube);
        //获取组件
        //meshRenderer = cube.gameObject.GetComponent();
        //boxCollider = cube.gameObject.GetComponent();

        //添加一个组件
        //cube.gameObject.AddComponent();

        //初始化cube1
        cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube1.transform.position = new Vector3(10, 0, 1);
        cube1.GetComponent<Renderer>().material.color = Color.yellow;

        //举一反三
        gos = GameObject.FindGameObjectsWithTag("CubeTag");
        Debug.Log("gos[0]:");
        Debug.Log(gos[0]);

       go = GameObject.FindWithTag("CubeTag");
        Debug.Log("go:");
        Debug.Log(gos[0]);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.W))
        {
                cube.SetActive(false);
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
                cube.SetActive(true);
        }
    }
}

4.3. 运行结果

Unity Gameobject类测试_第9张图片

你可能感兴趣的:(Unity)