Search Unity

swap material not per object but globally

Discussion in 'Scripting' started by Pulov, Mar 5, 2015.

  1. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Hi there

    I made this script to swap materials per object. it gets the materials in an array, the originals and the new ones and lets you swap betwen them back and forth as needed. Then thing is that you've to apply it to every single object having these materials and I tryed to swap the materials from teh root so the original material is swapped and the swapped is applyed to all objects using that material, but did not found the way. So. Any ideas?.

    Code (CSharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.     [RequireComponent(typeof(Renderer))]
    5.  
    6. public class ThermalSwap : MonoBehaviour {
    7.  
    8.         private int index; // index for the materials to swap from the editor array
    9.         private int resetIndex; // index renderer swap position
    10.         public Material[] matOriginales; // original materials
    11.         public Material [] matThermal; // materials to use in thermal mode
    12.         private Material [] mat0; //  position in renderer of existing material to swap
    13.         public bool thermalMode;
    14.  
    15.  
    16.         // Use this for initialization
    17.         void Start () {
    18.  
    19.          
    20.             mat0 = renderer.materials ;
    21.          
    22.         }
    23.  
    24.      
    25.  
    26.         void Update () {
    27.  
    28.  
    29.         if (Input.GetKeyDown (KeyCode.T)){
    30.  
    31.             index = 0;
    32.             resetIndex = 0;
    33.  
    34.             thermalMode = !thermalMode;
    35.  
    36.             while (index < matThermal.Length) {
    37.  
    38.             //Debug.Log(i);
    39.             Debug.Log(index);
    40.  
    41.             if(thermalMode == true ){
    42.  
    43.                 mat0[resetIndex] = matThermal[index];
    44.                 renderer.materials = mat0;
    45.             }
    46.  
    47.             else if(thermalMode == false){
    48.  
    49.                 mat0[resetIndex] = matOriginales [index];
    50.                 renderer.materials = mat0;
    51.             }
    52.             index++;
    53.             resetIndex++;
    54.             }      
    55.         }
    56.     }
    57. }
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
     
    Last edited: Mar 5, 2015