Hi all,
I wanted to add materials for .obj files. First of all, I turned the obj files into prefabs in order to import them into my scene. Then, I wanted to add the specific materials to it from their respective .mtl file. I used the following code and it loads the prefabs but not the materials to it. Can someone help, please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class loadPrefabs : MonoBehaviour
{
void Start()
{
UnityEngine.Object[ ] prefabObjects = Resources.LoadAll(“Prefabs”, typeof(GameObject));
Material [ ] mat_s = Resources.LoadAll(“Materials”, typeof(Material)) as Material[ ];
foreach (UnityEngine.Object prefabObject in prefabObjects)
{
GameObject prefab = (GameObject)prefabObject;
string prefabName = prefab.name;
Debug.Log(prefab.name);
GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
Renderer renderer = newPrefab.GetComponent();
foreach (var mat in mat_s) {
if (mat.name == name)
renderer.material=mat;
}
}
}
}