StreamingAssets加载资源(音频、图片)源码
1、加载音频(unity能直接应用的格式ogg/wav)(ps:本人当前理解,或许后续优化)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using Assets.Public;
public class LoadMusic : MonoBehaviour
{
public AudioSource Audio; //音乐资源组件
public AudioClip[] _clips; //加载的音乐数组
//private AudioSource[] AudioSour;
int index = 0; //当前播放下标
bool first = false; //播放第一个开始标量
bool isload = false; //加载完成标量
//音乐路径
List<string> eachPicPathList = new List<string>();
float musicValue;
private void Awake()
{
GetCountPath();
}
// Start is called before the first frame update
void Start()
{
musicValue = IniSetting.GetFloat("音量大小", "背景音乐设置", 0.5f);
Audio.volume = musicValue;
}
// Update is called once per frame
void Update()
{
if(Global.IsMusic) //播放音乐标量打开
{
if (!first) //
{
index = 0;
Audio.clip = _clips[index];
Audio.Play();
first = true;
}
}
else
{
Audio.Stop();
first = false;
}
if (isload) //加载完成
{
isload = false;
_clips = new AudioClip[eachPicPathList.Count];
for(int i=0;i< _clips.Length;i++)
{
StartCoroutine(LoadMusicBGFunc(i, eachPicPathList[i], _clips));
}
}
if(Audio.isPlaying)
{
first = true;
}
if(!Audio.isPlaying&&first) //若是播放完毕,更换音乐
{
index++;
if (index >= _clips.Length)
{
index = 0;
}
Audio.clip = _clips[index];
Audio.Play();
Debug.LogError(index);
}
if (Global.IsMute)
{
Audio.mute = true;
Global.IsMute = false;
}
if(Global.PlayMusic)
{
Audio.mute = false;
Global.PlayMusic = false;
}
}
//加载音乐资源
IEnumerator LoadMusicBGFunc(int index,string str, AudioClip[] clips)
{
WWW w = new WWW(str);//("file://" + Application.streamingAssetsPath + "/Music/" + index.ToString()+ ".wav");
yield return w;
if(w.error==null&&w.isDone)
{
AudioClip s = w.GetAudioClip();
clips[index] = s;
if(index==0)
{
Global.IsMusic = true;
//Audio.clip = s;
//Audio.Play();
}
}
}
//获取音乐文件夹的每一个音乐路径-------》以及获取音乐数量
public void GetCountPath()
{
string path = Application.streamingAssetsPath + "/Music";
string[] picPath = Directory.GetFiles(path);
for (int i = 0; i < picPath.Length; i++)
{
if (!picPath[i].Contains("meta"))
{
eachPicPathList.Add(picPath[i]);
}
}
Resources.UnloadUnusedAssets();
GC.Collect();
isload = true;
}
}
//公共命名空间,方便随时访问变量以及修改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Public
{
public class Global
{
//背景音乐标量--》默认false--》是否静音
public static bool IsMusic = false;
public static bool PlayMusic = false;
//背景音乐静音mute
public static bool IsMute = false;
//中控接收信息标量
public static bool IsReceMessage = false;
//节点位置信息返回客户端标量
public static bool ReturnMsg = false;
//判断节点1与节点八特殊性
public static bool Flag = false;
//视频进度滑动条
public static bool isClickSlider = true;
}
}
2、加载图片(ps:本人当前理解,或许后续优化)
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using RenderHeads.Media.AVProVideo;
using System.IO;
using System;
using Assets.Public;
public class ShowPanelMgr_1 : MonoBehaviour
{
public int ShowPanelIndex;
public Transform BtnPanel;
//界面按钮,点击显示下一张图片或者上一张图片
public Button BtnLeft;
public Button BtnRight;
private int panelIndexID = -1;
bool onClick = false;
int showIndex = 0;
//每张图片的路径集合
List<string> eachPicPathList = new List<string>();
//每张图片的Texture集合
List<Texture2D> eachTextureList = new List<Texture2D>();
//UGUI中的RawImage,显示图片。。。也可以是Image(ps:Texture2d转化成sprite在赋值给image)
private RawImage ShowImg; //显示加载图片
// Start is called before the first frame update
void Start()
{
ShowImg = this.transform.GetComponent<RawImage>();
//加载路径-》str
string str = "/Picture/" + ShowPanelIndex.ToString();
//加载图片
loadLettr(str);
Debug.LogError("str:" + str);
//按钮监听
BtnLeft.onClick.AddListener(() =>
{
showIndex--;
if (showIndex < 0)
{
showIndex = eachTextureList.Count - 1;
}
onClick = true;
});
BtnRight.onClick.AddListener(() =>
{
showIndex++;
if (showIndex >= eachTextureList.Count)
{
showIndex = 0;
}
onClick = true;
});
//CurMediaPlayer.Events.AddListener(MediaEventHandler);
}
// Update is called once per frame
void Update()
{
if(onClick)
{
onClick = false;
ShowImg.texture = eachTextureList[showIndex];
Debug.LogError("showIndex:" + showIndex);
}
}
//
private void OnDisable()
{
showIndex = 0;
}
public void SetpanelIndexID(int id)
{
panelIndexID = id;
}
#region 加载资源 ----加载背景按钮
public void loadLettr(string pathLoad)
{
Application.runInBackground = true;
GetPath(pathLoad); //
}
//图片路径 -----
void GetPath(string pathLoad)
{
string path = Application.streamingAssetsPath + pathLoad;
string[] picPath = Directory.GetFiles(path);
for (int i = 0; i < picPath.Length; i++)
{
if (!picPath[i].Contains("meta"))
{
eachPicPathList.Add(picPath[i]);
}
}
//判断是否显示按钮
if (eachPicPathList.Count > 1)
{
BtnPanel.gameObject.SetActive(true);
}
else
{
BtnPanel.gameObject.SetActive(false);
}
var strPath = eachPicPathList[0].Split('\\'); //获取路径
//循坏加载每一张图片
for (int i = 0; i < eachPicPathList.Count; i++)
{
//unity外部加载图片,因图片大小原因,协程处理的时间不一样导致加载顺序会出现混乱
//这里tmp是为了按照顺序进行图片加载(ps:我这文件里面图片命名是 1.jpg 2.jpg等等)
string tmp = strPath[0] + "\\" + (i+1).ToString() + ".jpg";
Debug.LogError("tmp:" + tmp);
StartCoroutine(LoadPic(tmp, i));
}
Resources.UnloadUnusedAssets();
GC.Collect();
}
//加载图片
IEnumerator LoadPic(string path,int index)
{
string url = "file://" + path;
WWW www = new WWW(url);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
Texture2D texture = TextureToTexture2D(www.texture);
eachTextureList.Add(texture);
if(index==0)
{
ShowImg.texture = texture;
}
//Sprite spr = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
//eachSpriteList.Add(spr);
}
}
//提升图片清晰度,,因为unity外部加载图片会是默认格式(压缩处理),若是图片分辨率太大加载会有些模糊
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
#endregion 加载资源
}
这次先到这里啦,主要是我工作软件功能需要,我整理了下,希望对大家都有帮助