关于unity的本地加载(Resource)与外部加载(StreamingAssets)

#第一次写博客,这只是建议,希望有用。

  1. 我相当是刚入unity开发的新人,最近主要是开发查询软件。既然开发查询软件,那就免不了加载图片、视频、音乐音效、pdf等等。一般若是客户需求确定,没有外部修改是比较简单的。
  2. Resource图片加载
    一般加载的是Image(UGUI图片组件),加载图片就是加载Image中的Sprite图片精灵。若是加载Resource中的图片:
Type type = Resources.Load("", typeof(Type)) as Type;//模板

例子:比如加载Resource中文件夹PPTImg文件中所有的图片。

Sprite[] SpriteArr = Resources.LoadAll<Sprite>("PPTImg");
//然后按钮图片切换
void Start()
{
	  LoadImg();
 	  Btn_Next.onClick.AddListener(() =>
       {
           Index++;
           if(Index>= ResList.Count)
           {
               Index = 0;
           }
           ShowBGImg(Index);
       });

       Btn_Pro.onClick.AddListener(() =>
       {
           Index--;
           if (Index < 0 )
           {
               Index = ResList.Count-1;
           }
           ShowBGImg(Index);
       });
       ShowBGImg(Index);
}
void LoadImg()
   {
       Sprite[] SpriteArr = Resources.LoadAll<Sprite>("PPTImg");

       foreach (var spr in SpriteArr)
       {
           if(spr!=null)
           {
               ResList.Add(spr);
           }
       }
   }

   void ShowBGImg(int index)
   {
       print(index);
       BGimg.sprite = ResList[index];
   }

还可以加载工程中的文件中的物体:
Resources.LoadAssetAtPath();
例:
prefab = Resources.LoadAssetAtPath("Assets/Artwork/mymodel.fbx", GameObject);

2、StreamingAssets加载

相比较Resource加载资源,StreamingAssets的优势是可以外部修改资源(自由加载)。但是劣势就是比较占内存,但是相对优势来说这是可以忽略的。毕竟若是用户要改资源,自己都能修改,也就不需要再次经过程序修改打包了(一般来说都是要外置的emmmm),好了,戚~!说正文吧:
1、先是加载路径

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
//加载路径
List<Sprite> characterList = new List<Sprite>();
List<string> characterPathList = new List<string>();

//start调用GetWordPath函数就可以了
    void GetWordPath(string str = null)//这里str可以是StreamingAssets中子文件夹的名称,若是不填默认是该目录下
    {
        if (!Application.runInBackground)
            Application.runInBackground = true;

        string path = Application.streamingAssetsPath + "/" + str;
        string[] picPath = Directory.GetFiles(path);
        for (int i = 0; i < picPath.Length; i++)
        {
            if (!picPath[i].Contains("meta"))
            {
                characterPathList.Add(picPath[i]);
            }
        }
        //循坏加载每一张图片
        for (int i = 0; i < characterPathList.Count; i++)
        {
            StartCoroutine(LoadWordPic(characterPathList[i], i));
        }
        Resources.UnloadUnusedAssets();
        GC.Collect();
    }
    
    //加载图片
    IEnumerator LoadWordPic(string path, int num)
    {
        string url = "file://" + path;
        WWW www = new WWW(url);
        yield return www;
        if (www != null && string.IsNullOrEmpty(www.error))
        {

            Texture2D texture = www.texture;
            Sprite spr = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

            characterList.Add(spr);

        }
        //print(eachSpriteList.Count);
    }
    //***这里path就是路径,如果要字符串(文件名),拆分这个字符串就可以了***
    //若是加载音乐,var music=www.GetAudioClip();(一般是ogg/wav)

3、结尾各位评论朋友需求,本人整理了下,导出个工程,希望帮助各位,转载标明源出处。本着交流原则,免费提供百度云盘链接。看在我这耗费时间整理的份上,点个赞不过分吧。

链接:https://pan.baidu.com/s/12PAuaBZrOE-vvKfqmQ7V9w
提取码:wct7
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值