Question by
yZRXConfusion · Jul 01, 2018 at 06:07 PM ·
scripting problemserializationresourcesresources.load
How to get Resource path of asset/prefab in code?
I was trying to use AssetDatabase.LoadAssetAtPath and AssetDatabase.GetAssetPath to handle serialization of certain resources in my game (such as which sprite certain item will use, it serializes only a string of the resource path of the sprite and than gets it back when deserializing), but then I found out you can't use UnityEditor in build, so I've been using Resources.Load as a substitiute for LoadAssetAtPath, however I couldn't find a substitute for GetAssetPath after some searching. Sorry if an obvious question, but help would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization;
using UnityEditor;
[System.Serializable][DataContract(Name = "Resource")]
public class Resource<T> where T : UnityEngine.Object {
[DataMember(Name = "ResourcePath")]
private string resourcePath;
public T resource {
get {
//return AssetDatabase.LoadAssetAtPath(resourcePath, typeof(T)) as T;
return Resources.Load(resourcePath, typeof(T)) as T;
}
set {
//resourcePath = AssetDatabase.GetAssetPath(value);
}
}
public Resource (T resource) {
this.resource = resource;
}
}
Code in question if it's of any use.
Comment