using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Events;
using System.IO;
public static class StreamingAssetPathConfigReader
{
///
/// 读取StreamingAsset中的配置文件
///
///
///
///
public static IEnumerator TextReader(string configName, UnityAction action = null)
{
string path;
#if UNITY_WIN_STANDALONE || UNITY_IPHONE &&!UNITY_EDITOR
path ="file://"+ Application.streamingAssetsPath + configName;
#else
path = Application.streamingAssetsPath + "/" + configName;
#endif
UnityWebRequest unityWebRequest = UnityWebRequest.Get(path);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.error != null)
Debug.Log(unityWebRequest.error);
else
{
string content = unityWebRequest.downloadHandler.text;
if (action != null)
action(content);
}
}
///
/// 读取streamingAsset中的图片
///
///
///
///
public static IEnumerator TextureReader(string mediaName,UnityAction action)
{
string path;
#if UNITY_WIN_STANDALONE || UNITY_IPHONE &&!UNITY_EDITOR
path ="file://"+ Application.streamingAssetsPath + configName;
#else
path = Application.streamingAssetsPath + "/" + mediaName;
#endif
UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(path);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.error != null)
Debug.Log(unityWebRequest.error);
else
{
byte[] bts = unityWebRequest.downloadHandler.data;
if (action != null)
{
action(DownloadHandlerTexture.GetContent(unityWebRequest));
}
}
}
///
/// 读取streamingAsset文件夹中的多媒体(音频)
///
///
///
///
public static IEnumerator AudioClipReader(string mediaName, UnityAction action=null)
{
string path;
#if UNITY_WIN_STANDALONE || UNITY_IPHONE &&!UNITY_EDITOR
path ="file://"+ Application.streamingAssetsPath + configName;
#else
path = Application.streamingAssetsPath + "/" + mediaName;
#endif
FileInfo fileInfo = new FileInfo(path);
string fileExtension = fileInfo.Extension;
AudioType audioType;
switch (fileExtension)
{
case ".mp3":
audioType = AudioType.MPEG;
break;
case ".ogg":
audioType = AudioType.OGGVORBIS;
break;
case ".wav":
audioType = AudioType.WAV;
break;
case ".aiff":
audioType = AudioType.AIFF;
break;
default:
audioType = AudioType.MPEG;
break;
}
UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(path, audioType);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.error != null)
Debug.Log(unityWebRequest.error);
else
{
if (action != null)
action(DownloadHandlerAudioClip.GetContent(unityWebRequest));
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PathDebug : MonoBehaviour
{
public GUISkin guiSkin;
public RawImage rawImage;
public AudioSource audioSource;
string s = "";
void OnGUI()
{
GUI.skin = guiSkin;
GUI.Label(new Rect(100, 0, Screen.width, Screen.height), s);
if (GUI.Button(new Rect(0, 0, 100, 50), "LoadCsv"))
StartCoroutine(StreamingAssetPathConfigReader.TextReader("config.csv", ShowCsv));
if (GUI.Button(new Rect(0, 50, 100, 50), "LoadTxt"))
StartCoroutine(StreamingAssetPathConfigReader.TextReader("Test.txt", ShowTxt));
if (GUI.Button(new Rect(0, 100, 100, 50), "LoadJson"))
StartCoroutine(StreamingAssetPathConfigReader.TextReader("Test.json", ShowJson));
if (GUI.Button(new Rect(0, 150, 100, 50), "ShowImage"))
StartCoroutine(StreamingAssetPathConfigReader.TextureReader("Picture.png", ShowTexture));
if (GUI.Button(new Rect(0, 200, 100, 50), "PlayerAudio"))
StartCoroutine(StreamingAssetPathConfigReader.AudioClipReader("audio.wav", AudioPlay));
}
private void WriteText(string content)
{
this.s += content;
}
///
/// 读取csv文件
///
///
private void ShowCsv(string s)
{
this.s = s;
}
///
/// 读取txt文件
///
///
private void ShowTxt(string s)
{
print("----" + s);
string[] content = s.Split('\n');
List line = new List();
for (int i = 0; i < content.Length; i++)
{
line.Add(content[i]);
}
List txtMessages = new List();
TxtMessage currentLine = new TxtMessage();
for (int i = 0; i < line.Count; i++)
{
string key = line[i].Split(':')[0];
string value = line[i].Split(':')[1];
currentLine.key = key;
currentLine.value = value;
txtMessages.Add(currentLine);
}
Debug.Log(txtMessages[0].value);
if (txtMessages[0].key.Equals("信息是否保密"))
if (txtMessages[0].value.Trim().Equals("是"))
{
this.s = "该用户信息保密,无法访问";
}
else
{
this.s = "";
for (int i = 1; i < content.Length; i++)
{
this.s += content[i] + "\n";
}
}
}
///
/// 读取json文件
///
///
private void ShowJson(string s)
{
//json格式是utf-8 不带bom功能 ,不然会报错
JsonMessage jsonMessage = JsonUtility.FromJson(s);
this.s = string.Format("{0}"+jsonMessage.name+"\n","用户名: ");
this.s += string.Format("{0}" + jsonMessage.age+"\n","年龄: ");
this.s += string.Format("{0}" + jsonMessage.sex+"\n","性别: ");
}
private void ShowTexture(Texture texture)
{
rawImage.texture = texture;
}
private void AudioPlay(AudioClip audioClip)
{
audioSource.clip = audioClip;
audioSource.Play();
}
}
///
/// Txt信息文档
///
public struct TxtMessage
{
public string key;
public string value;
}
///
/// json文件信息
///
[System.Serializable]
public struct JsonMessage
{
public string name;
public double age;
public string sex;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
private void Awake()
{
StartCoroutine(StreamingAssetPathConfigReader.TextReader("Config.txt", ShowTxt));
}
private void ShowTxt(string s)
{
lineArray = s.Split('\r');
}