定义一个RawImage 和一个按钮button, 此脚本挂在button 上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Load_Scence : MonoBehaviour
{
string path;
public RawImage rawImage;
public void OpenFileExplor()
{
path = EditorUtility.OpenFilePanel("show all images(.png)","","png");
StartCoroutine(GetTexture()); ;
}
IEnumerator GetTexture()
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture("file:///"+path);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else {
Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
rawImage.texture = myTexture;
}
}
}
|