监听资源导入——AssetPostprocessor

作者:追风剑情 发布于:2017-11-7 15:57 分类:Unity3d

重写AssetPostprocessor可对资源的导入、移动、删除等进行后处理


using UnityEngine;
using UnityEditor;
using System.Collections;

/// <summary>
/// 参考 http://blog.csdn.net/yhy2218/article/details/50847127
/// 此类必须放在Editor目录下
/// </summary>
public class CAssetPostprocessor : AssetPostprocessor
{
    // 模型导入前
    public void OnPreprocessModel()
    {
        Debug.Log("OnPreprocessModel=" + this.assetPath);
        ModelImporter modelImporter = (ModelImporter)assetImporter;
    }

    // 模型导入后
    public void OnPostprocessModel(GameObject go)
    {
        Debug.Log("OnPostprocessModel=" + go.name);
    }

    /// <summary>
    /// 模型导入时
    /// OnPreprocessModel()->OnPreprocessTexture()->OnPostprocessTexture()
    /// ->OnAssignMaterialModel()->OnPostprocessGameObjectWithUserProperties
    /// ->OnPreprocessAnimation->OnPostprocessModel()
    /// </summary>
    /// <param name="material">模型材质</param>
    /// <param name="renderer">模型渲染器</param>
    /// <returns></returns>
    public Material OnAssignMaterialModel(Material material, Renderer renderer)
    {
        Debug.Log(string.Format("OnAssignMaterialModel material.name={0}, renderer.name={1}", material.name, renderer.name));
        //这里可以对模型使用的材质或shader进行重新处理,并返回新的材质。
        //return new_material;
        return null;//如果未对模型材质进行任何处理必须返回null
    }

    /// <summary>
    /// 关联好模型的渲染信息后,会调用这个函数来处理用户信息
    /// 典型的运用是从存储在3dmax/maya的对象中读取"userdata"
    /// </summary>
    /// <param name="go">模型对象</param>
    /// <param name="propNames">属性名</param>
    /// <param name="values">类型可以是string,Vector4,bool,Color,float,int</param>
    void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] propNames, System.Object[] values)
    {
        Debug.Log("OnPostprocessGameObjectWithUserProperties go.name=" + go.name);
        for (int i = 0; i < propNames.Length; i++)
        {
            Debug.Log(string.Format("{0}={1}", propNames[i], values[i]));
            var value = values[i];
            if (value.GetType().ToString() == "System.Int32")
            {

            }
        }
    }

    // 动画导入前
    void OnPreprocessAnimation()
    {
        var modelImporter = assetImporter as ModelImporter;
    }

    // 纹理导入前
    public void OnPreprocessTexture()
    {
        Debug.Log("OnPreProcessTexture=" + this.assetPath);
        TextureImporter textureImporter = (TextureImporter)assetImporter;
    }

    // 纹理导入后
    public void OnPostprocessTexture(Texture2D texture)
    {
        Debug.Log("OnPostProcessTexture=" + this.assetPath);
    }

    // 当Sprites被创建后
    public void OnPostprocessSprites(Texture2D texture , Sprite[] sprites)
    {
        Debug.Log("OnPostprocessSprites sprites.Length=" + sprites.Length);
    }

    // 音频导入前
    public void OnPreprocessAudio() 
    {
        Debug.Log("OnPreprocessAudio");
        AudioImporter audioImporter = (AudioImporter)assetImporter;
        audioImporter.forceToMono = true;//强制到单声道
        audioImporter.format = AudioImporterFormat.Compressed;
    }

    // 音频导入后
    public void OnPostprocessAudio(AudioClip clip)
    {
        Debug.Log("OnPreprocessAudio="+clip.name);
    }

    // 当AssetBundle的名称发生变化时,编辑器会自动执行以下函数,返回变化信息
    public void OnPostprocessAssetbundleNameChanged(string assetPath, string previousAssetBundleName, string newAssetBundleName)
    {
        Debug.Log("OnPostprocessAssetbundleNameChanged");
        Debug.Log("Asset " + assetPath + " has been moved from assetBundle " + previousAssetBundleName + " to assetBundle " + newAssetBundleName);
    }

    // SpeedTree资源导入前
    public void OnPreprocessSpeedTree()
    {
        Debug.Log("OnPreprocessSpeedTree");
    }

    // SpeedTree资源导入后
    public void OnPostprocessSpeedTree(GameObject go)
    {
        Debug.Log("OnPostprocessSpeedTree go.name="+go.name);
    }

    /// <summary>
    /// 所有的资源的导入,删除,移动,都会调用此方法
    /// </summary>
    /// <param name="importedAssets">导入的资源</param>
    /// <param name="deletedAssets">删除的资源</param>
    /// <param name="movedAssets">移动资源到新位置</param>
    /// <param name="movedFromAssetPaths">被移动资源原来的位置</param>
    public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        Debug.Log("OnPostprocessAllAssets");
        foreach (string str in importedAssets)
        {
            Debug.Log("importedAsset = " + str);
        }
        foreach (string str in deletedAssets)
        {
            Debug.Log("deletedAssets = " + str);
        }
        foreach (string str in movedAssets)
        {
            Debug.Log("movedAssets = " + str);
        }
        foreach (string str in movedFromAssetPaths)
        {
            Debug.Log("movedFromAssetPaths = " + str);
        }  
    }
}


1111tex.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号