If you want to save an object, you can:
- Store the name
- Store the transform
- Read all the Components … and store their class
- …and their data
- Read all the child-gameobjects … and repeat
All those things can be reversed to Load the object later in the game - you can instantiate classes, you can create GameObjects, you can parent things, you can attach components to GameObjects, you can restore data.
BUT: when you hit any reference to a project-wide Asset (e.g. a ScriptableObject, used as recommended by Unity’s own documentation to store config data, game-data, etc) … BOOM! Fail.
As far as I can tell:
- Resources.Load is hardcoded to require a filename at runtime
- Unity is hardcoded to prevent you reading filenames of assets at runtime
In the Editor, there are a bunch of ways to “find the original Asset in the project, starting from a reference to it in-scene”, but they all use Editor-only API calls (e.g. AssetDatabase, e.g. PrefabUtility, etc).
I can’t see a way out of this. What am I missing?
EDIt: see below, but a partial solution is to read (ScriptableObject).name … although it’s undocumented, in current Unity versions this is actually the filename, even at runtime.
NB: it’s only a partial solution: if you ever store any assets in a subfolder of Resources, you will still have no way of loading them at runtime, because there’s no way to find out which folder they came from.
its typically better (in most scenarios) to simply have them referenced via the inspector. its much less code to worry about and Unity can do some fancy build optimizations to not include assets that are never used, allowing the game to use less memory.
if you want an asset to always be available in the build even when its not detect as ever being referenced during building then you’ll want it in a Resources folder and then use Resources.Load() to grab it by filename.
Project assets are a little different from scene assets, but saving/loading them shouldn’t be that much more difficult
Resources.Load requires a path to the prefab. Have you considered putting any prefabs you wish to load into the same subfolder under the resources folder? Then you’ll just need the name of the prefab, which it looks like you’re saving in step #1.
Personally in instances like this I create a list of prefabs and just drag them there in the inspector. I never use Resources.Load. Is your list of objects so large for this to be impractical?
1 Like
They are already in the Inspector. What you describe has literally zero effect. The problem is that the inspector has (and stores) data that Unity apparently doesn’t let games access.
My whole problem here is that they ARE different from scene assets - and that Unity appears to have no working API for accessing them after the build is made! 
As far as I can tell, it is literally impossible to read “The name of the prefab” (or, in my case, the ScriptableObject-asset - but I believe they use exactly the same mechanism internally, so I thikn solving one would solve the other).
To be clear: you can read the name of the GameObject itself, but not the name of the prefab that generated it. You can read the class, the type, etc of a ScriptableObject - but you cannot read the name of the SO, and Unity’s docs require you to provide the name of the SO to load it later :(.
That’s great if you’re not doing any content at runtime. My players are creating their own monsters / vehicles / levels / dungeons / etc. This is on their iPad, or their laptop, or in the WebGL player - there is no such thing as a Unity Editor at runtime 
Aha! Your comment made me think about this differently, and I found a solution that is simple, stupid, and … works, for simple cases.
Complex cases are still impossible to sovle until Unity provides their missing /secret/private API calls (you can’t find the subfolder a ScriptableObject is in, which is critical becuase Resouces.Load REQUIRES yuo to provide it).
But you can follow the reference, and call .name on it, and what you get back isn’t a normal Unity GameObject name, it’s actually a filename.
The inspector allows you to assign the references in the editor and they will be available in the build. if a monobehaviour has a field
public ScriptableObject mySO;
and in the inspector you drag a ScriptableObject asset into that field. then that asset will simply be available when the Build runs… through that monobehaviour. its that simple. Unity tracks those references by GUID and makes sure any GUIDs that are referenced are put in the build. for this reason ScriptableObject and Monobehaviour classes (at least the ones not labeled abstract) must be written in their own files of the same name as the class (because the GUID for that class is stored in the meta file)
You can also have one ScriptableObject Reference other scriptableObjects, as a sort of Dictionary. and then use Resource.Load to grab that dictionary reference.
They do. Its called Resources. perhaps if you shown your code we can see what the issue is. but I’m able to load Assets via Resources just fine.
now you can’t create new unity assets and save them at runtime, but thats not what you said. besides theres ways around that.
Sure, but that has nothing to do with this thread.
The core problem of this thread is: how do you discover the filename that you’ll later pass to Resources.Load?
As above, I’ve found an undocumented way of doing it for some cases, but the general case appears to be a bug in Unity: the API call doesn’t exist at runtime.
I missed this earlier, but this is your real problem. you haven’t been asking the right question.
User-Generated content is not the same as Unity Project assets. Resources class is designed to load Project assets you put there before you built the game, not as some repository for User content. User content shouldn’t be placed inside the Resources folder. use the persistentDataPath instead.
you can’t create Project assets at runtime, instead you want to create this content within a custom class and the save it under your own custom filetype. You’ll create this file in the persistentDataPath, serialize the custom user data with a formatter and save that format to the file. later you can open the file, pull the formatted stream, and deserialize it back into an object instance. and you got your custom user data back.
There are various ways to serialize this data, I prefer BinaryFormatter for save files, XML is great for tweaking stats and game balance post build, and JSONUtility is a relatively new option added to Unity.
I am not doing those things. I am using Resources to load assets that were packed inside the build, at build-time.
In your example, think about what happens when one of your runtime objects has a reference to a project-wide asset. That’s what this question is about. How do you (de-)serialize the reference?
thats done automatically. if its a Scriptable object, the moment a reference is grabbed Unity will Deserialize data from the asset file to the instance. Deserialization typically happens just before ScriptableObject.OnEnable() fires while serialization happens just after OnDisable. OnDisable will fire when the reference is unloaded. Typically when Resources UnloadUnsedAssets is called, which is usually done automatically when a scene unloads, but you can always choose to manually call it.
so say a scene loads and it has a Gameobject with a monobehaviour. this monobehaviour has a reference to some project-level asset (an Animator Controller, a ScriptableObject, doesn’t matter) as that monobehaviour is getting loaded in Unity will detect that a project asset is needed. so it will grab the GUID thats stored in the field do an internal search of GUID to asset. if an instance of said asset isn’t currently active it creates the instance and backstores it in some internal lookup, deserializes data from within the Asset’s file (if the asset references other assets that need deserialization then it loops to those as well) then calls OnEnable on that asset (if its a scriptableObject). THEN control goes back to the Monobehaviour.
so when a monobehaviour has a public field that references some project asset, unity will make sure that that asset is loaded before any code on that Monobehaviour will run. This is why I recommended the use of public fields and why I felt that it was initially relevant to this thread. Unity is an engine that does a lot of the work for you and saving/loading project assets is one of them (albeit probably not always the exact way you want but in a way that works for most games).
Another reason to do this is now you don’t need to use Resources.Load, you don’t need to put your assets into the Resources folder, and you don’t need the asset’s filename. the serialized reference handles that for you.
You’ve been stating the same problem while I’ve been stating the same answer. If this doesn’t help you then obviously there is a miscommunication happening. Only thing I can say to that is post the code you’ve been trying to use.