public static Sprite GetSprite(string pPath, bool nonReandable = true) { if (!System.IO.File.Exists(pPath)) { #if UNITY_EDITOR Debug.LogError("找不到路径 : " + pPath); #endif return(null); } var bytes = System.IO.File.ReadAllBytes(pPath); var tex = new Texture2D(1, 1); tex.LoadImage(bytes, nonReandable); Sprite spr = CCTool.TextureToSprite(tex); return(spr); }
/// <summary> /// 传入图片路径,加载图片到Image /// </summary> ///<param name="pImg">需要设置sprite的UIImage</param> /// <param name="pPath">需要加下载的图片路径</param> /// <param name="pSize">UIImage的大小</param> public static bool SetImageSprite(Image pImg, string pPath, Vector2 pSize) { if (pImg == null) { #if UNITY_EDITOR Debug.LogError("找不到图片"); #endif return(false); } if (!System.IO.File.Exists(pPath)) { #if UNITY_EDITOR Debug.LogError("找不到路径 : " + pPath); #endif return(false); } var bytes = System.IO.File.ReadAllBytes(pPath); var tex = new Texture2D(1, 1); tex.LoadImage(bytes, true); //tex.Apply(); pImg.sprite = CCTool.TextureToSprite(tex); float tWidth = pSize.x; float tHeight = pSize.y; if (tex.width / tex.height > tWidth / tHeight) { tHeight = tex.height * tWidth / tex.width; } else { tWidth = tex.width * tHeight / tex.height; } pImg.GetComponent <RectTransform>().sizeDelta = new Vector2(tWidth, tHeight); return(true); }