Last active
March 16, 2021 09:31
-
-
Save bzgeb/420f57262ded46f83333aabc1731ccc1 to your computer and use it in GitHub Desktop.
Configs and Descs
This file contains 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
using System.Collections.Generic; | |
using UnityEngine; | |
public abstract class Config<T> : ScriptableObject where T : Desc | |
{ | |
[SerializeField] protected List<T> m_Descs = new List<T>(); | |
public List<T> Descs { get { return m_Descs; } } | |
public virtual T FindByUuid(string a_Uuid) | |
{ | |
foreach (var desc in m_Descs) | |
{ | |
if (desc.DescId.uuid == a_Uuid) | |
{ | |
return desc; | |
} | |
} | |
return null; | |
} | |
} |
This file contains 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
using UnityEngine; | |
public abstract class Desc : ScriptableObject | |
{ | |
[SerializeField] protected Uuid m_DescId = null; | |
public Uuid DescId { get { return m_DescId; } } | |
#if UNITY_EDITOR | |
public void GenerateNewUuid() | |
{ | |
m_DescId = Uuid.Generate(); | |
} | |
#endif | |
} |
This file contains 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
using UnityEngine; | |
[CreateAssetMenu()] | |
public class PropConfig : Config<PropDesc> | |
{ | |
} |
This file contains 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
using UnityEngine; | |
[CreateAssetMenu()] | |
public class PropDesc : Desc | |
{ | |
public Prop m_PropPrefab; | |
} |
This file contains 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
[System.Serializable] | |
public class Uuid | |
{ | |
public string uuid; | |
public static Uuid Generate() | |
{ | |
Uuid newUuid = new Uuid {uuid = System.Guid.NewGuid().ToString()}; | |
return newUuid; | |
} | |
} |
This file contains 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
using System; | |
using UnityEditor; | |
using UnityEngine; | |
[CustomPropertyDrawer(typeof(Uuid))] | |
public class UuidDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect a_Position, SerializedProperty a_Property, GUIContent a_Label) | |
{ | |
EditorGUI.BeginProperty(a_Position, a_Label, a_Property); | |
a_Position = EditorGUI.PrefixLabel(a_Position, GUIUtility.GetControlID(FocusType.Passive), a_Label); | |
int indent = EditorGUI.indentLevel; | |
EditorGUI.indentLevel = 0; | |
const int uuidButtonWidth = 80; | |
Rect uuidRect = new Rect(a_Position.x, a_Position.y, a_Position.width - uuidButtonWidth, a_Position.height); | |
Rect newUuidButtonRect = | |
new Rect(a_Position.x + uuidRect.width, a_Position.y, uuidButtonWidth, a_Position.height); | |
SerializedProperty uuidProperty = a_Property.FindPropertyRelative("uuid"); | |
EditorGUI.SelectableLabel(uuidRect, uuidProperty.stringValue); | |
if (GUI.Button(newUuidButtonRect, "New Uuid")) | |
{ | |
if (EditorUtility.DisplayDialog("Generate new UUID?", | |
"Generating a new UUID will break any data using the current uuid", "Yes", "Cancel")) | |
{ | |
uuidProperty.stringValue = Guid.NewGuid().ToString(); | |
} | |
} | |
EditorGUI.indentLevel = indent; | |
EditorGUI.EndProperty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment