Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Open A Prefab In Editor By Its Address

Discussion in 'Addressables' started by Rafarel, Apr 12, 2019.

  1. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    Hello, I want to make a script that opens an addressable prefab asset into the prefab editor by its address.

    Something using PrefabUtility.LoadPrefabContents I guess but with an address. Thanks !
     
  2. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    we don't have a lookup map for address to asset in the editor (only guid to asset entry, which includes address). That being said, you should be able to grab the AddressableAssetSettings object, loop the groups, then loop the entries in the group to find the one you want.

    Keep in mind address is not enforced to be unique. A user can name multiple things with the same address.
     
  3. Spelborea

    Spelborea

    Joined:
    May 20, 2020
    Posts:
    11
    Any tutorials? I need same thing
     
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    It should be something like this, I didn't test the OpenPrefab() method

    Code (CSharp):
    1.     void OpenPrefab()
    2.     {
    3.         if(TryGetAssetGuid("YourAddress", out var guid))
    4.         {
    5.             var path = AssetDatabase.GUIDToAssetPath(guid);
    6.             var asset = PrefabUtility.LoadPrefabContents(path);
    7.         }
    8.     }
    9.  
    10.         public static bool TryGetAssetGuid(string address, out string guid)
    11.         {
    12.             var path = "Assets/AddressableAssetsData/AssetGroups/Default Local Group.asset";
    13.             var assetGroup = AssetDatabase.LoadAssetAtPath<AddressableAssetGroup>(path);
    14.             var entries = assetGroup.entries;
    15.          
    16.             foreach(var entry in entries)
    17.             {
    18.                 if(string.Equals(address, entry.address, StringComparison.Ordinal))
    19.                 {
    20.                     guid = entry.guid;
    21.                     return true;
    22.                 }
    23.             }
    24.  
    25.             guid = null;
    26.             return false;
    27.         }
    28.  
    29.