简易MonoScript序列化数据字段分析器

本文介绍了一个用于分析Unity MonoBehaviour上序列化数据字段的工具,特别是在进行Asset Bundle打包和优化资源加载速度时,该工具能帮助检查和处理序列化字段不匹配的问题。
摘要由CSDN通过智能技术生成

本文分享一个用于分析MonoBehaviour上序列化数据字段信息的工具

在使用Unity开发中,我们有时需要分析挂在Prefab上脚本的序列化数据格式,比如:

  • 做增量式的Asset Bundle打包时,就需要判断序列化数据是否与前一个版本相同,如果Bundle和脚本上的序列化字段不一致,则加载资源时Unity就会报出一个序列化字段不匹配的错误,导致加载失败

    The AssetBundle could not be loaded because it references scripts that are not compatible with the currently loaded ones. Rebuild the AssetBundle to fix this error.
    WWW download had an error:The AssetBundle could not be loaded because it references scripts that are not compatible with the currently loaded ones. Rebuild the AssetBundle to fix this error.

    AssetBundle loading failed because the MyMonoScript class serialization hash does not match. Supported: 2a66b79b, loading: 5ab6257e AssetBundleloading failed because the MyMonoScript class serialization hash does not match. Supported: e262cf75, loading: 92d7bacd

  • 如果你嫌弃Unity的资源加载速度,你可以由此实现一个Prefab上脚本序列化代码生成器

例如在NGUI中,一个UISprite上序列化数据如下:

这里写图片描述

在Unity中,有如下字段会被序列化:

  • 所有没有标记有NonSerialized属性的public字段
  • 所有标记有SerializeField属性的字段
  • 支持的类型除了所有基础类型之外,还有数组,List<>,和标记有Serializable的class和struct

下面是主要的分析代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
using CSharpExtensions;

namespace CSharpExtensions {
    public static class TypeExtensions {
        public static string ToGenericTypeString( this Type t ) {
            if ( !t.IsGenericType )
                return t.Name;
            string genericTypeName = t.GetGenericTypeDefinition().Name;
            genericTypeName = genericTypeName.Substring( 0,
                genericTypeName.IndexOf( '`' ) );
            string genericArgs = string.Join( ",",
                t.GetGenericArguments()
                    .Select( ta => ToGenericTypeString( ta ) ).ToArray() );
            return genericTypeName + "<" + genericArgs + ">";
        }
    }
}

public static class MonoScriptDumper {

    static Type[] BaseTypes = {
            typeof( 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值