一、图
二、代码码 (注释很详细)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164using 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
最后
以上就是高挑汽车最近收集整理的关于unity3d从streamingassets拷贝到persistentassets的全部内容,更多相关unity3d从streamingassets拷贝到persistentassets内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复