docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class TypeHash

    Produces a stable type hash for a Type based on its layout in memory and how that memory should be interpreted.

    Inheritance
    object
    TypeHash
    Namespace: Unity.Entities
    Assembly: solution.dll
    Syntax
    public class TypeHash
    Remarks

    You can rename field members and this hash isn't affected. However, if a field's type changes, this hash changes, because the interpretation of the underlying memory will have contextually changed.

    The purpose of the stable type hash is to provide a version linking serialized component data to its runtime representation.

    As such, the type hash has a few requirements:

    • R0: TypeHashes apply only to types that Unity.Entities serializes. The internals of a UnityEngine.Object reference contained in a component mustn't have an effect on the type hash. You can safely change the internals of the UnityEngine.Object reference because they're serialized outside of Unity.Entities.
    • R1: Types with the same data layout but are different types should have different hashes.
    • R2: If a type's data layout changes, so should the type hash. This includes:
      • A nested field's data layout changes (for example, a new member added)
      • FieldOffsets, explicit size, or pack alignment are changed
      • Different types of the same width swap places (for example if a uint swaps with an int)
      • Note: Unity can't detect if fields of the same type swap (for example, mInt1 swaps with mInt2) This is a semantic difference which you should increase your component [TypeVersion(1)] attribute. You shouldn't try to hash field names to handle this, because you can't control all field names. Also, field names have no effect serialization and doesn't affect hashes.
    • R3: You should version the hash in case the semantics of a type change, but the data layout is unchanged
    • R4: DOTS Runtime relies on hashes generated from the Editor (used in serialized data) and hashes generated during compilation. These hashes must match. This rule exists because of the following:
      • Tiny swapa out assemblies for 'tiny' versions, which means you should avoid any hashing using the AssemblyName or handle it specially for the known swapped assemblies.
        • This means you should avoid Type.AssemblyQualifiedName in hashes, but as well, closed-form generic types include the assembly qualified name for GenericArguments in Type.FullName which causes issues. For example, typeof(ComponentWithGeneric.GenericField).FullName == Unity.Entities.ComponentWithGeneric.GenericField1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]`
      • System.Reflection and Mono.Cecil provide different Type names (they use a different format). Generating hashes from System.Reflection to match hashes using Mono.Cecil must account for this difference.

    Methods

    Name Description
    CalculateMemoryOrdering(Type, out bool, Dictionary<Type, ulong>)

    Calculates a MemoryOrdering for the input type.

    CalculateStableTypeHash(Type, IEnumerable<CustomAttributeData>, Dictionary<Type, ulong>)

    Calculates a stable type hash for the input type.

    CombineFNV1A64(ulong, ulong)

    Combines a FNV1A64 hash with a value.

    FNV1A64(int)

    Generates a FNV1A64 hash.

    FNV1A64(string)

    Generates a FNV1A64 hash.

    FNV1A64<T>(T)

    Generates a FNV1A64 hash.


    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    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.

    In This Article
    • Methods
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)