Skip to content

Instantly share code, notes, and snippets.

@fum1h1ro
Created April 16, 2012 05:27
Show Gist options
  • Save fum1h1ro/2396486 to your computer and use it in GitHub Desktop.
Save fum1h1ro/2396486 to your computer and use it in GitHub Desktop.
public class EditorFieldBase {
protected static uint uid_ = 0;
protected string name_;
protected string name { get { return name_; } }
protected EditorFieldBase() {
name_ = (uid_++).ToString();
}
}
public class EditorIntField : EditorFieldBase {
private GUIStyle style_;
private int value_;
private bool changed_;
public bool changed { get { return changed_; } }
public EditorIntField(int iniv) : base() {
changed_ = false;
value_ = iniv;
}
public int OnGUI(string label, int v) {
if (style_ == null) {
style_ = new GUIStyle(EditorStyles.numberField);
}
changed_ = false;
GUILayout.BeginHorizontal();
GUILayout.Label(label);
GUI.SetNextControlName(this.name);
value_ = EditorGUILayout.IntField(value_, style_, GUILayout.Width(50));
GUILayout.EndHorizontal();
if (v != value_)
style_.normal.textColor = Color.yellow;
else
style_.normal.textColor = Color.white;
Event e = Event.current;
if (e.type != EventType.KeyUp) return v;
e.Use();
if (v != value_ &&
e.keyCode == KeyCode.Return &&
GUI.GetNameOfFocusedControl() != this.name) {
changed_ = true;
return value_;
} else {
return v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment