public class L9 : MonoBehaviour
{public AudioSource audioS;private Texture tex;// Start is called before the first frame updatevoid Start(){//Resources资源动态加载的作用//一个工程当中可以有多个Resources文件夹,打包时Unity会自动打包到一起//通过代码动态加载Resources文件夹下指定路径资源,避免繁琐的拖曳操作//常用资源类型//预设体对象 GameObject//音效文件 AudioClip//文本文件 TextAsset//图片文件 Texture//其他类型//预设体对象加载需要实例化,其他资源一般加载之后直接使用//资源同步加载 - 普通方法//预设体对象 - 想要创建在场景上就必须要实例化//第一步,加载预设体的资源文件,加载预设体的配置数据Object obj = Resources.Load("Cube");//第二部,创建预设体即在场景上实例化Instantiate(obj);//音效资源Object obj2 =Resources.Load("Music/mfnf");audioS.clip = obj2 as AudioClip;audioS.Play();//文本资源//文本资源支持的格式 txt xml bytes json html csvTextAsset ta = Resources.Load("TXT/Test") as TextAsset;//文本内容print(ta.text);//字节数据组print(ta.bytes);//图片tex = Resources.Load("Texture/1") as Texture;//如果文件的名字相同,无法准确加载出你想要的内容//解决方法//加载指定类型的资源tex = Resources.Load("TXT/Test",typeof(Texture)) as Texture;//加载指定名字的所有资源Object[] objs = Resources.LoadAll("TXT/Test");foreach(Object item in objs){if(item is Texture){}else if (item is TextAsset){}}//资源同步加载的泛型方法TextAsset ta2 = Resources.Load<TextAsset>("TXT/Test");}private void OnGUI(){GUI.DrawTexture(new Rect(0, 0, 100, 100), tex);}}