简体   繁体   中英

How can I loop over all game objects in hierarchy including disabled objects all children too in both scenes?

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class CompareObjects : EditorWindow
{
    string searchString = "";
    List<GameObject> items = FindObjectsOfType(typeof(GameObject)).ToList();

    void OnGUI()
    {
        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        GUILayout.FlexibleSpace();
        searchString = GUILayout.TextField(searchString, EditorStyles.toolbarTextField);
        GUILayout.EndHorizontal();

        // Do comparison here. For example
        for (int i = 0; i < items.Length; i++)
        {
            if (items[i].name.Contains(searchString))
            {
                GUI.Label(items[i].name);
            }
        }
    }
}

The first problem is I can't convert the array of:

FindObjectsOfType(typeof(GameObject))

To List.

Seconds how can I loop over the objects in two scenes? I want to make a comparison between scene a and scene b objects and to be able also to narrow the searching for objects with specific component/s and list the results in the EditorWindow: List A the objects with components in scene A and List B the objects with components in scene B

For example if I type in the search bar: Space,BoxCollider Then it should find all objects in both scenes that contain the name Space and that also have a BoxCollider attached or without a BoxCollider attached.

I want to know in the result what objects name Space have boxcollider and what not.

Example:

List A : Space1 BoxCollider -- Scene A
List A : Space2 Empty -- Scene A

List B : Space1 Empty -- Scene B
List B : Space2 BoxCollider -- Scene B

Something like this comparison.

I can't convert the array of

Method FindObjectsOfType(Type) return array of objects, to cast those objects to list you can use LINQ method OfType :

List<GameObject> items = FindObjectsOfType(typeof(GameObject)).OfType<GameObject>.ToList();

Or just use generic override of method FindObjectsOfType<T>()

List<GameObject> items = GameObject.FindObjectsOfType<GameObject>().ToList();

Seconds how can I loop over the objects in two scenes?

You need select all scenes from SceneManager and make loop over it's GetRootGameObjects() .

If you need to loop all objects and make some manual calculations - you can loom manually like was described here . If you know what exactly components you need you can use method public Component[] GetComponentsInChildren(Type t, bool includeInactive); with scene root objects (make sure then you set includeInactive to True if you want to search in inactive objects)

I want to know in the result what objects name Space have boxcollider and what not.

using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class Searcher
{
    [MenuItem("Tools/PrintSearch")]
    public static void PrintSearch()
    {
        var objectName = "Space";
        var componentType = typeof(BoxCollider);

        var scenes = Enumerable.Range(0, SceneManager.sceneCount)
            .Select(SceneManager.GetSceneAt)
            .ToArray();
        var objectsWithNames = scenes
            .ToDictionary(s => s, s=> GetSceneObjectsWithName(s, objectName.ToLower()));

        var forLog = objectsWithNames
            .SelectMany(pair => pair.Value.Select(child =>
                $"Scene {pair.Key.name}: object name '{child.gameObject.name}' {(child.GetComponent(componentType) != null ? "contains" : "do not contains")} component {componentType.Name}"));

        Debug.Log(string.Join("\n", forLog));
    }

    private static Transform[] GetSceneObjectsWithName(Scene scene, string objectName)
    {
        return scene.GetRootGameObjects()
            .SelectMany(g => GetChildHierarchyRecursively(g.transform))
            .Where(t=>t.gameObject.name.ToLower().Contains(objectName))
            .ToArray();
    }

    private static IEnumerable<Transform> GetChildHierarchyRecursively(Transform parentTransform)
    {
        yield return parentTransform;
        if (parentTransform.childCount > 0)
            yield return parentTransform;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM