Search Unity

something like AssetReferenceT<SceneAsset>

Discussion in 'Addressables' started by wang37921, Jan 19, 2020.

  1. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    SceneAsset is in UnityEditor namespace.
    how to reference a scene asset.
     
  2. pahe4retro

    pahe4retro

    Joined:
    Aug 13, 2019
    Posts:
    33
    Scene is in the UnityEngine.SceneManagement namespace.
     
  3. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    upload_2020-1-21_14-23-6.png
    upload_2020-1-21_14-33-54.png
    how to reference a scene asset. using AssetReferenceT
     
  4. carlbinalla

    carlbinalla

    Joined:
    Mar 5, 2018
    Posts:
    7
    You can do something like this:

    Code (CSharp):
    1. public AssetReference someScene;
    then just drag the addressable scene to the editor. It will accept the scene, though not sure if it is the same as using its key, as I haven't use
    AssetReference
    yet
     
  5. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    AssetReference certainly could be use. it works, but i want to restrain the asset reference scene file only.
    like AssetReferenceT<Sprite>
    upload_2020-1-21_14-53-11.png
     
  6. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Do just that.
    public class AssetReferenceScene : AssetReferenceT<Scene> { }
    then use AssetReferenceScene for your field.
     
  7. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    89
    That doesn't work, AssetReferenceT requires a type of Object, Scene is a struct.
     
    carlbinalla likes this.
  8. WeltenbauerRenn

    WeltenbauerRenn

    Joined:
    Jun 20, 2017
    Posts:
    40
    My workaround looks like this:
    Code (CSharp):
    1.   [System.Serializable]
    2.   public class SceneAssetReference : AssetReference {
    3.  
    4.         public SceneAssetReference(string guid) : base(guid) {}
    5.  
    6.         //-----------------------------------------------------------------------------
    7.  
    8.         public override bool ValidateAsset(string path) {
    9.             return path.EndsWith(".unity");
    10.         }
    11.     }
     
    Jackal256, _geo__, Thygrrr and 2 others like this.
  9. JamesFrowenDev

    JamesFrowenDev

    Joined:
    Oct 10, 2015
    Posts:
    20
    Here is a class with the same stuff as AssetReferenceT

    Code (CSharp):
    1. [System.Serializable]
    2. public class AssetReferenceScene : AssetReference
    3. {
    4.     /// <summary>
    5.     /// Construct a new AssetReference object.
    6.     /// </summary>
    7.     /// <param name="guid">The guid of the asset.</param>
    8.     public AssetReferenceScene(string guid) : base(guid)
    9.     {
    10.     }
    11.  
    12.  
    13.     /// <inheritdoc/>
    14.     public override bool ValidateAsset(Object obj)
    15.     {
    16. #if UNITY_EDITOR
    17.         var type = obj.GetType();
    18.         return typeof(UnityEditor.SceneAsset).IsAssignableFrom(type);
    19. #else
    20.         return false;
    21. #endif
    22.  
    23.     }
    24.  
    25.     /// <inheritdoc/>
    26.     public override bool ValidateAsset(string path)
    27.     {
    28. #if UNITY_EDITOR
    29.         var type = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(path);
    30.         return typeof(UnityEditor.SceneAsset).IsAssignableFrom(type);
    31. #else
    32.         return false;
    33. #endif
    34.     }
    35.  
    36. #if UNITY_EDITOR
    37.     /// <summary>
    38.     /// Type-specific override of parent editorAsset.  Used by the editor to represent the asset referenced.
    39.     /// </summary>
    40.     public new UnityEditor.SceneAsset editorAsset => (UnityEditor.SceneAsset)base.editorAsset;
    41. #endif
    42. }
     
    samanabo, LazloBonin, warpfx and 7 others like this.
  10. Yuki_Rain

    Yuki_Rain

    Joined:
    Jul 25, 2017
    Posts:
    13
  11. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    89
    But SceneAsset lives in the UnityEditor namespace and cannot be used in builds. How does that work during runtime?
     
    davidrochin and Prodigga like this.
  12. kdserra

    kdserra

    Joined:
    May 16, 2018
    Posts:
    10
    Here's what I do:

    My solution is to reference an Addressable, then I validate that the asset referenced is SceneAsset, if it's not, the field gets reset.

    The SceneAsset is only checked in the Unity Editor which means you will have no issues building.

    The code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AddressableAssets;
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6.  
    7. namespace YourProject
    8. {
    9.     public class Level : ScriptableObject
    10.     {
    11.         [SerializeField]
    12.         private AssetReference _scene;
    13.  
    14.         #if UNITY_EDITOR
    15.         private void OnValidate()
    16.         {
    17.             if (_scene.editorAsset is not SceneAsset)
    18.             {
    19.                 _scene = null;
    20.             }
    21.         }
    22.         #endif
    23.     }
    24. }
    25.  
     
    ShadySheikah likes this.