Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to serialize ComponentTypes?

Discussion in 'Entity Component System' started by xVergilx, Feb 22, 2021.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Want to do some editor tooling, and that requires serializing ComponentTypes.
    Figured out I could just use TypeManager to convert ComponentTypes into ints to serialize them in editor and use in runtime. Will they match in build?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    And the answer is no, they do not. Which is logical, because editor types are also included.
    Anyways, if anyone has a suggestion how to serialize ComponentTypes, please let me know.
     
  3. Roycon

    Roycon

    Joined:
    Jul 10, 2012
    Posts:
    50
    Serialize the type instead and then at runtime (or conversion) get the Compoenent Type from it?
    Thats what im doing at least at it seems to work fine
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    You mean serialize as string -> get type in runtime? That seems a bit slow.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    So from TypeManager you can get TypeInfo, which has a field called StableTypeHash which is a ulong. You can then call GetTypeIndexFromStableTypeHash().
     
    SubPixelPerfect, Sarkahn and xVergilx like this.
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Exactly what I was looking for, thanks.

    Also, worth mentioning for future readers:
    - TypeManager.GetTypeIndexFromStableTypeHash can be used to fetch TypeIndex for ComponentType in runtime;
    - ComponentType.ReadWrite(int typeIndex) - (which is also identical to
    ComponentType.FromTypeIndex) - for fetching ComponentType with Read + Write access.
    - ComponentType.ReadOnly(int typeIndex) - for fetching types with read only access.
     
  7. Roycon

    Roycon

    Joined:
    Jul 10, 2012
    Posts:
    50
    Yup thats exaclty what im doing, except its at conversion time instead of runtime (so the slowness dosn't matter)
    But now I've seen this StableTypeHash I'll probably update and use that
     
  8. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Can you check if a component exists with EntityManager and StableTypeHash? I have a future issue that will require having a buffer of these to check for components.