We have LoadAsync, cool, now LoadAll definitely takes longer so async on this one would make sense yah?
So where is it.
Itâs not Resources, but itâs the same basic idea. If you really want a lot of fine control, move to asset bundles.
Bundles require to export them to external files and you need a bunch of boilerplate code to handle them in editor, right?
Itâs right in front of you.
https://docs.unity3d.com/ScriptReference/Resources.LoadAsync.html
read the post - LoadALLAsync
Youâre asking for it to load the entire resources folder, correct? It clearly states it does that if you pass an empty string.
Lol. When in doubt, read the manual. Canât believe I missed that.
ResourceRequest.asset returns only one object no?
I imagine youâd cast it to an Object[ ].
Why not try it and see? You could answer that more accurately and faster by simply opening Unity.
I didnât know you could cast an Object to an Object[ ] so I tried () and âasâ and nope it doesnât compile.
So I did my own soup casting the result to IEnumerable and building the list.
.asset is null, what am I doing wrong?
Here is the code.
public List<GameObject> objects = new List<GameObject>();
IEnumerator Start () {
var timer = Time.time;
var resourceLoad = Resources.LoadAsync ("", typeof(GameObject));
yield return resourceLoad;
Debug.Log (Time.time-timer);
if (resourceLoad.asset.GetType ().IsArray) {
var objs = (IEnumerable)resourceLoad.asset;
foreach (var o in objs)
objects.Add ((GameObject)o);
}
}
Got a reply from Unity QA itâs a bug and sent for resolution. Letâs see if resolution turns out to be removing the bit in the documentation ![]()
bug# 858737
So I guess this never got resolved? LoadAsync only loads a single asset, and there is no LoadAllAsync. The only thing I can find is this one feature request
https://feedback.unity3d.com/suggestions/resources-dot-loadallasync
But with 5 votes after 3 years, I am not holding my breath
Here is the corresponding bug-report:
If you need âLoadAllâ functionality, you could build a table that contains asset paths of all resources at build time and then use this information to load and look-up assets at runtime.
That being said, Unity Technologies recommends to avoid the Resources system.
https://unity3d.com/learn/tutorials/topics/best-practices/resources-folder
Your Google Fu is on point. Cheers.
Itâs nice and all the whole idea âavoid the resources systemâ but itâs mighty convenient, and it seems like the best bet when working with Android and especially WebGL. Iâm struggling with this and the idea I had was the same as mentioned, just somehow have a list of the relative resource paths. Itâs taking a while to crack the puzzle, though.
Using WebGL by the way youâre also limited in where you canât hang the browser with while { !isDone }; as wellâŚ
There IS a solution if you want to do things like preload assets from Resources at your sceneâs start.
Basically, you can write a TextAsset to a folder called âPathsâ (or whatever youâd like) in your resources folder upon the import of your asset like soâŚthis example uses audio. This is an editor script thatâs placed into an âEditorâ folder:
public class MyAudioPostprocessor : AssetPostprocessor
{
private string GetFileDirectoryForResources(string inputString)
{
inputString = inputString.Replace("\\", "/");
string outputString = inputString.Substring(inputString.IndexOf("Resources/") + 10);
return outputString;
}
void OnPreprocessAudio()
{
AudioImporter audioImporter = (AudioImporter)assetImporter;
AudioImporterSampleSettings audioImporterSampleSettings = audioImporter.defaultSampleSettings;
audioImporterSampleSettings.loadType = AudioClipLoadType.DecompressOnLoad;
audioImporter.defaultSampleSettings = audioImporterSampleSettings;
string filePath = audioImporter.assetPath;
string fileNameNoExtension = Path.GetFileNameWithoutExtension(filePath);
string fileDirectory = Path.GetDirectoryName(filePath);
string fullPathNoExtension = fileDirectory + "\\" + fileNameNoExtension;
string fileDirectoryToWriteIntoFile = GetFileDirectoryForResources(fileDirectory + "/" + fileNameNoExtension);
File.WriteAllText(Application.dataPath + "/Global/Resources/Paths/" + fileNameNoExtension + ".txt", fileDirectoryToWriteIntoFile);
}
}
Then what you do is when you preload the asset, in my case audio, you would load the text asset once you have the audio clipâs name you want to load, use textAsset.text to get the path and then use that the load the actual audio clip. This is so that then you can play audio files wherever you want, in any subfolder etc⌠and itâll find it.
TextAsset pathTextAsset = Resources.Load<TextAsset>("Paths/" + AudioClipToLoadName + "_Path");
string audioPath = pathTextAsset.text;
AudioClip audioClip = Resources.Load<AudioClip>(audioPath);
preloadedAudioClips.Add(audioClip);
That way you avoid uses LoadAll at all. The only thing that would make LoadAll useful would be Async, but there are ways around it.
I think this is a good solution if youâre making a simple WebGL game and want to load audio thatâs gonna be packed up inside your built game. The only thing you then have to focus on externally is save/load files.
Wouldnât it be way simpler if you either:
- Add a MonoBehaviour to a gameobject in your scene that has an array of AudioClipâs and serves of some kind of audioclip database.
- Add a reference to the AudioClip in the MonoBehaviour where you want to play it.
@Peter77 The game object would just have to be global and hold audioclips, then when a scene loads in itâs preloading process it needs to detect in your events in your scene where a PlayBGM would happen or whatever your event is called and preload that clip, then unload the clips when the scene transfers into the next oneâŚunless youâre just streaming the clips. It would be like a scene manager - > audio manager that would have that array or list of audio clips I guess, then maybe make some methods like PreloadClip() UnloadAll() and all that. I donât know if thereâs an easier way than that.
If there is a reference to an AudioClip in a scene, Unity loads the clip automatically, unless you disable the âPreload Audio Dataâ option on the clip:
If you switch to another scene, Unity unloads the AudioClip when no longer used.
There shoulnât be any need for a global manager if you just want to have that basic preload/unload built-in functionality.