Search Unity

IComponentData not blittable

Discussion in 'Entity Component System' started by xVergilx, Jan 24, 2019.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I'm getting weird error message:
    Code (CSharp):
    1. ArgumentException: Components.EntitySpawnerData is an IComponentData, and thus must be blittable (No managed object is allowed on the struct).
    2. Unity.Entities.TypeManager.BuildComponentType (System.Type type) (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/Types/TypeManager.cs:499)
    This is how component data is defined:
    Code (CSharp):
    1. [Serializable]
    2.     public struct EntitySpawnerData : IComponentData {
    3.         public bool SpawnOnTap;
    4.         public bool SingleEntity;
    5.         public bool HasEntityAttached;
    6.         public float EntitySpawnDelay;
    7.         public float LastSpawnTime;
    8.     }
    What am I missing? Do I need to use StructLayout + Explicit because data has 11 bytes?
     
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    @Kichang-Kim linked my post, here is the solution of that and a PropertyDrawer for the inspector! Have fun ;)

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [Serializable]
    5. public struct boolean {
    6.     [SerializeField] private byte boolValue;
    7.  
    8.     public boolean(bool value) {
    9.         boolValue = (byte)(value ? 1 : 0);
    10.     }
    11.     public static implicit operator bool(boolean value) {
    12.         return value.boolValue == 1;
    13.     }
    14.     public static implicit operator boolean(bool value) {
    15.         return new boolean(value);
    16.     }
    17.  
    18.     public override string ToString() {
    19.         return ((bool)this).ToString();
    20.     }
    21. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomPropertyDrawer(typeof(boolean))]
    7. public class booleanPropertyDrawercs : PropertyDrawer {
    8.  
    9.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    10.         var booleanProp = property.FindPropertyRelative("boolValue");
    11.         booleanProp.boolValue = EditorGUI.Toggle(position, label, booleanProp.boolValue);
    12.     }
    13. }
     
    xVergilx likes this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
  6. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    I saw a sample somewhere from unity about that

    Do it this way:
    [Serializable]
    public struct EntitySpawnerData : IComponentData {
    public Carrier Value;
    }

    [Serializable]
    public struct Carrier
    {
    public bool SpawnOnTap;
    public bool SingleEntity;
    public bool HasEntityAttached;
    public float EntitySpawnDelay;
    public float LastSpawnTime
    }
     
    Last edited: Jan 25, 2019
  7. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    If you can adjust your logics, you could also do:

    public bool3 boolValues;
    public float2 floatValues;
     
    xVergilx likes this.