123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- [ExecuteInEditMode]
- public class SetWebGLText : MonoBehaviour
- {
- public static SetWebGLText Instance;
- public static Font webglfont;
- // Start is called before the first frame update
- void Awake()
- {
- Instance = this;
- StartCoroutine(LoadAB());
- }
- IEnumerator LoadAB()
- {
- string url = Application.streamingAssetsPath + "/simkai";
- #if UNITY_EDITOR
- url = "https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/simkai";
- #endif
- using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(url))//
- {
- yield return req.SendWebRequest();
- if (req.error == null)
- {
- AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
- webglfont = ab.LoadAsset<Font>("simkai");
- }
- else
- {
- Debug.Log("���س���" + req.responseCode + "," + req.error);
- }
- }
- }
- Queue<callback> dl = new Queue<callback>();
- public void GetTexture(string url,Action<Texture2D> msg)
- {
- callback cb = new callback();
- cb.url = url;
- cb.msg = msg;
- dl.Enqueue(cb);
- }
- bool isload;
- private void Update()
- {
- if(dl.Count>0&&!isload)
- {
- isload = true;
- callback cb = dl.Dequeue();
- StartCoroutine(DownloadImage(cb));
- }
- }
- Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
- // Э�̷����������첽����ͼƬ
- IEnumerator DownloadImage(callback url)
- {
- if(!stlist.ContainsKey(url.url))
- {
- // ʹ��UnityWebRequestTexture����ȡͼƬ����
- UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
- yield return m_webrequest.SendWebRequest();
- // ��������Ƿ�ɹ�
- if (m_webrequest.result != UnityWebRequest.Result.Success)
- {
- isload = false;
- // ��ӡ������Ϣ
- Debug.LogError("Failed to download image");
- }
- else
- {
- // �����ش�������ȡ����
- Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
- stlist.Add(url.url, tex);
- url.msg.Invoke(tex);
- // ���������õ�RawImage�������ʾ
- // GetComponent<RawImage>().texture = tex;
- // �����Ҫ����Sprite����ʾ��Image����У�����ʹ�����´���
- // Sprite createSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
- // GetComponent<Image>().sprite = createSprite;
- isload = false;
- }
- }
- else
- {
- yield return null;
- url.msg.Invoke(stlist[url.url]);
- isload = false;
- }
- }
- public class callback
- {
- public string url;
- public Action<Texture2D> msg;
- }
- }
|