- Home /
How to replace all materials at once with one material, in a selected gameobject
I have multiple gameobjects / meshes in my scene with two or more materials on it.
The goal is when i select a gameobject with a pointer I want to change all the materials that are on the selected gameobject to one certain material. And when pressing the button again while aming on the same GO it has to set the materials back as they where.
I get a reference to the meshrenderer by using a OnPointerDown event and change all the materials to one material, its partially working, i can select the gameobject and change the material but i cant change it back.
Here is my code;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class DataTransfer : MonoBehaviour, IPointerDownHandler
{
public GameObject selectedObject;
public Material selectMat;
private Material originalMat;
private Material[] mats;
private Material[] sharedMaterials;
public void OnPointerDown(PointerEventData eventData)
{
selectedObject = eventData.pointerCurrentRaycast.gameObject;
mats = selectedObject.GetComponent<Renderer>().materials;
Debug.Log(selectedObject.name + " Has " + mats.Length + " materials in the mesh renderer");
ChangeMaterial();
//HighLiteGo();
}
public void ChangeMaterial()
{
if (sharedMaterials == null)
{
sharedMaterials = (Material[])selectedObject.gameObject.GetComponent<Renderer>().materials.Clone();
for (int i = 0; i < sharedMaterials.Length; i++)
{
sharedMaterials[i] = new Material(selectMat);
}
selectedObject.gameObject.GetComponent<Renderer>().materials = sharedMaterials;
SelectObject(selectedObject);
}
else
{
ClearSelection();
}
}
void SelectObject(GameObject obj)
{
if (selectedObject != null)
{
if (obj == selectedObject)
{
return;
}
ClearSelection();
}
selectedObject = obj;
}
void ClearSelection()
{
if (selectedObject == null)
{
return;
}
else
{
selectedObject.GetComponent<Renderer>().materials = mats;
originalMat = null;
selectedObject = null;
}
}
}
Answer by hrgchris · Oct 29, 2019 at 02:17 PM
Whilst there's a few funky bits in there, I think the key issue is that you store off the 'mats' list every time OnPointerDown down is pressed. So the first time you press it, you store the 'original materials' and replace with your 'select mat'.
The 2nd time you press it you just first 'overwrite' your mats list with the current materials, all of which are 'select mat'. Then you try and restore the materials to your mats list.
I think your logic in general could do with a bit more thought - the fact that you have ChangeMaterial calling SelectObject, which can then call ClearSelection, or ChangeMaterial can call ClearSelection is a bit of a recipe for bugs.
Answer by V-J · Oct 29, 2019 at 02:37 PM
@hrgchris Well that make sense :), i'm not a pro programmer but i understand what you mean.
So how can i store the materials on the selectedgameobject one time and clear the buffer or something like that when i select another gameobject? Do I need to use a if (material.name != selectMat) in the onpointerdown event?
Ill get rid of one ClearSection()
'How do I write the whole thing' is getting into a longer subject than I have time for I'm afraid :) Depends what you're after - I'd get it working absolutely perfectly for 1 object first, then think about how you might expand it to work for others as well. Baby steps!
Answer by reckless_glitch · May 11 at 03:07 PM
@hrgchris Thanks for taking the time to state that you dont have the time to give a usefull answer, so at least you talked... if you got nothing to say, keep it shut, thanks.
you need to copy the shared materials to another array, change that and copy it back:
public Material[] materialOriginals;
public Material[] materialCopies;
public Material myMaterial;
void replaceMaterials()
{
materialOriginals = rendy.sharedMaterials;
materialCopies = rendy.sharedMaterials;
for (int i = 0; i < rendy.sharedMaterials.Length;i++)
{
materialCopies[i] = myMaterial;
}
rendy.sharedMaterials = materialCopies;
}
Your answer
Follow this Question
Related Questions
Having a problem with the second material (on the same renderer) 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers