Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Assign multiple Textures to mesh renderer with multiple materials

Discussion in 'Scripting' started by malak, Sep 19, 2018.

  1. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    Hello guys ,

    Today i'm getting problem with materials and textures in runtime .

    I want change all textures from all gameobject in live scene .

    I have already a working script to do this but only if the gameobject have a single material .
    if it have 2 ou 3 material this script change only the first one.

    Can you please help me to improve my script ?

    here is the script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeTex : MonoBehaviour {
    6.  
    7.     private GameObject lui;
    8.     public string luiname;
    9.     public Texture texture;
    10.     private string genString;
    11.  
    12.      void Start()
    13.      {
    14.          luiname = gameObject.name.ToString();
    15.          Replace();
    16.    
    17.      }
    18.  
    19.     void Update ()
    20.     {
    21.        if(Input.GetKey ("1") )
    22.        {
    23.         //Debug.Log("Get material name..." + gameObject.GetComponent<Renderer>().material.name);
    24.    
    25.         gameObject.GetComponent<Renderer>().material.mainTexture = texture;
    26.    
    27.         }
    28.     }
    29.     void Replace(){
    30.         genString = "/";
    31.         luiname = luiname.Replace(genString, "_");
    32.         texture  = Resources.Load("Textures/" + luiname) as Texture;
    33.     }
    34.  
    35. }
    and this script edit the texture file :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeTex : MonoBehaviour {
    6.  
    7.     private GameObject lui;
    8.     public string luiname;
    9.     public Texture texture;
    10.     private string genString;
    11.  
    12.      void Start()
    13.      {
    14.          luiname = gameObject.name.ToString();
    15.          Replace();
    16.      
    17.      }
    18.  
    19.     void Update ()
    20.     {
    21.        if(Input.GetKey ("1") )
    22.        {
    23.         //Debug.Log("Get material name..." + gameObject.GetComponent<Renderer>().material.name);
    24.    
    25.         gameObject.GetComponent<Renderer>().material.mainTexture = texture;
    26.    
    27.         }
    28.     }
    29.     void Replace(){
    30.         genString = "/";
    31.         luiname = luiname.Replace(genString, "_");
    32.         texture  = Resources.Load("Textures/" + luiname) as Texture;
    33.     }
    34.  
    35. }
    i'm pretty sure i can merge both script to one but my skill is not enought :(

    i think i need to collect :
    all Materials in array,
    all Materials name in another array
    change the character "/" in material name array
    and finish by change the material texture .


    Thanks by advance guys for your help and time ;)
     
    Last edited: Sep 19, 2018
  2. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    OK i got it :)

    I find a simple and good way to do that in basic line of code
    here is the script if that can help someone in future :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GetRend : MonoBehaviour {
    6.  
    7.  
    8.    
    9.     public Material[] renderers;
    10.     public string[] matname;
    11.     public string luiname;
    12.     private string genString;
    13.     public Texture texture;
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         Invoke("GetRenderers", 5);
    18.        
    19.     }
    20.     void Update(){
    21.         renderers = (Material[])Object.FindObjectsOfType(typeof(Material));
    22.        
    23.     }
    24.     void GetRenderers() {
    25.         foreach (Material objectRenderer in renderers) {
    26.             genString = "/";
    27.             luiname = objectRenderer.name.Replace(genString, "_");
    28.             texture  = Resources.Load("Textures/" + luiname) as Texture;
    29.             objectRenderer.mainTexture = texture;
    30.         }
    31.     }
    32. }
    hope this help ;)
     
    Genstandsled likes this.