Search Unity

[How to import an image at runtime][Need help!!!]

Discussion in 'Scripting' started by flyric123, May 8, 2011.

  1. flyric123

    flyric123

    Joined:
    May 8, 2011
    Posts:
    13
    Hi there,
    I am new to Unity3D.
    I got a problem now: I have no idea how to open an image and import it at runtime to be a texture of a certain mesh.
    Let me explain the problem in details:
    I want to import an image at runtime to be a texture. And the image is from other location on the disk(not in Unity project folder). I am using the EditorUtility.OpenFilePanel() to get the path right now. But I do not know which function can be used to load the image via the path.
    I have tried functions in Resource class, WWW class and AssetDatabase class.

    Could anybody please tell me how to do this?

    Thanks a lot!
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Use the WWW class with the file:// protocol.

    If you are also looking for a document browser to let the user select a file, I believe there is one somewhere on the wiki.
     
  3. flyric123

    flyric123

    Joined:
    May 8, 2011
    Posts:
    13
    Thank you!
    The problem is solved!
    I did not realize that I should use file:// protocol.
     
  4. ersaurabh101

    ersaurabh101

    Joined:
    Oct 8, 2010
    Posts:
    412
    hiii, if u have resolved it, plz share the sample code, i am too looking for something similar...
     
  5. flyric123

    flyric123

    Joined:
    May 8, 2011
    Posts:
    13
    www = WWW("file://" + File_Name)
    Face.renderer.sharedMaterial.mainTexture = www.texture
     
  6. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Glad you got it working!

    Just an fyi: it's bad practice to use protected keywords with a case shift for variable names. It's just a little thing, but in programming it's always the little things that cause you stare at the screen for hours on end wondering why a piece of code isn't working correctly.
     
  7. ersaurabh101

    ersaurabh101

    Joined:
    Oct 8, 2010
    Posts:
    412
    hiii i am actually a starter, i have a script attached to a plane, n when i compile it n host the webplayer, i see that image on the plane...now please tell what next to do step by step..


    thanks a lot in advance
     
  8. flyric123

    flyric123

    Joined:
    May 8, 2011
    Posts:
    13
    Ye, indeed! I think I should be careful in that.
    And I got another question here if you can do me a favor:
    I wanna choose a file from my disk, and I think function EditorUtility.OpenFilePanel from UnityEditor class is a good choise.
    However, it has a problem when building, which is "Namespace 'UnityEditor' not found, maybe you forgot to add an assembly reference"
    Do you have any good ideas about selecting a file from disk and then return a path?

    Again, thanks a lot!
     
  9. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Not saying your wrong, and I'll keep it in mind, but that is how the unity scripting reference does it.
     
  10. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  11. flyric123

    flyric123

    Joined:
    May 8, 2011
    Posts:
    13
    Ye, I saw this one.
    Just wondering if the Editor classes are available.