Unity Editor中打开场景获取GameObject

编辑器工具开发中,有时需要找到场景中的物体。下面介绍用代码在Editor中寻找指定场景中的GameObject

在这里插入图片描述
如上图,在三个场景中分别有一些GameObject,现在随机打卡一个场景,然后再当前场景中找其他场景中的GameObject
代码如下:

using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;

public class ForeachObjectsInEditor {
	[MenuItem("Tools/ForeachObjectsInEditor")]
	public static void ForeachObjects(){
		string[] sceneArr = {
			"ForeachObjsScene1",
			"ForeachObjsScene2",
			"ForeachObjsScene3",
		};

		string sceneRootPath = "Assets\\ForeachObjectsInEditor";
		for (int i = 0; i < sceneArr.Length; i++)
		{
			string sceneName = sceneArr[i];
			string fullPath = Path.Combine(sceneRootPath, sceneName) + ".unity";
			Scene scene = EditorSceneManager.OpenScene(fullPath, OpenSceneMode.Additive);
			GameObject[] objects = scene.GetRootGameObjects();
			Debug.Log("<color=red>======================================</color>");
			for (int j = 0; j < objects.Length; j++)
			{
				Debug.LogFormat("<color=yellow>Scene : {0}, GameObject : {1}</color>", sceneName, objects[j].name);
			}
			EditorSceneManager.CloseScene(scene, true);
		}
	}
}

运行效果如下:
在这里插入图片描述


其中需要注意的是:

1. OpenSceneMode
Scene scene = EditorSceneManager.OpenScene(fullPath, OpenSceneMode.Additive);

OpenSceneModed有三种:

public enum OpenSceneMode
    {
        //
        // 摘要:
        //     Closes all current open scenes and loads a scene.
        Single = 0,
        //
        // 摘要:
        //     Adds a scene to the current open scenes and loads it.
        Additive = 1,
        //
        // 摘要:
        //     Adds a scene to the current open scenes without loading it. It will show up as
        //     'unloaded' in the Hierarchy Window.
        AdditiveWithoutLoading = 2
    }
2. Close时候的第二个参数
EditorSceneManager.CloseScene(scene, true);
// true:关闭后删除
// false:关闭后不删除

设置为false后的效果如下:
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity,可以使用PrefabUtility类来获取未实例化的GameObject预制体上的组件。 下面是一个简单的示例,演示如何获取未实例化的GameObject预制体上的组件: 1. 首先,打开Unity编辑器并创建一个新场景。 2. 在项目视图创建一个新的预制件。 3. 选择预制件并在Inspector视图添加一个新组件。例如,您可以添加一个名为“TestComponent”的C#脚本。 4. 在TestComponent脚本添加以下代码: ``` using UnityEngine; public class TestComponent : MonoBehaviour { public int testValue = 0; } ``` 5. 然后,将预制件拖动到场景。 6. 选择预制件并在Inspector视图更改TestComponent的testValue属性。例如,您可以将其设置为5。 7. 打开脚本编辑器并创建一个新的C#脚本。将以下代码添加到脚本: ``` using UnityEngine; using UnityEditor; public class GetPrefabComponent : MonoBehaviour { public GameObject prefab; void Start() { TestComponent testComponent = PrefabUtility.GetPrefabAssetType(prefab) == PrefabAssetType.Regular ? PrefabUtility.GetPrefabInstanceHandle(prefab).GetComponent<TestComponent>() : null; if (testComponent != null) { Debug.Log("TestComponent testValue: " + testComponent.testValue); } else { Debug.Log("TestComponent not found on prefab"); } } } ``` 8. 在场景创建一个新的空对象,并将GetPrefabComponent脚本添加到该对象上。 9. 在GetPrefabComponent脚本组件,将prefab属性设置为您创建的预制件。 10. 您现在可以运行场景,并查看控制台的输出。如果一切正常,您应该会看到以下消息: ``` TestComponent testValue: 5 ``` 这表明您成功获取了未实例化的GameObject预制体上的TestComponent组件,并检索了其testValue属性。 希望这个示例能够帮助您了解如何在Unity获取未实例化的GameObject预制体上的组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值