Last active
September 8, 2015 03:32
-
-
Save rutcreate/a2307731e5e1eebfd523 to your computer and use it in GitHub Desktop.
Unity3D: Basic Editor Field
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 BaseField<T> { | |
public string name; | |
public T value; | |
public Field(string name) { | |
this.name = name; | |
this.value = default(T); | |
} | |
public Field(string name, T defaultValue) { | |
this.name = name; | |
this.value = defaultValue; | |
} | |
public virtual T DrawField() { | |
return default(T); | |
} | |
} |
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 CharacterModel { | |
public IntField id = new IntField("ID"); | |
public TextField name = new TextField("Name"); | |
} |
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; | |
[System.Serializable] | |
public class Database { | |
public CharacterModel character; | |
public List<CharacterModel> characters = new List<CharacterModel>(); | |
} |
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
public class DatabaseEditorWindow : EditorWindow { | |
[MenuItem("Window/Database Editor", false, 2)] | |
public static void OpenDatabaseEditorWindow() { | |
EditorWindow.GetWindow(typeof(DatabaseEditorWindow), false, "DB Editor"); | |
} | |
public Database database; | |
public void OnGUI() { | |
database.character.id.DrawField(); | |
database.character.name.DrawField(); | |
foreach (CharacterModel cm in database.characters) { | |
cm.id.DrawField(); | |
cm.name.DrawField(); | |
} | |
} | |
} |
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 IntField : BaseField<int> { | |
public IntField(string name) : base(name) {} | |
public IntField(string name, int defaultValue) : base(name, defaultValue) {} | |
public override int DrawField() { | |
#if UNITY_EDITOR | |
Value = UnityEditor.EditorGUILayout.IntField(Name, Value); | |
return Value; | |
#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
[System.Serializable] | |
public class TextField : BaseField<string> { | |
public TextField(string name) : base(name) {} | |
public TextField(string name, string defaultValue) : base(name, defaultValue) {} | |
public override string DrawField() { | |
#if UNITY_EDITOR | |
Value = UnityEditor.EditorGUILayout.TextField(Name, Value); | |
return Value; | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment