123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Networking;
- public class LoadHelper
- {
- #region 图片加载
- /// <summary>
- /// 从Streamming下加载图片
- /// </summary>
- /// <param name="filePath">Streamming下路径</param>
- /// <returns></returns>
- public static Sprite LoadSpriteFromStreamming(string filePath)
- {
- Sprite sprite = null;
- string tmpPath = Path.Combine(Application.streamingAssetsPath, filePath);
- sprite = LoadSprite(tmpPath);
- return sprite;
- }
- /// <summary>
- /// 加载精灵
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static Sprite LoadSprite(string filePath)
- {
- Sprite sprite = null;
- Texture2D tmpTexture = LoadTexture(filePath);
- if (tmpTexture != null)
- {
- sprite = Sprite.Create(tmpTexture, new Rect(0f, 0f, tmpTexture.width, tmpTexture.height), Vector2.one * 0.5f);
- }
- return sprite;
- }
- /// <summary>
- /// 加载Texture
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static Texture2D LoadTexture(string filePath)
- {
- Texture2D tmpTexture = null;
- filePath = GetSuffixExtension(filePath);
- if (!File.Exists(filePath))
- {
- Debug.Log("<color=red>" + filePath + "不存在" + "</color>");
- return null;
- }
- using (FileStream stream = new FileStream(filePath, FileMode.Open))
- {
- byte[] tmpArr = new byte[stream.Length];
- stream.Read(tmpArr, 0, tmpArr.Length);
- stream.Close();
- tmpTexture = new Texture2D(1, 1);
- tmpTexture.LoadImage(tmpArr);
- }
- return tmpTexture;
- }
- /// <summary>
- /// 检索图片类型
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private static string GetSuffixExtension(string filePath)
- {
- string tmpPath = filePath;
- if (File.Exists(filePath + ".png")) { tmpPath = filePath + ".png"; }
- if (File.Exists(filePath + ".PNG")) { tmpPath = filePath + ".PNG"; }
- if (File.Exists(filePath + ".JPG")) { tmpPath = filePath + ".JPG"; }
- if (File.Exists(filePath + ".jpg")) { tmpPath = filePath + ".jpg"; }
- if (File.Exists(filePath + ".jpeg")) { tmpPath = filePath + ".jpeg"; }
- return tmpPath;
- }
- #endregion
- #region 音频
- /// <summary>
- /// 加载音频
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="audioType"></param>
- /// <param name="callBack"></param>
- /// <returns></returns>
- IEnumerator LoadAudioClip(string filePath, AudioType audioType, Action<AudioClip> callBack = null)
- {
- filePath = GetAudioClipExtension(filePath);
- using (UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioClipType(filePath)))
- {
- yield return unityWebRequest.SendWebRequest();
- if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
- {
- Debug.LogError(unityWebRequest.error);
- }
- else
- {
- AudioClip tmpAudioClip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
- if (tmpAudioClip != null)
- {
- callBack?.Invoke(tmpAudioClip);
- }
- else
- {
- Debug.LogError("音频加载失败");
- }
- }
- }
- }
- /// <summary>
- /// 加载视频
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="audioType"></param>
- /// <param name="callBack"></param>
- /// <returns></returns>
- IEnumerator LoadVideoClip(string filePath, AudioType audioType, Action<AudioClip> callBack = null)
- {
- filePath = GetAudioClipExtension(filePath);
- using (UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioClipType(filePath)))
- {
- yield return unityWebRequest.SendWebRequest();
- if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
- {
- Debug.LogError(unityWebRequest.error);
- }
- else
- {
- AudioClip tmpAudioClip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
- if (tmpAudioClip != null)
- {
- callBack?.Invoke(tmpAudioClip);
- }
- else
- {
- Debug.LogError("音频加载失败");
- }
- }
- }
- }
- /// <summary>
- /// 获取音频格式
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private static AudioType GetAudioClipType(string filePath)
- {
- AudioType audioType = AudioType.UNKNOWN;
- if (File.Exists(filePath + ".mp3") || File.Exists(filePath + ".MP3")) { audioType = AudioType.MPEG; }
- if (File.Exists(filePath + ".wav") || File.Exists(filePath + ".WAV")) { audioType = AudioType.WAV; }
- if (File.Exists(filePath + ".vag") || File.Exists(filePath + ".VAG")) { audioType = AudioType.VAG; }
- return audioType;
- }
- /// <summary>
- /// 获取音频全路径
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private static string GetAudioClipExtension(string filePath)
- {
- string audioPath = string.Empty;
- if (File.Exists(filePath + ".mp3")) { audioPath = filePath + ".MP3";}
- if (File.Exists(filePath + ".MP3")) { audioPath = filePath + ".MP3"; }
- if (File.Exists(filePath + ".wav")) { audioPath = filePath + ".wav"; }
- if (File.Exists(filePath + ".WAV")) { audioPath = filePath + ".WAV"; }
- if (File.Exists(filePath + ".vag")) { audioPath = filePath + ".vag"; }
- if (File.Exists(filePath + ".VAG")) { audioPath = filePath + ".VAG"; }
- return audioPath;
- }
- #endregion
- }
|