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

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

JavaScript static function FindGameObjectsWithTag(tag: string): GameObject[];
C# static GameObject[] FindGameObjectsWithTag(string tag);

Parameters 参数

tag

The name of the tag to search GameObjects for. 

用于搜索对象标签的名称

Description 描述

Returns a list of active GameObjects tagged /tag/. Returns empty array if no GameObject was found.

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

Tags must be declared in the tag manager before using them. A UnityException will be thrown if the tag does not exist or an empty string or null is passed as the tag.

标签必须在使用之前到标签管理器里面声明。如果标签不存在或为空字符串或传递null作为标签,将抛出Unity异常。

JavaScript:

// Instantiates respawnPrefab at the location 

// of all game objects tagged "Respawn".

 

var respawnPrefab : GameObject; 

 

var respawns; function Start() 

{ 

if (respawns == null) 

respawns = GameObject.FindGameObjectsWithTag ("Respawn"); 

for (var respawn in respawns) 

Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation); 

} 

C#:

using UnityEngine;

using System.Collections;

 

public class ExampleClass : MonoBehaviour {

    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;

        }

    }

}

Another example:

另一个例子:

JavaScript:

// Print the name of the closest enemy

print(FindClosestEnemy().name); 

 

// Find the name of the closest enemy

function FindClosestEnemy () : GameObject {

// Find all game objects with tag Enemy

var gos : GameObject[];

gos = GameObject.FindGameObjectsWithTag("Enemy"); 

var closest : GameObject; 

var distance = Mathf.Infinity; 

var position = transform.position; 

// Iterate through them and find the closest one

for (var go : GameObject in gos)  { 

var diff = (go.transform.position - position);

var curDistance = diff.sqrMagnitude; 

if (curDistance < distance) { 

closest = go; 

distance = curDistance; 

} 

} 

return closest;

}

C#:

using UnityEngine;

using System.Collections;

 

public class ExampleClass : MonoBehaviour {

    GameObject FindClosestEnemy() {

        GameObject[] gos;

        gos = GameObject.FindGameObjectsWithTag("Enemy");

        GameObject closest;

        float distance = Mathf.Infinity;

        Vector3 position = transform.position;

        foreach (GameObject go in gos) {

            Vector3 diff = go.transform.position - position;

            float curDistance = diff.sqrMagnitude;

            if (curDistance < distance) {

                closest = go;

                distance = curDistance;

            }

        }

        return closest;

    }

    void Example() {

        print(FindClosestEnemy().name);

    }

}

Another example, testing for empty array:

又一个例子,测试空数组:

JavaScript:

// Search for game objects with a tag that is not used

 

function Start () 

{ 

var gos : GameObject[]; 

gos = GameObject.FindGameObjectsWithTag("fred"); 

if (gos.length == 0) 

{ 

Debug.Log("No game objects are tagged with fred"); 

} 

} 

C#:

using UnityEngine;

using System.Collections;

 

public class ExampleClass : MonoBehaviour {

 

void Start () {

 

GameObject[] gos; 

gos = GameObject.FindGameObjectsWithTag("fred"); 

if (gos.Length == 0) 

{ 

Debug.Log("No game objects are tagged with fred"); 

} 

 

}

}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值