Created
July 1, 2020 10:47
-
-
Save James-Frowen/cc2746a0058853e3ca6199322403e3e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AssetSet : ScriptableObject { } | |
public abstract class AssetSet<T> : AssetSet where T : Object | |
{ | |
[SerializeField] protected List<T> _items = new List<T>(); | |
private Dictionary<string, T> _dictionary; | |
public string TypeName => typeof(T).Name; | |
public const string CAN_NOT_FIND_WARNING = "{0} could not find {1}"; | |
public bool TryFind(string itemName, out T value) | |
{ | |
return this._dictionary.TryGetValue(itemName.ToLower(), out value); | |
} | |
public T Find(string itemName) | |
{ | |
this._dictionary.TryGetValue(itemName.ToLower(), out var value); | |
return value; | |
} | |
public T FindOrFirst(string itemName) | |
{ | |
return this._dictionary.TryGetValue(itemName.ToLower(), out var value) ? value : this._items[0]; | |
} | |
public T Find(string itemName, bool ShowWarning) | |
{ | |
if (!this._dictionary.TryGetValue(itemName, out var value) && ShowWarning) | |
{ | |
GameLog.LogWarningFormat(CAN_NOT_FIND_WARNING, this.TypeName, itemName); | |
} | |
return value; | |
} | |
public virtual void OnValidate() | |
{ | |
var names = new List<string>(); | |
foreach (var item in this._items) | |
{ | |
if (item == null) { continue; } | |
if (names.Contains(item.name)) | |
{ | |
GameLog.LogErrorFormat(this, "{0} has multiple objects with name '{1}'", this.name, item.name); | |
} | |
else | |
{ | |
names.Add(item.name); | |
} | |
} | |
} | |
private void OnEnable() | |
{ | |
this._dictionary = new Dictionary<string, T>(); | |
foreach (var item in this._items) | |
{ | |
if (item == null) { continue; } | |
var key = item.name.ToLower(); | |
if (this._dictionary.ContainsKey(key)) | |
{ | |
GameLog.LogError($"Dictionary '{this.name}' already contains key: '{key}'. Existing item: {this._dictionary[key].name} New Item: {item.name}"); | |
continue; | |
} | |
this._dictionary.Add(key, item); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CreateAssetMenu(fileName = "Label Set", menuName = "Asset Set/Labels")] | |
public class LabelAssetSet : AssetSet<WeaponPositionLabel> | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBehaviour : NetworkBehaviour | |
{ | |
public LabelAssetSet AllLabels; | |
[SerializeField] WeaponPositionLabel _currentLabel; | |
public WeaponPositionLabel CurrentLabel | |
{ | |
get => this._currentLabel; | |
set | |
{ | |
// only set sync var on server | |
if (this.isServer) | |
{ | |
this._currentLabel = value; | |
this.currentLabelName = value.name; | |
} | |
} | |
} | |
[SyncVar(hook = nameof(onCurrentLabelChanged))] | |
string currentLabelName; | |
void onCurrentLabelChanged(string _, string newValue) | |
{ | |
this._currentLabel = this.AllLabels.Find(newValue); | |
} | |
public override void OnStartServer() | |
{ | |
this.currentLabelName = this._currentLabel.name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment