This section documents Unity Resources API use.
Common precautions:
- Root directory files are read
Assets/Resources
, all resource files are placed in this folder path command fromResources
start writing folder. - Do not read the file suffix file.
Load
After<>
the type of the read writing.- Only type types of objects will be returned.
- Resources folder can be anywhere in the Assets folder.
一、Resouces.load
If we want to read audio files Assets / Resources / AudioClips / 1.wav, the command writing
string fname = "AudioClips/1";
AudioClip clip = Resources.Load<AudioClip>(fname);
Another example is the file you want to read a mix Assets / Resources / AudioMixerGroup.mixer, ibid writing
string fname = "AudioMixerGroup";
AudioMixer mixer = Resources.Load<AudioMixer>(fname);
二、Resources.LoadAll
path file is loaded Resources folder in the folder or all the resources in the file.
If the path is a folder, all resource files will be returned. If the path to a file, only this resource will be returned.
//因为LoadAll只能传入继承自Object的类型,但T也可能不继承自Object,因此“where T : Object”限定T继承自Object
//GetComponent<LoadManager>().LoadAll<AudioClip>("path");
public T[] LoadAll<T>(string path) where T : Object
{
return Resources.LoadAll<T>(path);
}