Skip to content

Instantly share code, notes, and snippets.

@probablyspoonie
Last active March 23, 2020 11:41
Show Gist options
  • Select an option

  • Save probablyspoonie/44daa0dcb13a4b8fe3789a95f49964be to your computer and use it in GitHub Desktop.

Select an option

Save probablyspoonie/44daa0dcb13a4b8fe3789a95f49964be to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
public class ModelPostProcessor : AssetPostprocessor
{
public void OnPreprocessModel()
{
// Apple settings to all atlases
ModelImporter importer = assetImporter as ModelImporter;
String name = importer.assetPath.ToLower();
if (assetPath.Contains(".blend") || assetPath.Contains(".fbx"))
{
//importer.globalScale = 1.0F;
//importer.importAnimation = false;
//importer.animationType = ModelImporterAnimationType.None;
//importer.importBlendShapes = false;
//importer.generateSecondaryUV = true;
//importer.importNormals = ModelImporterNormals.Calculate;
//importer.normalSmoothingAngle = 30;
importer.importMaterials = false;
//importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
//importer.materialSearch = ModelImporterMaterialSearch.Everywhere;
}
}
public Material OnAssignMaterialModel(Material material, Renderer renderer)
{
//if (AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)))
//return AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));
string matPath = string.Format("Assets/Project/Materials/{0}.mat", material.name);
string drivePath = Path.Combine(Application.dataPath, "Project/Materials/" + material.name + ".mat");
Log("Trying to find " + matPath + " at " + drivePath);
bool assetExists = File.Exists(drivePath);
if (assetExists)
{
// Find if there is a material at the material path
// Turn this off to always regeneration materials
if (AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)))
return AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)) as Material;
// Create a new material asset using the specular shader
// but otherwise the default values from the model
//material.shader = Shader.Find("Unlit/FoxtreeToon");
//AssetDatabase.CreateAsset(material, "Assets/" + material.name + ".mat");
return material;
}
Log("Could not find material!!!", "red");
return AssetDatabase.LoadAssetAtPath("Assets/Project/Materials/Default.mat", typeof(Material)) as Material;
}
void Log(string text, string color = "grey")
{
Debug.Log(string.Format("<color={1}>[Material Processing] {0}</color>", text, color));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment