Unity3Dunity3D技术分享

Unity3d Editor之代码管理资源

2018-11-21  本文已影响12人  土豆写书

需求分析

  在实际工作过程中,美术资源并不总是按照预想的方式提交的。随着项目的增大,资源管理混乱产生的影响也会成倍地增加。有些团队的做法是给相关负责人检查无误后再提交到项目中。这里我们采用另一种方法,使用代码控制资源的导入!每当资源导入项目后自动修改属性、提取数据、删除多余的资源,这种策略可以节省很多时间。

请把资源初始化属性的工作交给机器,使用命令来初始化资源!
请把检查资源的工作交给机器,使用命令来检查资源,如果资源不符合需求则弹出错误提示!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class AssetPostProcessor_Test1 : AssetPostprocessor {
    void OnPostprocessTexture(Texture2D texture)
    {
        TextureImporter import = assetImporter as TextureImporter;
        Debug.Log(import.assetPath);
    }
}
public class AssetPostProcessor_Test1 : AssetPostprocessor {

    void OnPostprocessTexture(Texture2D texture)
    {
        TextureImporter import = assetImporter as TextureImporter;
        Debug.Log(import.assetPath);

        //Set mipmapEnable = false;
        import.mipmapEnabled = false;
        import.textureType = TextureImporterType.Sprite;
        //Save The Changes
        EditorUtility.SetDirty(import);
    }
}
public class AssetPostProcessor_Test1 : AssetPostprocessor {
    void OnPostprocessModel(GameObject obj)
    {
        ModelImporter import = assetImporter as ModelImporter;
        Debug.Log(import.assetPath);
    }
}
   void OnPostprocessModel(GameObject obj)
    {
        ModelImporter import = assetImporter as ModelImporter;
        Debug.Log(import.assetPath);
        var key = "Models".ToLower();
        var path = import.assetPath.ToLower();
        if(!path.Contains(key))
        {
            EditorApplication.delayCall += HandCopyFBXAnimation;
        }
    }

    /// <summary>
    /// 处理拷贝模型动画
    /// </summary>
    void HandCopyFBXAnimation()
    {
        EditorApplication.delayCall -= HandCopyFBXAnimation;
        ModelImporter import = assetImporter as ModelImporter;
        var asset = AssetDatabase.LoadAllAssetsAtPath(import.assetPath);
        foreach (var clipObje in asset)
        {
            if (clipObje is AnimationClip && !clipObje.name.Contains("__preview__"))
            {
                string outputPath = Path.GetDirectoryName(import.assetPath)
                                        + Path.DirectorySeparatorChar + clipObje.name + ".anim";
                var currentAsset = AssetDatabase.LoadAssetAtPath<AnimationClip>(outputPath);
                //如果存在则覆盖还是追加?
                if (currentAsset != null)
                {
                    EditorUtility.CopySerialized(clipObje, currentAsset);//不影响文件关联的情况下更新文件内容
                    EditorUtility.SetDirty(currentAsset);
                }
                //不存在则创建
                else
                {
                    var newAnim = new AnimationClip();
                    EditorUtility.CopySerialized(clipObje, newAnim);
                    AssetDatabase.CreateAsset(newAnim, outputPath);
                }
            }
        }
        //删除模型
        AssetDatabase.DeleteAsset(import.assetPath);
    }
命令关键词(不区分大小写) 适用资源类型 作用
[UI] 图片 UI图片资源,取消 MipMap属性
[ANM] 模型 拷贝动画文件并且源模型文件
[LOOP] 模型 设置动画循环模式
[SKIP] 所有 跳过所有检查和设置



最后,我们将在下一章节讨论,导入模型自动绑定材质、使用命令检查资源是否完整、如何批量修改资源。

Be continued

上一篇 下一篇

猜你喜欢

热点阅读