【发布时间】:2021-11-25 13:02:32
【问题描述】:
我目前正在使用 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,但作为一个单独的窗口,而不是作为从中选择纹理的对话框。
【问题讨论】:
-
问题似乎与纹理或文件加载无关。解决问题通常从识别问题开始。这里没有发生这种情况。
-
感谢您的反馈。我添加了更多信息,希望能更好地隔离问题并阐明我已经尝试过的操作。
-
github.com/gkngkc/UnityStandaloneFileBrowser 或 assetstore.unity.com/packages/tools/gui/… ... 搜索“Unity Filebrowser Standalone”时在 Google 中搜索两个条目 ....
-
谢谢!我不知道我必须搜索独立文件浏览器。我在 finder/explorer 中搜索了运行时选择的纹理,但没有发现任何用处。
标签: c# windows macos unity3d textures