美文网首页Unity3Dunity3D技术分享
Unity3d Editor之代码管理资源

Unity3d Editor之代码管理资源

作者: 土豆写书 | 来源:发表于2018-11-21 18:43 被阅读12次

需求分析

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

  • 案例1:初始化资源(更改属性)
      模型美术上传了一批角色的动作文件,经过检查发现很多动作做都有问题,有些导入项目时,并没有使用快捷键ctrl+D提取其中的Animction文件;有些动作应该是循环的,但是美术并未勾选循环。

  • 案例2:检查资源
      新的一批模型文件再次袭来,这一次都是角色模型,项目中所有人物模型,都需要一个挂点手部武器挂点,最后测试部门对所有人物模型都运行一次后发现,因为缺少这个挂点,部分模型出现错误。

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

  • 图片导入回调
      注意脚本需要放到Editor文件夹下,才能正确被回调。
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);
    }
}
  • 图片导入后自动修改图片格式
      注意,MipMap是用于在远离摄像机时降低图片质量的功能,因此对于UI上的贴图都可以关闭,以减少内存占用。
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

相关文章

网友评论

    本文标题:Unity3d Editor之代码管理资源

    本文链接:https://www.haomeiwen.com/subject/onpcqqtx.html