TestLoadVuforia.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class TestLoadVuforia : MonoBehaviour
  7. {
  8. public GameObject VuforiaManager;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. //StartCoroutine(GetRequest("YCYL3_3Test.dat"));
  13. //StartCoroutine(GetRequest("YCYL3_3Test.xml"));
  14. StartCoroutine(GetRequestBundle("spacestation", 111));
  15. }
  16. private IEnumerator GetRequest(string methodName)
  17. {
  18. string url = Application.streamingAssetsPath + "/Vuforia/" + methodName;
  19. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  20. {
  21. yield return webRequest.SendWebRequest();
  22. if (webRequest.isHttpError || webRequest.isNetworkError)
  23. {
  24. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  25. }
  26. else
  27. {
  28. string path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
  29. if (!Directory.Exists(path))
  30. {
  31. Directory.CreateDirectory(path);
  32. }
  33. File.WriteAllBytes(Application.persistentDataPath + "/StreamingAssets/Vuforia/" + methodName, webRequest.downloadHandler.data);
  34. if(methodName == "YCYL3_3Test.dat")
  35. {
  36. StartCoroutine(OpenVuforia(5));
  37. }
  38. }
  39. }
  40. }
  41. private IEnumerator OpenVuforia(float timer)
  42. {
  43. yield return new WaitForSeconds(timer);
  44. VuforiaManager.SetActive(true);
  45. }
  46. private IEnumerator GetRequestBundle(string methodName ,int time)
  47. {
  48. string url = Application.streamingAssetsPath + "/Model/" + methodName;
  49. Debug.Log(url);
  50. using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url))
  51. {
  52. yield return webRequestAB.SendWebRequest();
  53. if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  54. {
  55. Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.text);
  56. }
  57. else
  58. {
  59. AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  60. // Debug.Log(bundle.GetAllAssetNames()[0]);
  61. GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  62. List<GameObject> listObjs = new List<GameObject>();
  63. listObjs.Add(obj);
  64. object value = listObjs;
  65. GameObject model = GameObject.Instantiate(obj);
  66. model.transform.position = Vector3.zero;
  67. }
  68. }
  69. }
  70. }