Unity之AssetPostprocessor学习一

来源:互联网 发布:南阳炳盛网络 编辑:程序博客网 时间:2024/05/16 06:16
using UnityEngine;using System.Collections;using UnityEditor;/// <summary>/// 在从外部导入texture、或者在导入之后对texture的自动处理/// </summary>public class ImportResDemo : AssetPostprocessor {    /// <summary>    /// 在导入纹理之后调用    /// </summary>    /// <param name="texture"></param>    public void OnPostprocessTexture(Texture2D texture)    {        TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;        if (textureImporter != null)        {            string AtlasName = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(assetPath)).Name;            Debug.Log("name is: " + AtlasName);            Debug.Log("textureType is: " + textureImporter.textureType);            Debug.Log("maxTextureSize is: " + textureImporter.maxTextureSize);            Debug.Log("textureFormat is: " + textureImporter.textureFormat);            textureImporter.textureType = TextureImporterType.Sprite;            textureImporter.spriteImportMode = SpriteImportMode.Single;            textureImporter.spritePackingTag = AtlasName;            textureImporter.mipmapEnabled = false;        }    }    /// <summary>    /// 在导入纹理之前调用    /// </summary>    public void OnPreprocessTexture()    {        TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;        if (textureImporter != null)        {            textureImporter.textureType = TextureImporterType.Advanced;            textureImporter.textureFormat = TextureImporterFormat.ARGB32;        }    }    /// <summary>    /// 移动、删除资源    /// </summary>    /// <param name="importedAssets"></param>    /// <param name="deletedAssets"></param>    /// <param name="movedAssets"></param>    /// <param name="movedFromAssetPaths"></param>    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)    {        //当移动资源的时候  也就是重新导入资源        for (int i = 0; i < movedAssets.Length; i++)        {            Debug.Log("Reimported Asset: " + movedAssets[i]);        }        //删除资源        for (int i = 0; i < deletedAssets.Length;i++ )        {            Debug.Log("Deleted Asset: " + deletedAssets[i]);        }        //移动资源        for (var i=0;i < movedAssets.Length;i++) {            Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);        }    }}

0 0
原创粉丝点击