Skip to content

Instantly share code, notes, and snippets.

@naikrovek
Created Apr 24, 2018
Embed
What would you like to do?
public static void ImportFile(string file) {
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(file);
GameObject part = OlImportFile(file);
if (null == part) {
return;
}
part.name = filenameWithoutExtension;
Mesh m = part.GetComponent<MeshFilter>().sharedMesh;
MeshRenderer mr = part.GetComponent<MeshRenderer>();
// save the folder and make it visible.
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Material[] meshMaterials = mr.sharedMaterials;
for (int i = 0; i < meshMaterials.Length; i++) {
string materialName = ((int)(meshMaterials[i].color.r * 255f)).ToString("X2")
+ ((int)(meshMaterials[i].color.g * 255f)).ToString("X2")
+ ((int)(meshMaterials[i].color.b * 255f)).ToString("X2")
+ ((int)(meshMaterials[i].color.a * 255f)).ToString("X2");
Debug.Log("Looking for Material: " + "Assets/Imported/Materials/" + materialName);
var existingMaterial = (Material)AssetDatabase.LoadAssetAtPath("Assets/Imported/Materials/" + materialName, typeof(Material));
if (existingMaterial != null) {
Debug.Log("Found a matching material.");
meshMaterials[i] = existingMaterial;
} else {
meshMaterials[i].enableInstancing = true;
AssetDatabase.CreateAsset(meshMaterials[i], "Assets/Imported/Materials/" + materialName + ".mat");
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
AssetDatabase.CreateAsset(m, "Assets/Imported/Models/" + part.name + "_M" + ".asset");
var prefab = PrefabUtility.CreateEmptyPrefab("Assets/Imported/Prefabs" + "/" + part.name + ".prefab");
PrefabUtility.ReplacePrefab(part, prefab);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment