14 [RequireComponent(typeof(CompoundButton))]
17 [Tooltip(
"Toggle behavior")]
20 [Tooltip(
"Profile to use when State is TRUE")]
24 [Tooltip(
"Profile to use when State is FALSE")]
28 [DropDownComponent(
true)]
29 [Tooltip(
"Component to target - must inherit from ProfileButtonBase")]
52 private void OnEnable() {
53 m_compButton = GetComponent<CompoundButton>();
56 SetState(state,
true);
58 if (m_compButton != null)
65 private void OnDisable()
67 if (m_compButton != null)
103 private void SetState (
bool newState,
bool force =
false) {
104 if ((!force || !Application.isPlaying) && state == newState)
107 if (Target == null || OnProfile == null || OffProfile == null)
115 FieldInfo fieldInfo = Target.GetType().GetTypeInfo().GetField(
"Profile");
117 FieldInfo fieldInfo = Target.GetType().GetField(
"Profile");
119 if (fieldInfo == null) {
120 Debug.LogError(
"Target component had no field type profile in CompoundButtonToggle");
124 fieldInfo.SetValue(Target, state ? OnProfile : OffProfile);
126 if (Application.isPlaying) {
129 ((MonoBehaviour)Target).enabled =
false;
130 ((MonoBehaviour)Target).enabled =
true;
136 public class CustomEditor : MRTKEditor
138 protected override void DrawCustomFooter() {
141 FieldInfo fieldInfo = null;
142 Type profileType = null;
143 if (toggle.
Target == null) {
144 DrawError(
"Target must be set.");
148 fieldInfo = toggle.
Target.GetType().GetField(
"Profile");
150 if (fieldInfo == null) {
151 DrawError(
"Target component has no 'Profile' field - are you use this class inherits from ProfileButtonBase?");
155 GUIStyle labelStyle =
new GUIStyle(
UnityEditor.EditorStyles.label);
156 labelStyle.fontSize = 18;
157 labelStyle.fontStyle = FontStyle.Bold;
159 profileType = fieldInfo.FieldType;
160 UnityEditor.EditorGUILayout.LabelField(
"Type: " + toggle.
Target.GetType().Name +
" / " + fieldInfo.FieldType.Name, labelStyle, GUILayout.MinHeight(24));
164 UnityEditor.EditorGUILayout.LabelField(
"Select on/off profiles of the type " + profileType.Name);
173 if (onProfile.GetType() == profileType) {
176 if (offProfile.GetType() == profileType) {
180 if (toggle.
OnProfile.GetType() != profileType) {
181 DrawError(
"On profile object does not match type " + profileType.Name);
183 if (toggle.
OffProfile.GetType() != profileType) {
184 DrawError(
"Off profile object does not match type " + profileType.Name);
187 if (onProfile == offProfile) {
188 DrawWarning(
"Profiles are the same - toggle will have no effect");