Search Unity

Are there any tools for inspecting scenes programmatically?

Discussion in 'Editor & General Support' started by JohnOknelorfDev, Jan 12, 2019.

  1. JohnOknelorfDev

    JohnOknelorfDev

    Joined:
    Oct 2, 2017
    Posts:
    21
    Hello, Dear Forum and Creators,

    Shortly speaking, the question is the same as in the title: are there any tools for inspecting scenes programmatically?

    Background:
    In our project, we create very many different scenes in the Editor. For some special scenes, we need to make sure that we have exactly one instance of some Component in the whole scene. (Something similar to [DisallowMultipleComponent] but regarding the entire scene). We want to check it each time before the build is started (it's easy to do with IPreprocessBuild or IPreprocessBuildWithReport as we know). BUT how can we inspect the scene programmatically? Of course, we could simply investigate the source *.asset scene files by loading the file as text and searching for the required thing... But it's quite a cumbersome approach. What I mean is: is there a built-in toolset of nice and comfortable tools for such tasks?

    Will be unbelievable grateful for any help.

    Regards,
    John.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Unfortunately (at least that was the case last time i checked), there's no API for dealing with a scene like with other asset types (prefabs, textures, etc). The only option is to load it and then iterate the scene objects.

    BUT....

    Your specific case can be solved by a few different approaches, for example:
    1. Tests - It's very simple to create a test that will load a scene, then verify the conditions you're after (e.g: only a single component). You mentioned that you have a lot of these, so it's also simple to use the [TestCaseSource] attribute that will basically use 1 test but feed it a different scene path every time.
    2. AssetModificationProcessor - this is an editor class that gets a notification when saving assets. Your case is a class example for implementing something like that (e.g: on a scene save, verify any conditions you want, if they're not met, show an error + do not save).
    Of course, you can also add some code that checks this during the build (since you get the PostProcessScene callback during the build process).

    You can choose which to use or mix a few of these for validating your project is "in tact" at different stages.

    Let me know if you need further help with this.
     
  3. JohnOknelorfDev

    JohnOknelorfDev

    Joined:
    Oct 2, 2017
    Posts:
    21
    Hello @liortal ,

    Excuse me for the delayed answer. Important tasks swallowed me. I get some fresh breath and now able to return to this task. Thank you very much for the detailed answer. You gave me the basis for thinking!

    I've tried all the options, and it seems that the option based on Tests is a perfect choice since we can run tests not only from the Unity Editor but automatically using the command-line interface.

    Thank you one more time. :)

    Regards,
    John.