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);
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值