LoadModelAB.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. public class LoadModelAB : MonoBehaviour
  7. {
  8. public Text t;
  9. public GameObject Model;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. StartCoroutine(LoadAB());
  14. }
  15. IEnumerator LoadAB()
  16. {
  17. Debug.Log(Application.streamingAssetsPath + "/main_1");
  18. string url = Application.streamingAssetsPath + "/main_1";
  19. #if UNITY_EDITOR
  20. url = "https://filebrowser.ghz-tech.com:8843/api/public/dl/1WZasi8T/unity/daping/main_1";
  21. #endif
  22. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(url))
  23. {
  24. req.SendWebRequest();
  25. while (!req.isDone)
  26. {
  27. t.text = "模型加载中 " + (req.downloadProgress/2f * 100).ToString("F2") + "%";
  28. yield return null;
  29. }
  30. if (req.error == null)
  31. {
  32. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  33. AssetBundleRequest abr = ab.LoadAssetAsync<GameObject>("main_1");
  34. while(!abr.isDone)
  35. {
  36. t.text = "模型加载中 "+ (50+ abr.progress / 2f*100).ToString("F2") + "%";
  37. yield return null;
  38. }
  39. Model = GameObject.Instantiate((GameObject)abr.asset);
  40. Model.transform.SetParent(this.transform);
  41. Model.transform.localEulerAngles = Vector3.zero;
  42. Model.transform.localPosition = Vector3.zero;
  43. t.gameObject.SetActive(false);
  44. ModelDataManager.Instance.isLoadModel = true;
  45. }
  46. else
  47. {
  48. t.text = "模型加载失败";
  49. Debug.Log("���س���" + req.responseCode + "," + req.error);
  50. }
  51. }
  52. }
  53. // Update is called once per frame
  54. void Update()
  55. {
  56. }
  57. }