Level One Magic
"Any sufficiently advanced technology is indistinguishable from magic." - Arthur C. Clarke
ModelImportMaterials.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class ModelImportMaterials : AssetPostprocessor {

    string GetMaterialPath(Material material) 
    {
        string materialPath = "Assets/Materials/" + material.name.Replace(":", "_") + ".mat";
        if (AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)))
            return materialPath;
        return Path.GetDirectoryName(assetPath) + "/Materials/" + material.name.Replace(":", "_") + ".mat";
    }

    Material OnAssignMaterialModel (Material material, Renderer renderer) 
    {
        
        string materialPath = GetMaterialPath(material);
        // Create the materials directory if it doesn't exist
        string basePath = Application.dataPath + "/../" + Path.GetDirectoryName(materialPath);
        if(!Directory.Exists(basePath))
            Directory.CreateDirectory(basePath);
        else
        {
            // Find if there is a material at the material path
            // Turn this off to always regenerate materials
            if (AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)))
                return (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));
        }
        // Create a new material asset using the appropriate shader
        string shaderName = "";
        if(material.name.Contains("_RGBA"))
            shaderName += "Transparent/";
        if(material.name.ToLower().Contains("blinn") || material.name.ToLower().Contains("phong"))
            shaderName += "Specular";
        else
            shaderName += "Diffuse";
        material.shader = Shader.Find(shaderName);
            
        AssetDatabase.CreateAsset(material, materialPath);
        return material;
    }
    
    void OnPostprocessGameObjectWithUserProperties( GameObject go, string[] names, object[] values) {
        if(go.name.EndsWith("_info"))
        {
            string materialPath = Path.GetDirectoryName(assetPath) + "/Materials/" + go.name.Substring(0, go.name.Length - 5) + ".mat";
            if (AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)))
            {
                Material material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));
                for (int i = 0; i < names.Length; i++)
                {
                    if(names[i].StartsWith("unityMaterial"))
                    {
                        if(names[i].StartsWith("unityMaterialRepeat"))
                        {   
                            Vector2 textureScale = material.GetTextureScale("_MainTex");
                            if(names[i] == "unityMaterialRepeatU")
                                textureScale[0] = (float)values[i];
                            else if(names[i] == "unityMaterialRepeatV")
                                textureScale[1] = (float)values[i];
                            material.SetTextureScale("_MainTex", textureScale);
                        }
                        else if(names[i].StartsWith("unityMaterialOffset"))
                        {   
                            Vector2 textureOffset = material.GetTextureOffset("_MainTex");
                            if(names[i] == "unityMaterialOffsetU")
                                textureOffset[0] = (float)values[i];
                            else if(names[i] == "unityMaterialOffsetV")
                                textureOffset[1] = (float)values[i];
                            material.SetTextureOffset("_MainTex", textureOffset);
                        }
                        else if(names[i] == "unityMaterialColorGain")
                        {
                            Vector4 newColor = (Vector4)values[i];
                            newColor[3] = 1.0f;
                            material.SetColor("_Color", (Color)newColor);
                        }
                    }
                }
            }
        }
    }
}

Back to Main

© 2003-2013 Andrew Kelts

Back to Main

© 2003-2013 Andrew Kelts