Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zmigmbh · Oct 04, 2021 at 02:50 PM · textureruntimewindowsmacosx

Load texture on runtime on Windows and Mac

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zmigmbh · Oct 05, 2021 at 12:41 PM

If anyone is interested in the solution: I used https://github.com/gkngkc/UnityStandaloneFileBrowser and adapted the script like that:

 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);
     }
 }

Simply attach a gameobject to the script or define one with GameObject.Find and the texture will change on runtime with a picture selected anywhere from your computer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Primitive Cube Vertices Different 1 Answer

creating an instance of a texture at runtime 1 Answer

Runtime loading normal texture 1 Answer

materials and textures go missing on my machine. 0 Answers

Changing terrain texture at runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges