Search Unity

Convert a Texture2D to Material

Discussion in 'Scripting' started by BatmanDeveloper, Jul 23, 2017.

Thread Status:
Not open for further replies.
  1. BatmanDeveloper

    BatmanDeveloper

    Joined:
    Jul 1, 2017
    Posts:
    54
    Hi

    How could I convert a Texture2D to a Material?

    Code (CSharp):
    1.  
    2. Material myMaterial = www.texture as Material;
    3. button.GetComponent<Image> ().material = myMaterial;
    4.  
    Thank You
     
  2. BatmanDeveloper

    BatmanDeveloper

    Joined:
    Jul 1, 2017
    Posts:
    54
    Please, somebody could help me. Please.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Materials and textures are different things.

    Materials are instances of shaders.

    Textures are imported bitmaps with certain import conditions applied.

    Materials MAY have slots that can accept texture references.

    Some materials MAY accept more than one texture (i.e., diffuse, normal, specular, etc. textures).
     
    Bunny83 likes this.
  4. BatmanDeveloper

    BatmanDeveloper

    Joined:
    Jul 1, 2017
    Posts:
    54
    ok, so can I apply a texture 2D on my button ? What is the best way to do it? Please
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    There's a lot of great information on various ways of doing things here: Learn
     
  6. Kasper_OmsK

    Kasper_OmsK

    Joined:
    Jul 9, 2015
    Posts:
    47
    I like how a lot of your threads show how you seem to think casting is the solution to everything.
    Code (CSharp):
    1. string myGameString "I would love to have a game where I can press WASD to move and click the mouse to shoot zombies. Yeah, I would love to have zombies. Zombie and dragon.  Please unity"
    2.  
    3. MonoBehaviour myScript = myGameString as MonoBehaviour;
    How great would it be if it was that easy.

    More seriously thought, to change the texture of a button, you don't have to access material properties.
    You have to change the source image of the image component attached to the button.

    Code (CSharp):
    1. #include UnityEngine;
    2. #include UnityEngine.UI;
    3.  
    4. void Start(){
    5. myButton.GetComponent<Image>().sprite = myOtherSprite;
    6. }
     
  7. TORIVILLIAN

    TORIVILLIAN

    Joined:
    Nov 11, 2023
    Posts:
    1
    Here is a simple way of how you can convert a texture to a material;
    • Import your desired texture to you unity project
    • Create a new material
    • In the new plain material you just created, there will be a feature called 'Albedo' in the inspector panel of the new material (located at the beginning).
    • Select the circle at the right side of the 'Albedo' writing
    • Select the texture you imported
    •Save
     
    Last edited: Nov 30, 2023
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Please don't necro threads.
     
Thread Status:
Not open for further replies.