美文网首页
8.0AssetPostprocessor控制资源导入管线

8.0AssetPostprocessor控制资源导入管线

作者: 莫忘初心_倒霉熊 | 来源:发表于2021-02-18 13:51 被阅读0次

    AssetPostprocessor脚本需要继承自AssetPostprocessor类。如下:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;
    
    public class AssetImportPipeline : AssetPostprocessor
    {
        //导入模型资源之前调用
        private void OnPreprocessModel()
        {
        }
        //导入模型资源之后调用
        private void OnPostprocessModel(GameObject g)
        {
        }
        //导入任意资源之前调用
        private void OnPreprocessAsset()
        {
        }
        //导入任意资源之后调用
        private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
            string[] movedFromAssetPaths)
        {
        }
        //还有animation,audio等资源的ProcessAsset方法,这里不一一列举了,下面以Texture为例
        //导入图片资源之前调用
        private void OnPreprocessTexture()
        {
            if (assetPath.StartsWith("Assets/Art/Bg"))
            {
                PreprocessBg();
            }
        }
        //导入图片资源之后调用
        private void OnPostprocessTexture(Texture2D texture)
        {
            //输出路径
            Debug.Log(assetPath);
        }
        private void PreprocessBg()
        {
            TextureImporter importer = assetImporter as TextureImporter;
            if (importer != null)
            {
                //设置贴图类型为Spite
                importer.textureType = TextureImporterType.Sprite;
                TextureImporterSettings texSettings = new TextureImporterSettings();
                importer.ReadTextureSettings(texSettings);
                //设置精灵的pivot
                texSettings.spriteAlignment = (int)SpriteAlignment.BottomLeft;
                //不开启mipmap
                texSettings.mipmapEnabled = false;
                importer.SetTextureSettings(texSettings);
            }
        }
    }
    

    AssetPostprocessor脚本在使用时,最好做成dll链接库,避免资源导入时,因为其他脚本编译错误,而导致AssetPostprocessor脚本不运行,dll链接库制作请自行百度。

    相关文章

      网友评论

          本文标题:8.0AssetPostprocessor控制资源导入管线

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