- Home /
Question by
kevinforrestconnors · Dec 15, 2017 at 08:27 PM ·
scripting problemtexturemesh
Applying texture to all meshparts
I have a script that attempts to apply a texture to an object and all of its meshparts when the object and the texture are imported. It isn't working because the material isn't getting set on the MeshRenderer. What is the matter?
Thanks, Kevin
using UnityEngine;
using UnityEditor;
using System.Collections;
public class OnImport : AssetPostprocessor
{
void OnPostprocessModel(GameObject g)
{
if (g.name.Contains("Scaled")) {
g.transform.Rotate(-90, 0, 180);
}
if (g.name.Contains("landscape"))
{
g.transform.Rotate(-90, 0, 180);
Transform def = g.transform.GetChild(0);
Material landscapeMaterial = new Material(Shader.Find("Standard"));
Texture landscapeTex = (Texture)AssetDatabase.LoadAssetAtPath("Assets/landscape_texture.tiff", typeof(Texture));
landscapeMaterial.mainTexture = landscapeTex;
foreach (Transform meshPart in def) {
Renderer rend = meshPart.GetComponent<Renderer>();
rend.material = landscapeMaterial;
}
}
}
}
Comment
Answer by kevinforrestconnors · Dec 16, 2017 at 07:27 PM
Still stuck on this... I have been washing my head against the wall.
The difference between material and sharedMaterial is also not clear to me.
Your answer
Follow this Question
Related Questions
How to confirm the vertex of a mesh that mouse in and change its texture 0 Answers
rendering hollow mesh ? 0 Answers
Texture layers on single mesh 1 Answer
Distortion of Texture from some Angles 0 Answers
Colour cubes after CombineMesh() 0 Answers