
在本文中,3D天堂将解释如何在Unity场景中运行时加载图像,会介绍一些方法,可以通过这些方法运行时在Unity中添加图像。
从资源文件夹加载图像

在Unity中准备一个简单的场景,以便在运行时加载图像,当前在场景中添加了RawImage游戏对象。
加载纹理
使用Resources.Load方法从资源文件夹加载图像。
首先,将图像复制到Resources(资源)文件夹中,使用哪种图片随你自己喜欢。

将以下脚本附加到主摄像机或任何其他游戏对象。
using UnityEngine;
using UnityEngine.UI;
public class LoadTexture : MonoBehaviour {
Texture2D myTexture;
// Use this for initialization
void Start () {
// load texture from resource folder
myTexture = Resources.Load ("Images/i3dttImage") as Texture2D;
GameObject rawImage = GameObject.Find ("RawImage");
rawImage.GetComponent<RawImage> ().texture = myTexture;
}
}
运行游戏如下所示:

加载精灵
首先在Unity编辑器中将图像转换为sprite,然后就可以像纹理一样在运行时加载它。

在场景中,需要使用Image 游戏对象,而不是RawImage游戏对象来加载精灵。

将下面给定的脚本添加到主摄像机或任何其他游戏对象。
using UnityEngine;
using UnityEngine.UI;
public class LoadSprite : MonoBehaviour {
Sprite sprite;
// Use this for initialization
void Start () {
sprite = Resources.Load <Sprite>("Images/i3dttSprite");
GameObject image = GameObject.Find ("Image");
image.GetComponent<Image>().sprite = sprite;
}
}
运行游戏如下所示:

在运行时从纹理创建精灵
还可以在运行时从纹理创建精灵。
// convert texture to sprite if required otherwise load sprite directly from resources folder
Texture2D myTexture = Resources.Load <Texture2D>("Images/i3dttImage");
sprite = Sprite.Create (myTexture, new Rect (0, 0, myTexture.width, myTexture.height), new Vector2 (0.5f,0.5f));
从本地电脑任何位置加载图像
使用WWW类从任何本地目录加载图像,确保文件路径正确,文件路径应该是这样子的 → www = new WWW (“file:///E://i3dttImage.png”)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoadLocalMachine : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WWW www = new WWW ("file:///E://i3dttImage.png");
while(!www.isDone)
yield return null;
GameObject image = GameObject.Find ("RawImage");
image.GetComponent<RawImage>().texture = www.texture;
}
}
运行游戏,将获得与上述相同的结果。
从字节数组加载图像
还可以使用Texture2D类的LoadImage方法使用字节数组加载图像。
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class LoadImageDemo : MonoBehaviour {
// Use this for initialization
void Start () {
// read image and store in a byte array
byte[] byteArray = File.ReadAllBytes(@"D:\i3dttImage.png");
//create a texture and load byte array to it
// Texture size does not matter
Texture2D sampleTexture = new Texture2D(2,2);
// the size of the texture will be replaced by image size
bool isLoaded = sampleTexture.LoadImage(byteArray);
// apply this texure as per requirement on image or material
GameObject image = GameObject.Find("RawImage");
if (isLoaded)
{
image.GetComponent<RawImage>().texture = sampleTexture;
}
}
}
从链接加载图像
也可以使用WWW类从任何URL加载图像。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoadWWW : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WWW www = new WWW("https://su.i3dtt.com/i3dttImage.png");
while (!www.isDone)
yield return null;
Debug.Log (www.texture.name);
GameObject rawImage = GameObject.Find ("RawImage");
rawImage.GetComponent<RawImage> ().texture = www.texture;
}
}
运行游戏将获得与上述相同的结果。
不过图像也可以从资产包中加载,可以参阅站内文章:
…
以上是关于在Unity运行时加载图像的全部内容,如果你有任何反馈,请随时在本页面下方留言。