unity3d从streamingassets拷贝到persistentassets
来源:互联网 发布:amd表面格式优化 编辑:程序博客网 时间:2024/04/30 08:41
一、图
二、代码码 (注释很详细)
using UnityEngine;using UnityEngine.UI;using System.Collections;using System.IO;using System.Runtime.Serialization.Formatters.Binary;public class main : MonoBehaviour{ public string fileName = "img.png"; public Image img_persistent; public Image img_streaming; public Button btn_copy; public Button btn_clear; public Button btn_close; //public Text txt_info; // Use this for initialization void Start() { img_persistent.sprite = null; img_streaming.sprite = null; LoadImage_from_Streaming(fileName, img_streaming); btn_copy.onClick.AddListener(() => { Debug.Log("click btn copy!"); if (img_persistent.sprite == null) { StartCoroutine(copy(fileName)); } }); btn_clear.onClick.AddListener(() => { clear(); }); btn_close.onClick.AddListener(() => { Application.Quit(); }); } /// <summary> /// 将streaming path 下的文件copy到对应用 /// 为什么不直接用io函数拷贝,原因在于streaming目录不支持, /// 不管理是用getStreamingPath_for_www,还是Application.streamingAssetsPath, /// io方法都会说文件不存在 /// </summary> /// <param name="fileName"></param> IEnumerator copy(string fileName) { string src = getStreamingPath_for_www() + fileName; string des = Application.persistentDataPath + "/" + fileName; Debug.Log("des:" + des); Debug.Log("src:" + src); WWW www = new WWW(src); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log("www.error:" + www.error); } else { //des = Application.persistentDataPath + "/" + fileName; if (File.Exists(des)) { File.Delete(des); } FileStream fsDes = File.Create(des); fsDes.Write(www.bytes, 0, www.bytes.Length); fsDes.Flush(); fsDes.Close(); LoadImage_from_Persistent(fileName, img_persistent); } www.Dispose(); } void clear() { img_persistent.sprite = null; string des = Application.persistentDataPath + "/" + fileName; if (File.Exists(des)) { File.Delete(des); } } /// <summary> /// Load Image from Persistent folder /// </summary> /// <param name="path"></param> /// <param name="uiImg"></param> void LoadImage_from_Persistent(string path, UnityEngine.UI.Image uiImg) { path = getPersistentPath_for_www() + path; Debug.Log("persistent path:" + path); StartCoroutine(wwwLoadImage(path, uiImg)); } /// <summary> /// Load Image from Streaming folder /// </summary> /// <param name="path"></param> /// <param name="uiImg"></param> void LoadImage_from_Streaming(string path, UnityEngine.UI.Image uiImg) { path = getStreamingPath_for_www() + path; Debug.Log("streaming path:" + path); StartCoroutine(wwwLoadImage(path, uiImg)); } IEnumerator wwwLoadImage(string path, UnityEngine.UI.Image uiImg) { WWW www = new WWW(path); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log("www.error:" + www.error); } uiImg.sprite = Sprite.Create(www.texture, new Rect(new Vector2(0, 0), new Vector2(www.texture.width, www.texture.height)), Vector2.zero); www.Dispose(); } //void LogToText(string log) //{ // Debug.Log(log); // txt_info.text += "\n" + log; //} string getStreamingPath_for_www() { string pre = "file://";#if UNITY_EDITOR pre = "file://";#elif UNITY_ANDROID pre = "";#elif UNITY_IPHONE pre = "file://";#endif string path = pre + Application.streamingAssetsPath + "/"; return path; } string getPersistentPath_for_www() { string pre = "file://";#if UNITY_EDITOR || UNITY_STANDALONE_WIN pre = "file:///";#elif UNITY_ANDROID pre = "file://";#elif UNITY_IPHONE pre = "file://";#endif string path = pre + Application.persistentDataPath + "/"; return path; }}
三、demo下载
http://download.csdn.net/detail/anyuanlzh/9111411
0 0
- unity3d从streamingassets拷贝到persistentassets
- unity从streamingassets拷贝到persistentassets,再从persistentassets用www加载进入场景
- Unity3d将资源从StreamingAssets文件夹释放到本地方法(.apk)
- StreamingAssets
- 从vss 到svn拷贝
- 从nand拷贝到sdram
- [Unity3D]Unity3D游戏开发之从Unity3D到Eclipse
- [Unity3D]Unity3D游戏开发之从Unity3D到Eclipse
- UBoot从ROM拷贝到RAM
- 从PC拷贝文件到磁盘
- distcp从ftp到hdfs拷贝文件
- DevExpress从SpreadsheetControl拷贝粘贴到richeditcontrol
- 从拷贝构造函数到内存管理
- ssh 从远程拷贝文件夹到本地
- 从Windows拷贝文件到Linux
- 从assets拷贝数据库到file目录
- ssh从远程拷贝到本地
- 从windows拷贝文件到centos
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- 剑指Offer系列---(24)栈的压入、弹出序列
- java 反编译 行号对齐 decompiler如何去掉行号
- html5 canvas 自定义画图裁剪图片
- 目前在国内见过最全的 IntelliJ Idea 快捷键
- unity3d从streamingassets拷贝到persistentassets
- 副本集指南(目录)
- Eclipse快捷键
- Java - 泛型详解
- eclipse+Jboss配置远程调试
- Xcode模拟器simulators8,7,6,5下载地址及安装
- Intent传递参数方法推荐
- iOS 开发常用的第三方库
- 替换switch操作之转移表