Unity中加载图片的几种方法

(1)Resources文件夹下inside图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
【Unity中加载图片的几种方法】using UnityEngine.UI;
public class LoadImage : MonoBehaviour {
public Texture inside;
public Texture outside;
//privateTexture inside;
//privateTexture outside;
void Start () {Debug.Log("LLLLLLLLLLLLL"); }
void LoadAssetsInside()
{
inside = Resources.Load("inside") as Texture; //Resources夹下动态加载
}
void OnGUI()
{
Debug.Log("???????????????????");
if (GUILayout.Button("内部加载资源"))
{LoadAssetsInside(); }
else if (GUILayout.Button("外部加载资源"))
{
StartCoroutine(LoadAssetsOutside());
}
Rect rect1 = new Rect(300, 0, 200, 100); //设置图片位置
GUI.DrawTexture(rect1, inside); //渲染图片
Rect rect2 = new Rect(300, 250, 200, 100); //设置图片位置
GUI.DrawTexture(rect2, outside); //绘制纹理
}
void Update ()
{
Debug.Log("Luuuuuuuuuuu");
}
(2)//Image夹下outside
IEnumerator LoadAssetsOutside()
{
string url = "Image/outside.jpg";
FileInfo fileinfo=new FileInfo(url);
WWW www=new WWW("file://"+fileinfo.FullName);
yield return www;
outside = www.texture;
}
}
(3)通过图片在Assets目录下的地址找到加载图片(很实用)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewHowToPlayDialog : Dialog {
public Image img;
private void Awake() {
img = transform.GetChild(0).GetComponent();
}
private void ChangeImage(int index){
AssetsManager.Load("Assets/_Sprites/Setting/" + index + ".png", (sprite) =>
{
img.sprite = sprite;
});
}
}

    推荐阅读