Is there a way to get GUID on runtime in build?

Hi I would like to know if there is a way to get GUID on runtime without the need of AssetDatabase.AssetPathToGUID() as this only works in editor and not in build.

I require the GUID because I wish to assign an AssetReference during runtime via code instead of via the inspector which requires a GUID.

Is this even possible at all?

That doesn’t make much sense. Why would you need to create an AssetReference for an asset you already have? You can also pass in the address instead of GUID to the constructor (though someone else has noted that IsValidKey will return false, but loading will still work).

I wish to assign a reference to an object on each of my scriptable objects.

Example:
Mercedes - Asset Reference to Maercedes Prefab
BMW - Asset Reference to BMW Prefab

Ok will try the direct method via Addressables itself. Realize I’ve been doing it already, just wanted to know if I could use Asset Reference as it is more visually pleasing then storing just a string of the address and loading it directly from Addressables

Oh, if you’re talking about in edit mode, you can get an asset’s GUID through the AssetDatabase.

[EDIT] Just realized you said that’s not what you’re trying to do in your OP. But you just said it’s more visually appealing, so that makes me think you’re talking about the inspector (which is editor only). So why are you trying to make an AssetReference at runtime?

I was trying to see if I could achieve assigning an asset reference on runtime so when I look at my scriptableobject via inspector it would display the assigned prefab during runtime. If i go with simply storing the address path string on the scriptableobject it will still work (which I was currently doing to assign an asset reference anyway but thanks for helping me realize that). It just seems to be less informative then assigning an asset reference as i wouldn’t be able to easily look in the inspector to know if i had referenced to the correct address or not since it is just a string instead of a visual prefab icon with it’s name on it.

And yes I’ve already tried asset database but realize it is editor only so I wouldn’t be able to go with that method unfortunately. Guess I’ll have to make do with a string address variable

If you have a reliable method of setting it through the assetdatabase, you can just wrap that code in an #if UNITY_EDITOR preprocessor block.

1 Like

I do something like this so that I can load based on guid.

using UnityEngine;

namespace Amilious.Core {
   
    /// <summary>
    /// This is a <see cref="ScriptableObject"/> that stores it's own guid.
    /// </summary>
    public abstract class AmiliousScriptableObject : ScriptableObject, ISerializationCallbackReceiver {

        /// <summary>
        /// This serialized field is used to store the assets guid.
        /// </summary>
        [SerializeField, HideInInspector] private string guid;

        /// <summary>
        /// This serialized field is used to store the assets local file id.
        /// </summary>
        [SerializeField, HideInInspector] private long fileId;

        /// <summary>
        /// This property contains the assets guid from the asset database.
        /// </summary>
        public string AssetGuid => guid;

        /// <summary>
        /// This property contains the assets local file id from the asset database.
        /// </summary>
        public long AssetFileId => fileId;
       
        /// <inheritdoc />
        public void OnBeforeSerialize() {
            #if UNITY_EDITOR
            UnityEditor.AssetDatabase.TryGetGUIDAndLocalFileIdentifier(GetInstanceID(), out guid, out fileId);
            #endif
        }

        /// <inheritdoc />
        public void OnAfterDeserialize() { }
       
    }
}
7 Likes