I am currently changing some texture on runtime for iOS and Android by using the NativeGallery asset. The asset basically opens the file explorer, let’s you chose an image file from your phones gallery and loads it into the app.
The therefore used code is:
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);
}
}
Is it possible to get the same behaviour for Windows and Mac? So for example a button click opens an explorer/finder window where you can chose an image file. The HTML equivalent would be
#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
which opens the explorer window on Windows and Finder on Mac, but as a separate window, not as a dialog to chose textures from.