SyncHashSet is an easy way to keep a HashSet collection automatically synchronized over the network.
Callbacks for SyncHashSet are similar to SyncList. And like other synchronization types changes are set immediately before the callback occurs.
privatereadonlySyncHashSet<int> _myCollection =newSyncHashSet<int>();privatevoidAwake(){_myCollection.OnChange+= _myCollection_OnChange;}privatevoidFixedUpdate(){ //You can modify a synchashset as you would any other hashset._myCollection.Add(Time.frameCount);}/* Like SyncVars the callback offers an asServer option * to indicate if the callback is occurring on the server * or the client. As SyncVars do, changes have already been * made to the collection before the callback occurs. */privatevoid_myCollection_OnChange(SyncHashSetOperation op,int item,bool asServer){switch (op) {/* An object was added to the hashset. Item is * is the added object. */caseSyncHashSetOperation.Add:break;/* An object has been removed from the hashset. Item * is the removed object. */caseSyncHashSetOperation.Remove:break;/* The hashset has been cleared. * Item will be default. */caseSyncHashSetOperation.Clear:break;/* An entry in the hashset has been updated. * When this occurs the item is removed * and added. Item will be the new value. * Item will likely need a custom comparer * for this to function properly. */caseSyncHashSetOperation.Update:break; /* When complete calls all changes have been * made to the collection. You may use this * to refresh information in relation to * the changes, rather than doing so * after every entry change. All values are * default for this operation. */caseSyncHashSetOperation.Complete:break; }}
If you are using this SyncType with a container, such as a class or structure, and want to modify values within that container, you must set the value dirty. See the example below.
[System.Serializable]privatestructMyContainer{publicstring PlayerName;publicint Level;}privatereadonlySyncHashSet<MyContainer> _containers =new();privateMyContainer _containerReference =new();privatevoidAwake(){_containerReference.Level=5;_containers.Add(_containerReference);}[Server]privatevoidModifyPlayer(){ //This will change the value locally but it will not synchronize to clients._containerReference.Level=10; //The value must be set dirty to force a synchronization._containers.Dirty(_containerReference);}