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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved SetSharedComponentData not setting data to the Entity

Discussion in 'Entity Component System' started by Zylkowski_a, Jun 5, 2020.

  1. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    I am testing Unity ECS for quite a while while, everything worked fine so far but today when I tested some things I noticed that Shared component is not properly working with SetSharedComponentData.
    That's component code:
    Code (CSharp):
    1. public struct TestComponent : ISharedComponentData, IEquatable<TestComponent>
    2. {
    3.     public Material material;
    4.     public Mesh mesh;
    5.  
    6.     public Texture2D texture;
    7.    
    8.     public int a;
    9.     public int b;
    10.  
    11. //Rest of the code(Equals etc.)
    12. }
    13.    
    I am adding this component with this code
    Code (CSharp):
    1.  
    2. entityManager.SetSharedComponentData(e, new TestComponent()
    3. {
    4.     a = 1,
    5.     b = 12345,
    6.     texture = //Some texture
    7. });
    8.  
    and after Setting component like this, there's no change in the entity debugger but there's no errors. When i delete
    texture = //Some texture
    data is added to the entity and a = 1, b = 12345. Why it happens and how can I fix this
     
  2. agurskis22cans

    agurskis22cans

    Joined:
    Jul 17, 2019
    Posts:
    9
    I'd guess Texture2D doesn't have a conversion from Standard Unity to DOTS Unity.
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Very likely your GetHashCode method is not deterministic
    This is the correct way of writing it. Specifically deletion has to be handled correctly.

    Code (CSharp):
    1.  /// <summary>
    2.         /// Two RenderMesh objects are equal if their respective property values are equal.
    3.         /// </summary>
    4.         /// <param name="other">Another RenderMesh.</param>
    5.         /// <returns>True, if the properties of both RenderMeshes are equal.</returns>
    6.         public bool Equals(RenderMesh other)
    7.         {
    8.             return
    9.                 mesh == other.mesh &&
    10.                 material == other.material &&
    11.                 subMesh == other.subMesh &&
    12.                 layer == other.layer &&
    13.                 castShadows == other.castShadows &&
    14.                 receiveShadows == other.receiveShadows &&
    15.                 needMotionVectorPass == other.needMotionVectorPass;
    16.         }
    17.  
    18.         /// <summary>
    19.         /// A representative hash code.
    20.         /// </summary>
    21.         /// <returns>A number that is guaranteed to be the same when generated from two objects that are the same.</returns>
    22.         public override int GetHashCode()
    23.         {
    24.             int hash = 0;
    25.             if (!ReferenceEquals(mesh, null)) hash ^= mesh.GetHashCode();
    26.             if (!ReferenceEquals(material, null)) hash ^= material.GetHashCode();
    27.             hash ^= subMesh.GetHashCode();
    28.             hash ^= layer.GetHashCode();
    29.             hash ^= castShadows.GetHashCode();
    30.             hash ^= receiveShadows.GetHashCode();
    31.             hash ^= needMotionVectorPass.GetHashCode();
    32.             return hash;
    33.         }
     
    deus0 likes this.
  4. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    Yeah I made silly mistake and I didn't calculate hash for texture at all, now I made it so texture has influence on hash generation and now everything works fine. Thanks!
     
  5. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    Life Saver. Didn't realize SetSharedComponent used a comparison internally. Added this bad boy to my custom rendermesh and badabing badaboom! Materials are finally set properly in my fade in systems!