66 * $Notice: See LICENSE.txt for modification and distribution information
77 * Copyright © 2020 by Shen, Jen-Chieh $
88 */
9- using Ookii . Dialogs ;
109using System . Collections ;
1110using System . Collections . Generic ;
1211using System . IO ;
1312using System . Text ;
1413using UnityEngine ;
1514using UnityEngine . Networking ;
16- using UnityEngine . UIElements ;
1715
1816namespace JCSUnity
1917{
@@ -36,20 +34,16 @@ public class JCS_StreamingAssets
3634 [ Header ( "** Check Variables (JCS_StreamingAssets) **" ) ]
3735
3836 [ Tooltip ( "List of file that we are going to download as target." ) ]
39- [ SerializeField ]
4037 public List < string > downloadList = null ;
4138
4239 [ Tooltip ( "Flag to see if currently downloading the file." ) ]
43- [ SerializeField ]
44- private bool mRequesting = false ;
40+ public bool requesting = false ;
4541
4642 [ Tooltip ( "Full request URL." ) ]
47- [ SerializeField ]
48- private string mRequestURL = "" ;
43+ public string requestURL = "" ;
4944
5045 [ Tooltip ( "Request data path." ) ]
51- [ SerializeField ]
52- private string mRequestPath = "" ;
46+ public string requestPath = "" ;
5347
5448 [ Header ( "** Initialize Variables (JCS_StreamingAssets) **" ) ]
5549
@@ -58,10 +52,6 @@ public class JCS_StreamingAssets
5852
5953 /* Setter & Getter */
6054
61- public bool Requesting { get { return this . mRequesting ; } }
62- public string RequestURL { get { return this . mRequestURL ; } }
63- public string RequestPath { get { return this . mRequestPath ; } }
64-
6555 /* Functions */
6656
6757 private void Awake ( )
@@ -90,7 +80,8 @@ public static string StreamingAssetsPath()
9080 public static string CachePath ( )
9181 {
9282 var gs = JCS_GameSettings . instance ;
93- return Application . dataPath + gs . STREAMING_CACHE_PATH ;
83+ string path = JCS_Utility . PathCombine ( Application . dataPath , gs . STREAMING_CACHE_PATH ) ;
84+ return path ;
9485 }
9586
9687 /// <summary>
@@ -146,8 +137,14 @@ public void AddDownloadTarget(string path)
146137
147138 protected override void TransferData ( JCS_StreamingAssets _old , JCS_StreamingAssets _new )
148139 {
149- _old . downloadList = _new . downloadList ;
150140 _old . requestCallback = _new . requestCallback ;
141+
142+ _old . downloadList = _new . downloadList ;
143+ _old . requesting = _new . requesting ;
144+ _old . requestURL = _new . requestURL ;
145+ _old . requestPath = _new . requestPath ;
146+
147+ _old . preloadPath = _new . preloadPath ;
151148 }
152149
153150 private bool ValidCacheData ( string path )
@@ -181,17 +178,17 @@ private byte[] TryStreamingAssets(string path)
181178 private void TryUrlData ( string path )
182179 {
183180 mResultData = null ;
184- mRequestPath = path ;
185- mRequestURL = UrlPath ( ) + mRequestPath ;
181+ requestPath = path ;
182+ requestURL = UrlPath ( ) + requestPath ;
186183
187- mRequesting = true ;
184+ requesting = true ;
188185
189186 StartCoroutine ( GetData ( ) ) ;
190187 }
191188
192189 private IEnumerator GetData ( )
193190 {
194- UnityWebRequest www = UnityWebRequest . Get ( mRequestURL ) ;
191+ UnityWebRequest www = UnityWebRequest . Get ( requestURL ) ;
195192 yield return www . SendWebRequest ( ) ;
196193
197194 bool success = false ;
@@ -204,17 +201,17 @@ private IEnumerator GetData()
204201 else
205202 {
206203 mResultData = www . downloadHandler . data ;
207- WriteFileAsCache ( mRequestPath , mResultData ) ;
204+ WriteFileAsCache ( requestPath , mResultData ) ;
208205
209206 success = true ;
210207 }
211208
212209 if ( requestCallback != null )
213- requestCallback . Invoke ( mRequestPath , success ) ;
210+ requestCallback . Invoke ( requestPath , success ) ;
214211 requestCallback = null ;
215212
216- downloadList . Remove ( mRequestPath ) ;
217- mRequesting = false ;
213+ downloadList . Remove ( requestPath ) ;
214+ requesting = false ;
218215 }
219216
220217 /// <summary>
@@ -235,10 +232,17 @@ private static void WriteFileAsCache(string path, byte[] data)
235232 /// </summary>
236233 private void ProcessDownload ( )
237234 {
238- if ( downloadList . Count == 0 || mRequesting )
235+ if ( downloadList . Count == 0 || requesting )
239236 return ;
240237
241- TryUrlData ( downloadList [ 0 ] ) ;
238+ string path = downloadList [ 0 ] ;
239+
240+ if ( NeedRequestData ( ReadAllBytes ( path ) ) )
241+ TryUrlData ( path ) ;
242+ else
243+ {
244+ downloadList . RemoveAt ( 0 ) ;
245+ }
242246 }
243247 }
244248}
0 commit comments