sevenPixels

动态加载图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;

public static class Tools {

/// <summary>
/// 加载图片
/// </summary>
/// <param name="_path">地址</param>
/// <param name="_image">需要赋值的图片</param>
/// <returns></returns>
public static IEnumerator IESetImg(string _path, Image _image)
{
WWW _www = new WWW(_path);
yield return _www;
LoadSprite(_www, _image);
}
/// <summary>
/// 生成图片并赋值
/// </summary>
/// <param name="_www"></param>
/// <param name="_sprite"></param>
private static void LoadSprite(WWW _www, Image _image)
{
if (string.IsNullOrEmpty(_www.error))
{
Texture2D _texture2D = _www.texture;
Sprite _spriteTemp = Sprite.Create(_texture2D, new Rect(0, 0, _texture2D.width, _texture2D.height), new Vector2(0.5f, 0.5f));
_image.sprite = _spriteTemp;
}
else
{
Debug.LogError(_www.error);
}
}
}

 

posted on 2019-05-23 14:23  sevenPixels  阅读(619)  评论(0编辑  收藏  举报

导航