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 加載資源
}
這次先到這里啦,主要是我作業軟體功能需要,我整理了下,希望對大家都有幫助
備注:以后會更新unity讀取json檔案(配置加載)、熱更新tolua(會一些)、服務器及客戶端整理,
歡迎評論,一起學習,一起討論
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/232098.html
標籤:其他
