c# - 在 Windows 和 Mac 上运行时加载纹理
问题描述
我目前正在使用 NativeGallery 资产更改 iOS 和 Android 运行时的一些纹理。该资产基本上会打开文件资源管理器,让您从手机图库中选择一个图像文件并将其加载到应用程序中。
因此使用的代码是:
public class DisplayHandler : MonoBehaviour
{
public GameObject Display;
public void PickImage(int maxSize)
{
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create Texture from selected image
Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
if (texture == null)
{
Debug.Log("Couldn't load texture from " + path);
return;
}
Material material = Display.GetComponent<Renderer>().material;
if (!material.shader.isSupported) // happens when Standard shader is not included in the build
material.shader = Shader.Find("Legacy Shaders/Diffuse");
material.mainTexture = texture;
// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
//Destroy(texture, 5f);
}
}); // , "Wählen Sie ein Bild aus", mime: "image/*" );
Debug.Log("Permission result: " + permission);
}
}
是否有可能在 Windows 和 Mac 上获得相同的行为?例如,单击按钮会打开资源管理器/查找器窗口,您可以在其中选择图像文件。HTML 等价物是
#if UNITY_EDITOR_WIN
public void ShowExplorer(string itemPath)
{
itemPath = itemPath.Replace(@"/", @"\"); // explorer doesn't like front slashes
System.Diagnostics.Process.Start("explorer.exe", "/select," + itemPath);
}
#endif
#if UNITY_EDITOR_OSX
public void ShowExplorer(string itemPath) {
var path = Path.Combine(Application.dataPath, "Resources");
var file = Directory.EnumerateFiles(path).FirstOrDefault();
if (!string.IsNullOrEmpty(file))
EditorUtility.RevealInFinder(Path.Combine(path, file));
else
EditorUtility.RevealInFinder(path);
}
#endif
它在 Windows 上打开资源管理器窗口,在 Mac 上打开 Finder,但作为一个单独的窗口,而不是作为从中选择纹理的对话框。
解决方案
如果有人对解决方案感兴趣:我使用了https://github.com/gkngkc/UnityStandaloneFileBrowser并像这样调整了脚本:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using SFB;
[RequireComponent(typeof(Button))]
public class CanvasSampleOpenFileImage : MonoBehaviour, IPointerDownHandler
{
public GameObject output;
#if UNITY_WEBGL && !UNITY_EDITOR
//
// WebGL
//
[DllImport("__Internal")]
private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
public void OnPointerDown(PointerEventData eventData) {
UploadFile(gameObject.name, "OnFileUpload", ".png, .jpg", false);
}
// Called from browser
public void OnFileUpload(string url) {
StartCoroutine(OutputRoutine(url));
}
#else
//
// Standalone platforms & editor
//
public void OnPointerDown(PointerEventData eventData) { }
void Start()
{
var button = GetComponent<Button>();
button.onClick.AddListener(OnClick);
}
private void OnClick()
{
var extensions = new[] {
new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )
};
var paths = StandaloneFileBrowser.OpenFilePanel("Title", "", extensions, false);
if (paths.Length > 0)
{
StartCoroutine(OutputRoutine(new System.Uri(paths[0]).AbsoluteUri));
}
}
#endif
private IEnumerator OutputRoutine(string url)
{
var loader = new WWW(url);
yield return loader;
output.GetComponent<Renderer>().material.mainTexture = loader.texture;
output.GetComponent<Renderer>().material.mainTextureScale = new Vector2(-1, -1);
}
}
只需将游戏对象附加到脚本或使用 GameObject.Find 定义一个游戏对象,纹理将在运行时更改,并从您计算机的任何位置选择一张图片。
推荐阅读
- java - 使用 adjustResize 防止键盘在启动时自动弹出
- php - 已解决找不到驱动程序 Debian SQL Server PHP
- javascript - 加载 Babel 的最佳方式是什么?
- html - 中文单词在探险者中变得一团糟
- kubernetes - 错误 503 后端获取失败 Guru Meditation: XID: 45654 Varnish 缓存服务器
- c# - 在 C# 中发送邮件失败
- python - Python 装饰器 - 为什么?
- amazon-web-services - DynamoDB 只能插入 Key
- vb.net - 如果已经存在则覆盖 My.Computer.Network.DownloadFile (vb.net)
- scala - 将重置索引添加到火花数据框