如何让对象标签有它的位置? [英] How to get Object tag having its position?

查看:28
本文介绍了如何让对象标签有它的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Unity 的新手,一个问题是我浪费了太多时间.

I'm new to Unity and one problem is stealing too much time from me that it should.

无论如何,我正在尝试从场景中获取对象标签,同时具有 X 和 Z 位置.指定,我想通过获取围绕我的机器人的游戏对象标签来获取神经网络的输入.有没有像 GetComponent<>() 这样的函数,或者可以接受 x,z 并返回标签的函数?

Anyway, I'm trying to get an object tag from a scene while having X and Z position of it. Specifying, I want to get inputs for neural network by getting game object tags surrounding my bot. Is there a function like GetComponent<>() or something that will take x,z and return tag?

还有一个问题,我手动将 emptyObject 放在每个空白空间中,这样我就可以得到一些回报.如果我删除这些空块"并尝试检查特定 x、z 位置的某些内容,会不会有问题?

And another question, I manually put emptyObject in every void space so I can get something in return. Would there be a problem if I delete these 'void blocks' and try to check something in that specific x, z position?

推荐答案

您可以使用 Transform 搜索场景中的每个对象,然后查找具有 x 和 z 位置的对象并返回标签.Resources.FindObjectsOfTypeAllGameObject.FindObjectsOfType 函数可以做到这一点,但为了性能,使用 Scene.GetRootGameObjects 来获取所有根对象并使用 GetComponentsInChildren 遍历每个根对象的子对象,并检查 x 和 z 位置是否匹配.

You can search for every Object in the scene with the Transform then look search for the ones with the x and z positions and return the tag. The Resources.FindObjectsOfTypeAll and GameObject.FindObjectsOfType functions can do this but for the sake of performance, use Scene.GetRootGameObjects to get all the root Objects and the loop thorugh the children from each Root Object with GetComponentsInChildren<Transform> and check if the x and z position match.

使用它们是因为它们不返回数组而只是填充一个列表.

Use them because they don't return array but just fills a List.

查找场景中的所有对象:

private List<GameObject> rootGameObjects = new List<GameObject>();
private List<Transform> childObjs = new List<Transform>();

private void GetAllRootObject()
{
    Scene activeScene = SceneManager.GetActiveScene();
    activeScene.GetRootGameObjects(rootGameObjects);
}

private void GetAllChildObjs()
{
    for (int i = 0; i < rootGameObjects.Count; ++i)
    {
        GameObject obj = rootGameObjects[i];

        //Get all child components attached to this GameObject
        obj.GetComponentsInChildren<Transform>(true, childObjs);
    }
}

使用 x,z 位置查找对象标签:

bool xzEquals(Vector3 pos1, Vector3 pos2)
{
    return (Mathf.Approximately(pos1.x, pos2.x)
        && Mathf.Approximately(pos1.z, pos2.z));
}

string GetTagFromPos(float x, float z)
{
    Vector3 pos = new Vector3(x, 0, z);

    rootGameObjects.Clear();
    childObjs.Clear();

    GetAllRootObject();
    GetAllChildObjs();

    //Loop through all Objects
    for (int i = 0; i < childObjs.Count; i++)
        //check  if x and z matches then return tag
        if (xzEquals(childObjs[i].position, pos))
            return childObjs[i].tag;

    return null;
}

GameObject GetObjectFromPos(float x, float z)
{
    Vector3 pos = new Vector3(x, 0, z);

    rootGameObjects.Clear();
    childObjs.Clear();

    GetAllRootObject();
    GetAllChildObjs();

    //Loop through all Objects
    for (int i = 0; i < childObjs.Count; i++)
        //check  if x and z matches then return the Object
        if (xzEquals(childObjs[i].position, pos))
            return childObjs[i].gameObject;

    return null;
}

用法:

//Get tag from x, z pos
Debug.Log(GetTagFromPos(1, 2));
//Get object from x, z pos
Debug.Log(GetObjectFromPos(1, 2));

如果您想在循环中节省一些时间,您也可以手动将要搜索的对象放入列表.

You can also manually put the Objects you want to search to the List if you want to save some time in the loop.

这篇关于如何让对象标签有它的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆