Ejemplo n.º 1
0
        public static ValueT HandleDefaultAndUserValue <ValueT>(string name, DefaultAndUserValue <ValueT> valInField, GUISkin skin) where ValueT : struct
        {
            bool       guiWasEnabled = UnityEngine.GUI.enabled;
            ValueT     newValue      = default(ValueT);
            MethodInfo floatMethod   = typeof(EditorGUILayout).GetMethod("FloatField", new[] { typeof(string), typeof(float), typeof(GUILayoutOption[]) });
            MethodInfo vector3Method = typeof(EditorGUILayout).GetMethod("Vector3Field", new[] { typeof(string), typeof(Vector3), typeof(GUILayoutOption[]) });
            MethodInfo method        = typeof(ValueT) == typeof(float) ?
                                       floatMethod :
                                       typeof(ValueT) == typeof(Vector3) ?
                                       vector3Method :
                                       null;

            if (method == null)
            {
                throw new NullReferenceException("Unknown DefaultAndUserValue type: " + typeof(ValueT).Name);
            }

            bool useDefaultToggled  = false;
            bool updateDefaultValue = false;

            GUILayout.BeginHorizontal();
            {
                // Note that we're checking if the value has changed!
                useDefaultToggled = Toggle(MakeLabel(name.SplitCamelCase(), false, "If checked - value will be default. Uncheck to manually enter value."),
                                           valInField.UseDefault,
                                           skin.button,
                                           Align(skin.label, TextAnchor.MiddleLeft),
                                           new GUILayoutOption[] { GUILayout.Width(22) },
                                           new GUILayoutOption[] { GUILayout.MaxWidth(120) }) != valInField.UseDefault;
                UnityEngine.GUI.enabled = !valInField.UseDefault;
                GUILayout.FlexibleSpace();
                newValue = (ValueT)method.Invoke(null, new object[] { "", valInField.Value, new GUILayoutOption[] { } });
                UnityEngine.GUI.enabled = valInField.UseDefault;
                updateDefaultValue      = GUILayout.Button(MakeLabel("Update", false, "Update default value"), skin.button, GUILayout.Width(52));
                UnityEngine.GUI.enabled = guiWasEnabled;
            }
            GUILayout.EndHorizontal();

            if (useDefaultToggled)
            {
                valInField.UseDefault = !valInField.UseDefault;
                updateDefaultValue    = valInField.UseDefault;

                // We don't want the default value to be written to
                // the user specified.
                if (!valInField.UseDefault)
                {
                    newValue = valInField.UserValue;
                }
            }

            if (updateDefaultValue)
            {
                valInField.OnForcedUpdate();
            }

            return(newValue);
        }
Ejemplo n.º 2
0
 private void PropagateChangesT <ValueT>(DefaultAndUserValue <ValueT> destination)
     where ValueT : struct
 {
     if (DefaultToggleChanged)
     {
         destination.UseDefault = UseDefault;
     }
     if (UpdateDefaultClicked)
     {
         destination.OnForcedUpdate();
     }
 }