AssetPostprocessor Unity资源导入预处理

当你导入资源时,想修改资源的内容或者属性等,你需要继承AssetPostprocessor类,实现相关的方法。
拿** OnPreprocessorAsset(**)方法,举个例子。

using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Text;
using LitJson;

/// <summary>
/// Spine 文件导入 修改Spine.json文件内的版本设置
/// </summary>
public class SpineImportSetting : AssetPostprocessor
{
    
    
    //任何资源(包括文件夹)导入都会被调用的方法
    void OnPreprocessAsset()
    {
    
    
        try
        {
    
    
            if (!this.assetPath.EndsWith(".json"))
                return;

            //先判断是否是 spine 文件
            string msg = File.ReadAllText(this.assetPath, Encoding.UTF8);
            JsonData jsonObj = JsonMapper.ToObject(msg);
            JsonData item = jsonObj["skeleton"]["spine"];

            if (item != null && item.IsString&& item.ToString()!="3.8")
            {
    
    
                jsonObj["skeleton"]["spine"] = "3.8";//美术破解的 3.8.75 导入会报错
                string newjson = JsonMapper.ToJson(jsonObj);
                File.WriteAllText(this.assetPath, newjson);
                AssetDatabase.Refresh();
            }
        }
        catch (Exception e)
        {
    
    
            Debug.LogError($"SpineImportSetting 异常 {e.Message}");
        }
    }
}

其他的预处理方法

void OnPreprocessTexture()
{
    
    
	Debug.Log("在纹理导入之前 获取通知")
}
void OnPostprocessTexture()
{
    
    
	Debug.Log("在纹理导入之后 获取通知")
}

void OnPreprocessModel()
{
    
    
	Debug.Log("在模型导入之前 获取通知")
}
void OnPostprocessModel()
{
    
    
	Debug.Log("在模型导入之后 获取通知")
}

void OnPreprocessAudio()
{
    
    
	Debug.Log("在音频导入之前 获取通知")
}
void OnPostprocessAudio()
{
    
    
	Debug.Log("在音频导入之后 获取通知")
}

void OnPostprocessAllAssets()
{
    
    
	Debug.Log("在一些资源被导入后调用(当资源进度条到达末端)")
}
//所有的资源的导入,删除,移动,都会调用此方法,注意,这个方法是static的
public static void OnPostprocessAllAssets(string[]importedAsset,string[] deletedAssets,string[] movedAssets,stringmovedFromAssetPaths)
{
    
    
	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/baidu_39447417/article/details/115162746
今日推荐