Search Unity

change texture on primitive cube from script

Discussion in 'Scripting' started by dansav, Jul 19, 2009.

  1. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    Could someone tell me how to take a photo from my hard drive and using a script apply it to a cube.

    First problem I ran into was creating a cube and applying a texture to it. I want to put a photo on this cube. I can create the cube fine and it falls to the ground, but with no texture.

    Either it can't find it or mainTexture does not set it, or something more fundamental that I don't understand. Here is the code.

    cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = Vector3(0,15,0);
    //cube.renderer.material.mainTexture = "lerpzUV"
    //cube.renderer.material.mainTexture = Resources.Load("glass");
    //cube.renderer.material.mainTexture="mypicture.jpg"
    cube.AddComponent(Rigidbody);


    Thanks for any help on this.

    Dan
    Porting Science Education Software from Blink3d to Unity3D finally.
     
  2. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    I had to load the photo into the resources folder for it to find it.

    This line worked
    cube.renderer.material.mainTexture="mypicture.jpg"

    I'm still confused as to whether a picture is automatically a texture and the relationship between shader texture and material.
     
  3. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    240
    Hey there,

    Let me try to clear up the deal with Shaders, Materials, and Textures for you.

    In order for an object to be rendered onto the screen, a Renderer needs to be attached to it. This Renderer needs a Material, which defines how the object is going to be drawn. A Material has a bunch of parameters such as Main Color, Texture(s), Specular values etc. that it passes to the Shader, and the Shader is processed on the Graphics Card along with those parameters, resulting in the image you see on the screen.

    This is a huge simplification and generalization but I think it gets the idea across.

    So, the best way to assign a texture to an object from script is:
    -Load an image into memory as a Texture2D with the following code.
    Code (csharp):
    1. Texture2D tex = Resources.Load("mypicture") as Texture2D;
    -Assign the Texture2D to the Renderer of the object, very similar to how you did.
    Code (csharp):
    1. cube.renderer.material.mainTexture = tex;
    Let me know if you have any questions.
     
  4. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    I'm trying to apply a picture .jpg onto a plane.

    How do I do this?

    The picture cayo.jpg is in the resources folder.

    var oPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    oPlane.transform.localScale = Vector3(15,15,15);
    var tex: Texture = Resources.Load("cayo");
    oPlane.renderer.material.mainTexture =tex;
    //oPlane.renderer.material.mainTexture="cayo"

    I've tried a lot of things like adding cayo.jpg or cayo and all sorts of stuff but it still doesn't work. What am I doing wrong.

    I want to be able to change textures on objects basically from the script.

    I'm confused about where textures should live and how they are accessed?

    Maybe I have to instantiate it and tell it what it is.

    Also even though cayo.jpg is in the resources folder it doesn't show up in the menu wizard thing???

    Really more confused now.

    Thanks,

    Dan
     
  5. smkamran

    smkamran

    Joined:
    Jun 21, 2010
    Posts:
    23
    Hi,

    I m using a remote image as texture.Below is the code

    var url = "http://www.smarty.com/iphone2/red.jpg";

    var myImage : WWW = new WWW (url);
    yield myImage;
    renderer.material.mainTexture = myImage.texture;

    I want to add lightmap because image is not display
    clearly.
    Can anyone tell me how can I add lightmap to it.
     
    vichi1992 likes this.
  6. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    84
    Attach this script to the cube

    Code (CSharp):
    1.  
    2.  
    3. public class ChangeCubeTexture : MonoBehaviour
    4. {
    5.    
    6.    public Sprite imageSprite ; // assign this variable from Inspector with an image ( set to sprite )
    7.  
    8.     void Start()
    9.     {
    10.        this.gameObject.GetComponent<Renderer>().material.mainTexture =  imageSprite.texture  ;
    11.  
    12.      }
    13.  
    14.