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;
});
}
}
推荐阅读
- 热闹中的孤独
- Shell-Bash变量与运算符
- JS中的各种宽高度定义及其应用
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 深入理解Go之generate
- 异地恋中,逐渐适应一个人到底意味着什么()
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- “成长”读书社群招募
- 2020-04-07vue中Axios的封装和API接口的管理