写一个Unity中,从StreamingAssets文件夹中加载图片替换场景中物体材质贴图的功能代码
时间: 2023-04-06 22:00:48 浏览: 86
你好,以下是Unity中从StreamingAssets文件夹中加载图片替换场景中物体材质贴图的代码:
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LoadTextureFromStreamingAssets : MonoBehaviour
{
public string textureName; // 图片名称
public Renderer targetRenderer; // 目标物体的Renderer组件
void Start()
{
StartCoroutine(LoadTexture());
}
IEnumerator LoadTexture()
{
string path = Application.streamingAssetsPath + "/" + textureName; // 获取图片路径
UnityWebRequest www = UnityWebRequestTexture.GetTexture(path); // 使用UnityWebRequest加载图片
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
Texture2D texture = DownloadHandlerTexture.GetContent(www); // 获取加载的图片
targetRenderer.material.mainTexture = texture; // 替换目标物体的材质贴图
}
else
{
Debug.LogError("Load texture failed: " + www.error);
}
}
}
```
以上是代码,希望对你有所帮助。