Hello all,
I’ve been looking for more info on this for a while, but I’m just not getting it.
So, I’ve successfully created a hashset of strings via TextAsset and everything works as expected. On Mac builds, however, not so much. I don’t get any errors building the hashset, and it seems to work at first, but then it will “lose” its values and revert back to an empty hashset. On top of that, the Contains() method and I think a few others seem to have no effect/return false even when I know beyond a doubt it contains the value.
It seems parts of .net aren’t making their way over to osx? I read that I need to explicitly point it to the System.Core dll in the object using the missing class(es), but I can’t figure it out… Does anyone have experience with this issue or know how to work around it?
Any insight would be appreciated a million times over! Thanks guys.
Sorry, no experience with OSX but I found a number of complaints regarding HashSet and Mono 2.
Best I can tell, Unity is using Mono version 2.0.50727.1433 and if you look here: Release Notes Mono 2.10.8 | Mono it seems HashSet wasn’t serializable until version 2.10
Is there a reason you are using HashSet? It is a relatively new construct in .net. Would Dictionary or List suit your needs?
Hey eisen, thanks for the response.
Gotchya. Indeed they would. The crazy thing is I tried Dictionary and List (List wouldn’t be ideal since it’s not hashed but it’s not that important in the scope of things) and got the exact same behavior. I even put Unity on my friend’s mac, thinking it might just be a windows machine making an osx build causing the problem, but no joy. Even in his editor, it’s the same behavior.
Unity has an issue with serializing generics. Make sure you aren’t running into this problem:
Also, Dictionaries need some special treatment. Look at the second answer here:
Thanks again for the efforts, eisen, but still no luck. I went ahead and made a custom Dictionary class just in case–which is great to know about them and serialization, actually, I wasn’t aware of that–but still the same. So perplexing!
…
Aha! I think it’s Linq that’s the trouble-maker, after all. Still not sure why the dictionary occasionally resets to an empty nothing, but I replaced it with an array of strings and still had issues. Then I replaced Contains() with a foreach loop and an if statement, to test it, and voila. Hmm, I’ll keep at it…
Edit: And ToDictionary() is Linq, too! I’ve been googling this so long I forgot about all the linq results
This sounds very suspect. Are you certain it only acts strangely on OSX?
If you post your code someone might notice why you are getting strange behavior.
Sure thing. Here’s the original, start to finish, before I started messing with it
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class Webster : MonoBehaviour {
HashSet<string> webster;
void Start ()
{
TextAsset dictFile = (TextAsset)Resources.Load ("dictionary");
webster = new HashSet<string>(dictFile.text.Split (System.Environment.NewLine.ToCharArray()));
}
public bool IsWerd(string werd)
{
if (webster.Contains (werd)) {
return true;
}
return false;
}
}
The other just replaces the hashsets with Dictionary<string, int> (the one line has .ToDictionary(key => key, val => 0); ), and this latest test just has string[ ] fart (I’m way too old for that) set to the split text, and IsWerd has the aforementioned foreach(string s in fart), if(s == fart[iterator]) etc
Thanks again for your time, goodness.
Another edit: And yes, it works perfectly on every (3 different) windows machine.