Search Unity

Question Scene Component

Discussion in 'Scripting Dev Blitz Day 2023 - Q&A' started by Thaina, Feb 23, 2023.

  1. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,168
    I still don't understand why we can't allow to attach component to scene. Are there any reason that scene must not contain component? Would it cause breaking change in some way?
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    I've asked the team that works on this feature for some details. I'll let you know what I find.
     
    Thaina likes this.
  3. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    219
    What is your use case?
    Note that a Scene is a container for GameObjects and a GameObject is a container for Components.
     
  4. DiacPaulAlexandru

    DiacPaulAlexandru

    Joined:
    Oct 20, 2020
    Posts:
    14
    Hey, are you trying to attach data to SceneAssets? if so you can use the "userData" field of the asset importer then create a custom Editor for "SceneAssetType", also if you need that data to be findable in scene you can create a class that implements "IProcessSceneWithReport" and move that data on scene.
    Here is a small example on how to do it
    Code (CSharp):
    1.      
    2.         public static bool TryGetData(string path, out SceneMetaData metaData)
    3.         {
    4.             var importer = AssetImporter.GetAtPath(path);
    5.  
    6.             var userData = importer.userData;
    7.  
    8.             if (string.IsNullOrEmpty(userData))
    9.             {
    10.                 metaData = default;
    11.                 return false;
    12.             }
    13.  
    14.             metaData = new SceneMetaData();
    15.             EditorJsonUtility.FromJsonOverwrite(userData, metaData);
    16.             return true;
    17.         }
    18.  
    19.         public static void SetData(string targetPath, SceneMetaData data)
    20.         {
    21.             var importer = AssetImporter.GetAtPath(targetPath);
    22.             var importerUserData = EditorJsonUtility.ToJson(data);
    23.             if (importerUserData != importer.userData)
    24.             {
    25.                 importer.userData = importerUserData;
    26.                 importer.SaveAndReimport();
    27.             }
    28.         }
     

    Attached Files:

    teutonicus likes this.
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Every Component has immediate access to .transform or .gameObject or .tag; those make no sense for a Scene. I can imagine all the horrible
    if (transform == null)
    types of checks to make sure a component remained safe to use if somehow attached to a Scene.
     
  6. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,168
    Because sometimes we don't want gameObject logic. We want to write scene logic

    Sometimes we want something that will stay with the scene, manipulate other root GameObjects. And many core logic of the scene should not depend on existence or activation of object inside the scene

    UX wise. When too many gameobject was spawned into the root of the scene it then become inconvenient to find core logic object. Also many times we don't know which gameObject should be attach the logic because it really not a gameobject specific logic
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Being able to add custom data to scenes that would be available at runtime without jumping through hoops would be nice.

    Unity does this internally with some data (OcclusionCullingSettings, RenderSettings, LightmapSettings and NavMeshSettings are all in the scene yaml file), so for things like those, it should be clear that this is useful.

    For our game, we usually end up with some global ScriptableObject that has a scene-to-data Dictionary that we look up for things like 2D light settings and other per-scene data. If we could store it as a part of the scene file, and do SceneManager.GetActiveScene().GetData<DataType>() to load it at runtime, it'd be a lot more convenient.
     
    Thaina likes this.
  8. Kodo

    Kodo

    Joined:
    Sep 6, 2014
    Posts:
    18
    I has two moments

    1) When you need some data associated with scene. You could might use it before or after scene load.

    2) You need some code associated with scene. Usualy before\after scene load\unload.