devbit

Untitled

May 16th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.IO;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class ObbExtractor : MonoBehaviour
  8. {
  9. //Only use in Preloader Scene for android split APK
  10. private string nextScene = "splash1";
  11. private bool obbisok = false;
  12. private bool loading = false;
  13. private bool replacefiles = false;
  14. //true if you wish to over copy each time
  15.  
  16. private string[] paths = {
  17. "Vuforia/Polishop.dat",
  18. "Vuforia/Polishop.xml"
  19. };
  20.  
  21. void Update()
  22. {
  23. if (Application.platform == RuntimePlatform.Android)
  24. {
  25. if (Application.dataPath.Contains(".obb") && !obbisok)
  26. {
  27. StartCoroutine(CheckSetUp());
  28. obbisok = true;
  29. }
  30. }
  31. else {
  32. if (!loading)
  33. {
  34. StartApp();
  35. }
  36. }
  37. }
  38.  
  39. public void StartApp()
  40. {
  41. loading = true;
  42. SceneManager.LoadSceneAsync(nextScene);
  43. }
  44.  
  45. public IEnumerator CheckSetUp()
  46. {
  47. //Check and install!
  48. for (int i = 0; i < paths.Length; ++i)
  49. {
  50. yield return StartCoroutine(PullStreamingAssetFromObb(paths[i]));
  51. }
  52. yield return new WaitForSeconds(1f);
  53. StartApp();
  54. }
  55.  
  56. //Alternatively with movie files these could be extracted on demand and destroyed or written over
  57. //saving device storage space, but creating a small wait time.
  58. public IEnumerator PullStreamingAssetFromObb(string sapath)
  59. {
  60. if (!File.Exists(Application.persistentDataPath + sapath) || replacefiles)
  61. {
  62. WWW unpackerWWW = new WWW(Application.streamingAssetsPath + "/" + sapath);
  63. while (!unpackerWWW.isDone)
  64. {
  65. yield return null;
  66. }
  67. if (!string.IsNullOrEmpty(unpackerWWW.error))
  68. {
  69. Debug.Log("Error unpacking:" + unpackerWWW.error + " path: " + unpackerWWW.url);
  70.  
  71. yield break; //skip it
  72. }
  73. else {
  74. Debug.Log("Extracting " + sapath + " to Persistant Data");
  75.  
  76. if (!Directory.Exists(Path.GetDirectoryName(Application.persistentDataPath + "/" + sapath)))
  77. {
  78. Directory.CreateDirectory(Path.GetDirectoryName(Application.persistentDataPath + "/" + sapath));
  79. }
  80. File.WriteAllBytes(Application.persistentDataPath + "/" + sapath, unpackerWWW.bytes);
  81. }
  82. }
  83. yield return 0;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment