使 ScriptableObject 派生类型优先使用二进制序列化(不考虑项目的资源序列化模式)。
对于包含大量数据的自定义资源类型,这非常有用。始终使它们存储为二进制可以提高读/写性能,以及在磁盘上生成更紧凑的表示。主要缺点是二进制资源文件不再人工可读,并且无法在版本控制软件中将其合并。
Unity 中的资源序列化对每个文件整体始终使用一致的序列化模式。因此,当资源文件包含多个资源时,强制特定资源使用二进制序列化可能无法始终满足。资源文件的序列化模式由该路径上的主资源控制。因此,在使用 AssetDabase.CreateAsset 和 AssetDatabase.AddObjectToAsset 组合复杂资源时必须谨慎,以确保主资源是设置了此属性的对象。场景文件始终遵循在项目中配置的资源序列化模式,因而对于场景中嵌入的资源,会始终忽略 PreferBinarySerialization。
该属性只能应用于 ScriptableObject 派生类,对于所有其他类型,都会被忽略。
using UnityEngine;
// Custom asset type that prefers binary serialization. // // Create a new asset file by going to "Asset/Create/Custom Data". // If you open this new asset in a text editor, you can see how it // is not affected by changing the project asset serialization mode. // [CreateAssetMenu] [PreferBinarySerialization] public class CustomData : ScriptableObject { public float[] lotsOfFloatData = new[] { 1f, 2f, 3f }; public byte[] lotsOfByteData = new byte[] { 4, 5, 6 }; }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.