How to set image on Texture in Unity 3d
Asked Answered
B

3

10

I am new to Unity 3D and I want to do just little task: to set image on a Texture instance in Unity 3D. I don't know how to do this at runtime, and I also would like to know how I set its transparency low.

I don't need Texture2D - I just need Texture. My image is in .png format. I also want set an image from my documents directory on to this texture.

Balbinder answered 22/11, 2012 at 13:0 Comment(0)
L
17
  • First import your image into your project by simply dropping it in your project window.

  • Select the image once it's in the project window and make sure that it is set to a Texture Type of Texture in your inspector.

  • Next, create a new material by right clicking in your project window.

  • Next you want to assign your image to this material and you can go about doing this by dragging and dropping your image (which is in the project window) on to your newly created material. In recent versions of Unity, you will need to drop it on the square to the left of "Albedo".

  • Then click on the new material and in your inspector window it should show you that your image is the active texture and the shader should be set to diffuse by default.

  • To activate transparency you'll want to change the shader type by clicking on the shader drop down menu in the inspector window and selecting Transparent/Diffuse (or any of the transparency options depending on what look you're going for).

  • After this to change it's transparency, simply click on the main color swatch and there should be a new window that opens giving you all kinds of modifiers (with 4 horizontal sliders to adjust Red, Green, Blue & Alpha).

  • Adjust the Alpha slider to affect the transparency of your material.

Now, whenever you need to make a call to your material at runtime (e.g if you wanted to change the texture applied to a gameobject), simply do so by using:

renderer.material

This will affect the material off the gameObject that the script is attached to. So for example, if you wanted to change the texture at runtime from script you could say:

// Assign the texture exposed in the inspector the renderer's material

var texture : Texture;
renderer.material.mainTexture = texture;

And if you wanted to change the alpha channel:

renderer.material.color.a = 0 // For example

Hope this helps. Let me know if anything needs clarifying.

Lali answered 22/11, 2012 at 20:6 Comment(6)
Thanks for detail answering but if i want to change image at run time on texture, so how can i do this?Balbinder
You need to change the material. renderer.material = newMaterial. For example you can set var newMaterial : Material as a public variable and than choose the Material in the Inspector which he should use.Amr
Step 3 Next, create a new material by right clicking in your project window. really needs a better description on how to do this.Bindweed
Dragging an image on a material doesn't work. I get the "circle with line through it" icon. What am I doing wrong?Energetics
Note that these instructions are quite out of date, and no longer apply in 2019.4.Importing textures and Creating and using materials covers this. You need to set the Shader to Unlit/textureStooge
This answer could use some updating, incorporating the below answer by TiagoMidriff
A
4

Once you have the image in your Assets

  1. Create a new Material.
  2. Change the shader of the Material to "Unlit/Texture". You'll get the following

New Material ready for texture

  1. Drag the image to where it says "None (Texture)" or click the select button and select the image. Then you'll get the texture

Final texture

Aphanite answered 6/8, 2020 at 12:16 Comment(0)
K
1

At first you to need to import an image to your asset folder.

Use below given code if you want to set image on a Texture runtime using code

[SerializeField] private Texture _texture;
[SerializeField] private GameObject _gameObject;


void Start()
{
    Material m = GetComponent<MeshRenderer>().material;
    m.color = new Color(1,1,1,.5f);
    m.mainTexture = _texture;
}    

First of all assign a Gameobject that you want to change texture. Then assign an image in _texture.

then get a material from that object and assign a _texture to that material.mainTexture.

Kee answered 30/5, 2021 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.