SetWebGLText.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. [ExecuteInEditMode]
  7. public class SetWebGLText : MonoBehaviour
  8. {
  9. public static SetWebGLText Instance;
  10. public static Font webglfont;
  11. // Start is called before the first frame update
  12. void Awake()
  13. {
  14. Instance = this;
  15. StartCoroutine(LoadAB());
  16. }
  17. IEnumerator LoadAB()
  18. {
  19. string url = Application.streamingAssetsPath + "/simkai";
  20. #if UNITY_EDITOR
  21. url = "https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/simkai";
  22. #endif
  23. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(url))//
  24. {
  25. yield return req.SendWebRequest();
  26. if (req.error == null)
  27. {
  28. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  29. webglfont = ab.LoadAsset<Font>("simkai");
  30. }
  31. else
  32. {
  33. Debug.Log("���س���" + req.responseCode + "," + req.error);
  34. }
  35. }
  36. }
  37. Queue<callback> dl = new Queue<callback>();
  38. public void GetTexture(string url,Action<Texture2D> msg)
  39. {
  40. callback cb = new callback();
  41. cb.url = url;
  42. cb.msg = msg;
  43. dl.Enqueue(cb);
  44. }
  45. bool isload;
  46. private void Update()
  47. {
  48. if(dl.Count>0&&!isload)
  49. {
  50. isload = true;
  51. callback cb = dl.Dequeue();
  52. StartCoroutine(DownloadImage(cb));
  53. }
  54. }
  55. Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  56. // Э�̷����������첽����ͼƬ
  57. IEnumerator DownloadImage(callback url)
  58. {
  59. if(!stlist.ContainsKey(url.url))
  60. {
  61. // ʹ��UnityWebRequestTexture����ȡͼƬ����
  62. UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
  63. yield return m_webrequest.SendWebRequest();
  64. // ��������Ƿ�ɹ�
  65. if (m_webrequest.result != UnityWebRequest.Result.Success)
  66. {
  67. isload = false;
  68. // ��ӡ������Ϣ
  69. Debug.LogError("Failed to download image");
  70. }
  71. else
  72. {
  73. // �����ش�������ȡ����
  74. Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
  75. stlist.Add(url.url, tex);
  76. url.msg.Invoke(tex);
  77. // ���������õ�RawImage�������ʾ
  78. // GetComponent<RawImage>().texture = tex;
  79. // �����Ҫ����Sprite����ʾ��Image����У�����ʹ�����´���
  80. // Sprite createSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  81. // GetComponent<Image>().sprite = createSprite;
  82. isload = false;
  83. }
  84. }
  85. else
  86. {
  87. yield return null;
  88. url.msg.Invoke(stlist[url.url]);
  89. isload = false;
  90. }
  91. }
  92. public class callback
  93. {
  94. public string url;
  95. public Action<Texture2D> msg;
  96. }
  97. }