- Home /
Keep replaced materials of a Game Object even after ending the Play Mode C#
Hello!
I'm able to change an existing Material (named Test_Material_01-02) from a Game Object and replace it by another material from the Resouces Folder. The Problem is, I want to keep the changed materials even after ending the Play Mode? At the moment all changes are resetting after ending the Play Mode but I would like to change the existing materials of the Object forever and if possible put the new ones in the folder where the first materials existed.
The reason is I want to create Objects with 3ds Max, load them into Unity and replace the materials permanently by running a script once!
Thanks for help!!
using UnityEngine;
using System.Collections;
public class SetMaterialToo_04 : MonoBehaviour
{
void Start() {
Material[] materials = gameObject.GetComponent<Renderer> ().sharedMaterials;
int Arraylength = materials.Length;
Debug.Log (Arraylength);
for (int i = 0; i < Arraylength; i++) {
string matName = materials[i].name;
if (matName == "Test_Material_01") {
int var = i;
Debug.Log (materials [var]);
// Material[] materials = gameObject.GetComponent<Renderer> ().sharedMaterials;
materials[var] = Resources.Load ("NewMaterial/TestMat") as Material;
gameObject.GetComponent<Renderer>().sharedMaterials = materials;
}
else if (matName == "Test_Material_02")
{
int var = i;
Debug.Log (materials[var]);
materials[var] = Resources.Load ("NewMaterial/TestMat2") as Material;
gameObject.GetComponent<Renderer>().sharedMaterials = materials;
}
else
{
Debug.Log ("No Material!!");
}
}
}
}
Sounds like a job for an editor script - a very handy feature of unity is you can do those kinds of operations as if the game were running, but before runtime in the editor. Look into extending the editor.
http://docs.unity3d.com/$$anonymous$$anual/ExtendingTheEditor.html
http://code.tutsplus.com/tutorials/how-to-add-your-own-tools-to-unitys-editor--active-10047
Your answer
Follow this Question
Related Questions
Replacing sharedMaterials by an existing Material C# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Changing material on an object multiple times 0 Answers
Gl.Lines appearing over objects 0 Answers