LoadModelAB.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 GameObject loadtext;
  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. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath + "/bd", "1");
  18. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  19. {
  20. yield return req.SendWebRequest();
  21. if (req.error == null)
  22. {
  23. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  24. AssetBundleRequest abr =ab.LoadAssetAsync<GameObject>("1");
  25. yield return abr;
  26. Model = GameObject.Instantiate((GameObject)abr.asset);
  27. Model.transform.SetParent(this.transform);
  28. Model.transform.localEulerAngles = Vector3.zero;
  29. Model.transform.localPosition = Vector3.zero;
  30. // webglfont = ab.LoadAsset<Font>("simkai");
  31. loadtext.SetActive(false);
  32. }
  33. else
  34. {
  35. Debug.Log("���س���" + req.responseCode + "," + req.error);
  36. loadtext.GetComponent<Text>().text=req.error;
  37. }
  38. }
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. }
  44. }