Unity资源导入加强工具

开发目的:unity里模型材质贴图处理起来很麻烦,当模型数量很多时会浪费很多时间。(目前支持2018.3以上版本)

目标:导入模型后自动创建材质,自动赋予贴图,修改模型和贴图导入设置属性

本文参考【技术美术小技巧】 美术资源的批量管理_哔哩哔哩_bilibili 

up主工具代码地址https://github.com/JiepengTan/UnityAssetToolTutorial

up主的方法个人感觉使用起来较麻烦,而且有很多局限性: 

1、仅支持一个模型一个材质,模型要合并在一起,不能出现子物体

2、修改物体大小时缩放了物体100倍,而不是设置导入的单位。

参考其内容,简化使用,使用AssetPostprocessor在资源导入处理资源,导入模型后会自动创建材质,仅需要配置材质路径和贴图路径就可以自动赋予材质贴图。

1、添加导入模型后,自动创建材质,设置尺寸

public class EditorFbxImportSetting : AssetPostprocessor
{
    void OnPreprocessModel()
    {
        ModelImporter importer = assetImporter as ModelImporter;
        if (importer == null)
        {
            return;
        }
        string name = importer.assetPath.ToLower();
        string extension = name.Substring(name.LastIndexOf(".")).ToLower();
        switch (extension)
        {
            case ".3ds":
            case ".fbx":
#if UNITY_2019_1_OR_NEWER
                importer.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
                importer.optimizeMeshPolygons = true;
#endif
                importer.importCameras = false;
                importer.importLights = false;
                importer.generateSecondaryUV = false;
                importer.swapUVChannels = false;
                importer.materialLocation = ModelImporterMaterialLocation.External;
                importer.materialSearch = ModelImporterMaterialSearch.RecursiveUp;
                importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
                importer.useFileUnits = false;
                break;

            default:
                break;
        }
    }
}

 

 

2、添加贴图手动导入后,法线贴图自动设置为bumpMap贴图类型

public class EditorTextureImprotSetting : AssetPostprocessor
{
    public void OnPreprocessTexture()
    {
        TextureImporter impor = this.assetImporter as TextureImporter;
        impor.textureCompression = TextureImporterCompression.Compressed;
        impor.maxTextureSize = 1024;
        impor.textureType = TextureImporterType.Default;
        impor.mipmapEnabled = false;
        string fileName = Path.GetFileNameWithoutExtension(this.assetPath);
        if (fileName.Contains("_Normal"))
        {
            impor.textureType = TextureImporterType.NormalMap;
        }
    }
}

3、配置脚本

[CreateAssetMenu(menuName = "Configs/FBXAssetConfig")]
public class FBXAssetConfig : ScriptableObject
{
    [Header("贴图命名:材质名+贴图类型/模型名+材质名+贴图类型")]
    [Label("材质路径")]
    public string MatPath="Art/Meshs/Materials/";
    [Header("贴图后缀:_BaseColor/_Metallic/_Roughness/_Normal")]
    [Label("贴图路径")]
    public string texturePath = "Art/Textures/";
    public ShaderType shaderType = ShaderType.Autodesk;
    [Header("顺序不建议修改")]
    public List<string> shaderPro = new List<string>() { "_MainTex", "_MetallicGlossMap", "_SpecGlossMap", "_BumpMap" };
    [ShowIf("ShowCustomShaderStr")]
    public string customShader;

    public bool ShowCustomShaderStr()
    {
        if (shaderType==ShaderType.None)
        {
            return true;
        }
        return false;
    }

    [Button()]
    public void SetMaterialTexture()
    {
        string p = Application.dataPath+"/" + MatPath;
        PathUtil.EnsureDirectoryExist(p);
        PathUtil.Walk(p,"*.mat", (s) => {

            Debug.Log(s);
            string fileName = System.IO.Path.GetFileName(s);
            Material mat = AssetDatabase.LoadAssetAtPath<Material>("Assets/"+MatPath+fileName);
            SetMat(mat,mat.name);
            EditorUtility.SetDirty(mat);
        });

        // 保存所有修改
        AssetDatabase.SaveAssets();
    }

    private Shader GetMaterialShader()
    {
        Shader shader = Shader.Find("Autodesk Interactive");
        switch (shaderType)
        {
            case ShaderType.None:
                shader = Shader.Find(customShader);
                break;
            case ShaderType.Standard:
                shader = Shader.Find("Standard");
                break;
            default:
                break;
        }
        return shader;
    }
    private void SetMat(Material material,string keys)
    {
        string p = Application.dataPath + "/" + texturePath;
        PathUtil.EnsureDirectoryExist(p);
        material.shader = GetMaterialShader();
        PathUtil.Walk(p, "*.png|*.tga", (s)=> {
            Texture texture = null;
            string fileName =System.IO.Path.GetFileName(s);
            if (fileName.Contains(keys))
            {
                if (fileName.Contains("_BaseColor"))
                {
                    texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/" + texturePath + fileName);
                    material.SetTexture(shaderPro[0], texture);
                }
                else if(fileName.Contains("_Metallic"))
                {
                    texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/" + texturePath + fileName);
                    material.SetTexture(shaderPro[1], texture);
                }
                else if (fileName.Contains("_Roughness"))
                {
                    texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/" + texturePath + fileName);
                    material.SetTexture(shaderPro[2], texture);
                }
                else if (fileName.Contains("_Normal"))
                {
                    texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/" + texturePath + fileName);
                    
                    material.SetTexture(shaderPro[3], texture);
                }
            }
        });
        
    }


    public enum ShaderType
    {
        None,
        Autodesk,
        Standard
    }


}

 

posted @ 2021-12-31 16:00  wsfw  阅读(280)  评论(0编辑  收藏  举报