Search Unity

[Solved] How to change Addressable Asset name via Script?

Discussion in 'Addressables' started by trunghieu974, Nov 19, 2019.

  1. trunghieu974

    trunghieu974

    Joined:
    Jun 19, 2015
    Posts:
    25
    Hi all,
    I want to make a custom editor window to rename some asset base on result return from server.
    Is there anyway to mark an asset as addressable and rename it by using c# script?
    I have been looking for document from unity but didn't find any solution yet.
    Thank you very much!
     
  2. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    I think what you're looking for is a combination of
    Code (CSharp):
    1. AddressableAssetSettingsDefaultObject.Settings.CreateOrMoveEntry(...)
    and the address property of an AssetEntry. You could try
    Code (CSharp):
    1. AddressableAssetSettingsDefaultObject.Settings.FindAssetEntry("AssetGUID").address
    If you don't know the guid of your asset but you know the path you can utilize
    Code (CSharp):
    1. AssetDatabase.AssetPathToGUID(...)
    Hope that helps!
     
  3. trunghieu974

    trunghieu974

    Joined:
    Jun 19, 2015
    Posts:
    25
    Thank you very much! I've solved my problem.
    Here is the code for anyone want to do this:
    Code (CSharp):
    1.   string path = AssetDatabase.GetAssetPath(source);
    2.         string guiID = AssetDatabase.AssetPathToGUID(path);
    3.         AddressableAssetSettingsDefaultObject.Settings.CreateOrMoveEntry
    4.             (guiID, AddressableAssetSettingsDefaultObject.Settings.DefaultGroup, false, true);
    5.         AddressableAssetSettingsDefaultObject.Settings.FindAssetEntry(guiID).address = "MyAddress";