123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using System;
- using System.IO;
- using UnityEngine.UI;
- public class WebImage
- {
- public string identifyId;
- public virtual void SetImage(Texture2D texture)
- {
- }
- }
- public class WebRawImage: WebImage
- {
- public RawImage mRawImage;
- public override void SetImage(Texture2D texture)
- {
- if (texture == null)
- {
- mRawImage.texture = texture;
- mRawImage.enabled = false;
- }
- else
- {
- mRawImage.texture = texture;
- mRawImage.enabled = true;
- }
- }
- public WebRawImage(RawImage mRawImage, string web_url)
- {
- this.mRawImage = mRawImage;
- this.identifyId = web_url;
- }
- }
- public class WebSprite: WebImage
- {
- public SpriteRenderer mRawImage;
- public override void SetImage(Texture2D texture)
- {
- if (texture == null)
- {
- mRawImage.sprite = null;
- mRawImage.gameObject.SetActive(false);
- }
- else
- {
- mRawImage.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(0.5f,0.5f));
- if(texture.width>texture.height)
- {
- mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.width / 1024f);
- }
- else
- {
- mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.height / 1024f);
- }
- mRawImage.gameObject.SetActive(true);
- }
- }
- public WebSprite(SpriteRenderer mRawImage, string web_url)
- {
- this.mRawImage = mRawImage;
- this.identifyId = web_url;
- }
- }
- public class WebImageTool: MonoBehaviour
- {
- private string format_name = "{0}/{1}";
- private static WebImageTool _instance = null;
- public static WebImageTool Instance
- {
- get
- {
- if (_instance == null)
- {
- GameObject go = new GameObject();
- go.name = "WebImageTool";
- DontDestroyOnLoad(go);
- _instance = go.AddComponent<WebImageTool>();
- }
- return _instance;
- }
- }
- public void LoadImage(WebRawImage img)
- {
- if(img.identifyId == null || img.identifyId.Equals(""))
- {
- img.SetImage(null);
- return;
- }
- StartCoroutine(LoadTexture(img));
- }
- public void LoadImage(WebSprite img)
- {
- if (img.identifyId == null || img.identifyId.Equals(""))
- {
- img.SetImage(null);
- return;
- }
- StartCoroutine(LoadTexture(img));
- }
- private string LocalURL(string url)
- {
- var strusers2 = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- string str1 = string.Join("/", strusers2);//array为数组
- return string.Format(format_name, GameEnum.asset_path, str1);
- }
- /// <summary>
- /// 判断是否本地有缓存,如果有则读取本地的资源,否则读取网络资源
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private IEnumerator LoadTexture(WebImage img)
- {
- if (!string.IsNullOrEmpty(GamePlayerData.Instance.url_image_str))
- {
- if (!File.Exists(LocalURL(img.identifyId)))
- {
- yield return LoadNetWorkTexture(img);
- }
- else
- {
- yield return LoadLocalTexture(img);
- }
- }
- }
- /// <summary>
- /// 本地已缓存
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private IEnumerator LoadLocalTexture(WebImage img)
- {
- //已在本地缓存
- string filePath = "file:///" + LocalURL(img.identifyId);
- CDebug.Log("本地文件路径 " + filePath);
- WWW www = new WWW(filePath);
- yield return www;
- img.SetImage(www.texture);
- }
- /// <summary>
- /// 本地未缓存
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private IEnumerator LoadNetWorkTexture(WebImage img)
- {
- string full_url = string.Format(format_name, GamePlayerData.Instance.url_image_str, img.identifyId);
- CDebug.Log("full_url : " + full_url);
- string img_url = (new Uri(full_url)).AbsoluteUri;
- CDebug.Log(img_url);
- WWW www = new WWW(img_url);
- yield return www;
- CDebug.Log("尝试网络加载");
- #if UNITY_EDITOR || UNITY_STANDALONE_WIN
- if (!Directory.Exists(GameEnum.asset_path))
- {
- Directory.CreateDirectory(GameEnum.asset_path);//创建新路径
- }
- #endif
- try
- {
- //判断网络状态
- if (Application.internetReachability != NetworkReachability.NotReachable)
- {
- //Debug.Log(Application.internetReachability);
- img.SetImage(www.texture);
- //将图片保存至缓存路径
- byte[] pngData = www.texture.EncodeToPNG();
- string png_path = LocalURL(img.identifyId);
- string[] path_ary = png_path.Split('/');
- path_ary[path_ary.Length - 1] = "";
- string file_path = string.Join("/", path_ary);//array为数组
- if (!Directory.Exists(file_path))
- {
- Directory.CreateDirectory(file_path);//创建新路径
- }
- File.WriteAllBytes(png_path, pngData);
- }
- }
- catch
- {
- Debug.Log("网络资源本地失败");
- }
- }
- }
|