Search Unity

Can't add a value to HashSet<T> in edit mode / dirtying it does nothing

Discussion in 'Scripting' started by Discipol, Dec 24, 2016.

  1. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    83
    I have a scene with a game object that has my component.
    The component has a HashSet<T> (doesn't matter what T is) and I want to add a value to it (via a button in custom inspector Editor) but it doesn't keep when pressing Play and back. I assume the EditorUtility.SetDirty( comp ); doesn't affect it since its not serializable.

    But I do need a solution for this. I thought about making a special List that has ONLY unique values (that's why I am using HashSet<T>) but perhaps one of your gurus have a fix for my problem.

    I am aware that EditorUtility.SetDirty will be depracated at one point, I don't want to use FindProperty because it has a string parameter and my stuff gets renamed and I'd have to make sure it has the same name as the string >.>
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    It's a pain, but Unity's serialization is a pain in general. You COULD do something like this:
    Code (csharp):
    1. [Serializable]
    2. public class HashSetObject : HashSet<SomeSerializableBaseClass> {};
    It's a dirty, ugly, horrible hack, but it should work. I've learned to avoid relying on Unity's serialization in general if at all possible.
     
    Last edited: Dec 24, 2016
  3. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    83
    Sort of works mate, except that I would like to have it work by <T> since I will be using this in many places with several different types of <T>
    This [Serializable] public class SerializableHashSet<T> : HashSet<T> {} does not work :<
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Yup, Unity's serialization hates generics. And Interfaces. And all those handy things that make our lives easy.
     
    a436t4ataf likes this.
  5. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    83
    I am going to try to extend list<T> and override add to force unique values, then overriding the inspector to have it look like a Set instead of an ordered list