简体   繁体   English

防止保存对 GameObject 的更改

[英]Prevent Changes on GameObject from being saved

I'm currently working on an Editor Script in Unity that disables specific GameObjects in a multi scene setup to make the Editor more responsive.我目前正在开发 Unity 中的编辑器脚本,该脚本在多场景设置中禁用特定的游戏对象,以使编辑器更具响应性。 I'm working with a setup that has up to 4 Scenes loaded at the same time.我正在使用同时加载多达 4 个场景的设置。 Since all these Scenes have many GameObjects in them (I'm talking thousands or even tens of thousands of Objects) the Unity Editor is getting pretty unresponsive and sloppy to use when these Scenes are loaded.由于所有这些场景中都有许多游戏对象(我说的是数千甚至数万个对象),因此在加载这些场景时,Unity 编辑器变得非常迟钝且使用马虎。 Playing and Builds are fine btw, its only the Editor that is unusable.顺便说一句,播放和构建都很好,只有编辑器无法使用。 So I'm working on a simple Editor Tool, that disables specific Parent Objects in all loaded Scenes so the Editor is usable again.所以我正在开发一个简单的编辑器工具,它会在所有加载的场景中禁用特定的父对象,以便编辑器再次可用。 This Tool works fine, but I want to prevent the changes on these GameObjects from being saved.此工具工作正常,但我想防止保存对这些游戏对象的更改。 I intentionally didn't mark the GameObjects as Dirty so Unity doesn't realize when I disable the Objects.我故意没有将 GameObjects 标记为 Dirty,因此当我禁用 Objects 时 Unity 没有意识到。 Problem is, when I actually work on a Scene and save my work, the disabled GameObjects will be saved aswell, because at this point the Scene will be marked as Dirty.问题是,当我实际处理场景并保存我的工作时,禁用的游戏对象也会被保存,因为此时场景将被标记为脏。 I've already looked into HideFlags but the problem there is, that I can't make Unity only ignore the changes that were made to an Object.我已经查看了 HideFlags,但存在的问题是,我不能让 Unity 只忽略对 Object 所做的更改。 HideFlags will only prevent the Object itself from being saved, which is not what I want. HideFlags 只会阻止 Object 本身被保存,这不是我想要的。

If somebody knows a simple way to make Unity ignore changes on specific GameObjects that would be extremly helpful:)如果有人知道一种让 Unity 忽略特定游戏对象上的更改的简单方法,这将非常有帮助:)

Alternativly I would be interested in a way that enables me to run custom Code while Unity is saving the Scenes.或者,我会对一种能够在 Unity 保存场景时运行自定义代码的方式感兴趣。 That way I could make sure, that these GameObjects will be enabled, before Unity saves the Scene.这样我就可以确保在 Unity 保存场景之前启用这些游戏对象。

Thanks in advance提前致谢

Don't know about preventing dirty objects from saving, doubt it is possible, but to run custom code before Unity saves a scene - easy.不知道如何防止脏对象保存,怀疑是否有可能,但在 Unity 保存场景之前运行自定义代码 - 很容易。

Example editor script (remember it should be placed under Editor/ dir to work as any other editor script)示例编辑器脚本(请记住,它应该放在Editor/目录下,以便像任何其他编辑器脚本一样工作)

using UnityEditor;

[InitializeOnLoad]
static class EditorSceneManagerSceneSaved {
    static EditorSceneManagerSceneSaved () {
        UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving;
    }

    static void OnSceneSaving (UnityEngine.SceneManagement.Scene scene, string path) {
        UnityEngine.Debug.LogFormat ("Saving scene '{0}' to {1}", scene.name, path);
        /* Do your magic here */
    }
}

Credit for the example goes here , more info on the API here示例的功劳在这里,更多关于 API 的信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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