Unity3D游戏开发之 模型、纹理、音频等资源导入事件监控

转载注明出处:http://www.manew.com/thread-99169-1-1.html

通过Unity提供的事件通知类UnityEditor.AssetPostprocessor,我们在Unity编辑器中能够监控纹理、音频、模型等其他资源的导入事件。


我们可以通过这些事件的监控可以很清楚的知道操作的事件,从而根据自己的需求定制一些特殊的编辑器功能。

示例代码如下:代码放入Editor文件夹下

public class AssetPostprocessorEvent : UnityEditor.AssetPostprocessor {
    //模型导入之前调用  
    public void OnPreprocessModel() {
        Debug.Log("OnPreprocessModel=" + this.assetPath);
    }
 
    //模型导入之前调用  
    public void OnPostprocessModel(GameObject go) {
        Debug.Log("OnPostprocessModel=" + go.name);
    }
 
    //纹理导入之前调用,针对入到的纹理进行设置  
    public void OnPreprocessTexture() {
        Debug.Log("OnPreProcessTexture=" + this.assetPath);
        TextureImporter impor = this.assetImporter as TextureImporter;
        impor.textureFormat = TextureImporterFormat.ARGB32;
        impor.maxTextureSize = 512;
        impor.textureType = TextureImporterType.Advanced;
        impor.mipmapEnabled = false;
 
    }
 
    //文理导入之后
    public void OnPostprocessTexture(Texture2D tex) {
        Debug.Log("OnPostProcessTexture=" + this.assetPath);
    }
 
    //音频导入之前
    public void OnPreprocessAudio() {
        Debug.Log("OnPreprocessAudio");
         
        AudioImporter audio = this.assetImporter as AudioImporter;
        audio.format = AudioImporterFormat.Compressed;
    }
 
    //音频导入之后
    public void OnPostprocessAudio(AudioClip clip) {
        Debug.Log("OnPostprocessAudio=" + clip.name);
    }
 
    //所有的资源的导入,删除,移动,都会调用此方法,注意,这个方法是static的  
    public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
        Debug.Log("OnPostprocessAllAssets");
        foreach (string str in importedAsset) {
            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);
        }
    }  
}

猜你喜欢

转载自blog.csdn.net/linxinfa/article/details/86518733