Search Unity

[SOLVED] Apply Image to Plane Primitive

Discussion in 'Scripting' started by aps502, Apr 21, 2015.

  1. aps502

    aps502

    Joined:
    Nov 14, 2014
    Posts:
    10
    Hi,

    Id like to add a PNG image to a primitive plane game object using C#
    This is simple in the editor as one can simply drag the image onto the plane, but i would like to create the plane dynamically at run-time and then add an image to it.

    Can anyone help with this?

    string imageString = "C:\\testimage.png");
    Material mat = Resources.Load( imageString ) as Material; // <--- Should it be material or texture2D?
    plane = GameObject.CreatePrimitive(PrimitiveType.Plane);

    Thanks
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,685
    OK, let's take it one step at a time.

    First, Resources.Load takes a Unix-style path within the assets folder, not a Windows-style path to some file on your hard drive. So, which do you need to do? Get something from your project resources, or get something from a file on disk?

    In the former case, move your asset and fix your path, and yes, it should be a Texture2D if what you have in your resources is an image. In the latter case, use standard C# file I/O methods to load the PNG file data, then call Texture2D.LoadImage to convert this into a Texture2D.

    Once you have that, you need to get or create a material. A material needs a shader, which you can locate with Shader.Find. Or, if you're only going to be creating one of these textured planes at a time (or if they all use the same texture), you can just set up a material in your project, and give a reference to that in the script.

    Once you have your material reference, you can assign your Texture2D to its mainTexture property, and then assign this material to your plane.
     
  3. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,110
    Resources.Load expects path relative to any Resources folder under your Assets folder.

    Assuming you have your texture under Resources/Textures folder.
    i.e, Assets/Resources/Textures/testimage.png

    Code (CSharp):
    1. Texture2D  texture = Resources.Load("Textures/testimage") as Texture2D; //No need to specify extension.
    If you don't have texture in Resources folder, you can go for normal File I/O or can download it by using WWW class.

    You need a material to set your created texture.
    Code (CSharp):
    1. Material material = new Material(Shader.Find("Diffuse"));
    Set the texture to this material
    Code (CSharp):
    1. material.mainTexture = texture;
    Now you have a material ready! Just attach it to the renderer, you are done!

    Code (CSharp):
    1. planeGameObject.GetComponent<Renderer>().material = material;
     
  4. aps502

    aps502

    Joined:
    Nov 14, 2014
    Posts:
    10
    Excellent explanation thanks, based on this I managed to get it working with the code below.

    Note the you must create a material in the editor and drag it onto the frontPlane property in the inspector window. Alternatively, you must create a new material at run-time and assign it to frontPlane.

    Code (CSharp):
    1. public Material frontPlane;
    2.  
    3. void Start() {
    4.  
    5. // Create the Plane
    6. GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    7.  
    8. // Load the Image from somewhere on disk
    9. var filePath = "C:/MYDATA/MYIMAGE.png";
    10. if (System.IO.File.Exists(filePath))
    11. {
    12.      // Image file exists - load bytes into texture
    13.      var bytes = System.IO.File.ReadAllBytes(filePath);
    14.      var tex = new Texture2D(1, 1);
    15.      tex.LoadImage(bytes);
    16.      frontPlane.mainTexture = tex;
    17.  
    18.      // Apply to Plane
    19.      MeshRenderer mr = plane.GetComponent<MeshRenderer> ();
    20.      mr.material = frontPlane;
    21. }
    22. }
     
    Last edited: Apr 22, 2015
    Rogerjak likes this.
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,685
    Cool! Thank you for following up with your solution, that provides great closure and a handy reference for future googlers who might stumble upon this thread.

    One request though: use code tags to make your code more readable. A good way to do that is to click the little "insert" button (between the movie film and the floppy disk) in the editor toolbar.
     
  6. aps502

    aps502

    Joined:
    Nov 14, 2014
    Posts:
    10
    No prioblem. Done. Thanks again
     
unityunity