/// <inheritdoc/> protected override void ApplyValuesToFields(object[] values) { if (Inspector.State.usingLocalSpace || !(parent is ScaleDrawer)) { base.ApplyValuesToFields(values); return; } #if DEV_MODE Debug.Log(ToString() + ".WriteToField(" + StringUtils.ToString(values) + ") with world space"); #endif bool changed = false; // Can't write to field when using LinkedMemberInfo, because lossyScale property is get-only. // Convert to localScale and write directly to transform targets instead. var targets = UnityObjects; for (int n = targets.Length - 1; n >= 0; n--) { var transform = targets[n] as Transform; var setLossyScale = transform.lossyScale; float value = (float)values[n]; if (transform.lossyScale[memberIndex] != value) { if (!changed) { changed = true; UndoHandler.RegisterUndoableAction(targets, UndoHandler.GetSetValueMenuText(parent.Name)); } setLossyScale[memberIndex] = value; transform.SetWorldScale(setLossyScale); } } }
/// <inheritdoc/> protected override void ApplyValuesToFields(object[] values) { if (Inspector.State.usingLocalSpace) { base.ApplyValuesToFields(values); return; } // Can't write to field when using LinkedMemberInfo, because lossyScale property is get-only. // Convert to localScale and write directly to transform targets instead. var targets = UnityObjects; #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(values.Length == targets.Length); #endif bool changed = false; for (int n = targets.Length - 1; n >= 0; n--) { var transform = targets[n] as Transform; var setLossyScale = (Vector3)values[n]; if (!ValuesAreEqual(transform.lossyScale, setLossyScale)) { if (!changed) { changed = true; UndoHandler.RegisterUndoableAction(targets, UndoHandler.GetSetValueMenuText(Name)); } transform.SetWorldScale(setLossyScale); } } }
/// <inheritdoc/> protected override void ApplyValueToField() { if (Inspector.State.usingLocalSpace) { base.ApplyValueToField(); return; } bool changed = false; var setLossyScale = Value; // Can't write to field when using LinkedMemberInfo, because lossyScale property is get-only. // Convert to localScale and write directly to transform targets instead. var targets = UnityObjects; for (int n = targets.Length - 1; n >= 0; n--) { var transform = targets[n] as Transform; if (!ValuesAreEqual(transform.lossyScale, setLossyScale)) { if (!changed) { changed = true; UndoHandler.RegisterUndoableAction(targets, UndoHandler.GetSetValueMenuText(Name)); } transform.SetWorldScale(setLossyScale); } } }
private void Setup() { if (instance != this) { if (instance == null) { instance = this; } else { #if DEV_MODE Debug.LogWarning("Destroying UndoHandler because there were multiple instances"); #endif DestroyImmediate(this); return; } } if (!listeningToEvents) { listeningToEvents = true; DrawGUI.OnEveryBeginOnGUI(OnBeginOnGUI, false); #if UNITY_EDITOR UnityEditor.Undo.undoRedoPerformed += DetectUndoneActions; #endif } Clear(); }
public static void OnCustomEnabledControlClicked(IComponentDrawer drawer, Event inputEvent) { DrawGUI.Use(inputEvent); var targets = drawer.Components; var firstBehaviour = targets[0] as Behaviour; #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(firstBehaviour != null, "createCustomEnabledFlag was true but target was not a Behaviour"); #endif bool setEnabled = !firstBehaviour.enabled; var changed = targets; for (int n = targets.Length - 1; n >= 1; n--) { if (((Behaviour)targets[n]).enabled == setEnabled) { changed = changed.RemoveAt(n); } } UndoHandler.RegisterUndoableAction(changed, changed.Length == 1 ? (setEnabled ? "Enable Component" : "Disable Component") : (setEnabled ? "Enable Components" : "Disable Components")); firstBehaviour.enabled = setEnabled; for (int n = targets.Length - 1; n >= 1; n--) { ((Behaviour)targets[n]).enabled = setEnabled; } }
/// <summary> Copies the vector's magnitude to clipboard. </summary> private void PasteEulerAnglesFromClipboard() { #if SAFE_MODE || DEV_MODE if (ReadOnly) { #if DEV_MODE Debug.LogWarning("PasteFromClipboard disabled for " + ToString() + " because ReadOnly"); #endif return; } #endif // this fixes some problems like when you paste on an array field with its resize field being selected, // the array won't update its contents. It could theoretically also work by only changing the selection // if the selected object is grandchild of this field, but then that would break consistency (sometimes // selection changes, sometimes not). It could also work by deselecting whatever was previously selected, // but that would be weird when the pasting is done via a keyboard shortcut (ctrl+V). Select(ReasonSelectionChanged.Unknown); UndoHandler.RegisterUndoableAction(UnityObjects, "Paste From Clipboard"); var setEulerAngles = Clipboard.Paste <Vector3>(); SetValue(Quaternion.Euler(setEulerAngles)); SendPasteFromClipboardMessage(); }
private void SetDays(int set) { var setValue = new TimeSpan(set, Hours, Minutes, Seconds, Milliseconds); UndoHandler.RegisterUndoableAction(memberInfo, setValue, "Days", true); SetValue(setValue, !ReadOnly, false); }
public static void NameByType(GameObject[] targets, Component[] components) { UndoHandler.RegisterUndoableAction(targets, "Auto-Name"); for (int n = targets.Length - 1; n >= 0; n--) { NameByType(targets[n], components[n]); } }
private void SetDay(int set) { if (Day != set && set >= 1 && set <= DateTime.DaysInMonth(Year, Month)) { var setValue = new DateTime(Year, Month, set, Hour, Minute, Second, Millisecond); UndoHandler.RegisterUndoableAction(memberInfo, setValue, UndoHandler.GetSetValueMenuText("Day"), true); SetValue(setValue, !ReadOnly, false); } }
private void SetYear(int set) { if (Year != set && set >= DateTime.MinValue.Year && set <= DateTime.MaxValue.Year) { var setValue = new DateTime(set, Month, Day, Hour, Minute, Second, Millisecond); UndoHandler.RegisterUndoableAction(memberInfo, setValue, "Year", true); SetValue(setValue, !ReadOnly, false); } }
private void SetHour(int set) { if (Hour != set && set >= 0 && set <= DateTime.MaxValue.Hour) { var setValue = new DateTime(Year, Month, Day, set, Minute, Second, Millisecond); UndoHandler.RegisterUndoableAction(memberInfo, setValue, UndoHandler.GetSetValueMenuText("Hour"), true); SetValue(setValue, !ReadOnly, false); } }
private void SetMinutes(int set) { if (set != Minutes && set >= TimeSpan.MinValue.Minutes && set <= TimeSpan.MaxValue.Minutes) { var setValue = new TimeSpan(Days, Hours, set, Seconds, Milliseconds); UndoHandler.RegisterUndoableAction(memberInfo, setValue, "Minutes", true); DoSetValue(setValue, !ReadOnly, false); } }
private void SetHours(int set) { if (set != Hours && set > -24 && set < 24) { var setValue = new TimeSpan(Days, set, Minutes, Seconds, Milliseconds); UndoHandler.RegisterUndoableAction(memberInfo, setValue, "Hours", true); SetValue(setValue, !ReadOnly, false); } }
private void SetMinute(int set) { if (Minute != set && set >= 0 && set < 60) { var setValue = new DateTime(Year, Month, Day, Hour, set, Second, Millisecond); UndoHandler.RegisterUndoableAction(memberInfo, setValue, UndoHandler.GetSetValueMenuText("Minute"), true); SetValue(setValue, !ReadOnly, false); } }
private static void SetExternal <T>(Object target, ref T subject, [NotNull] T value, string undoMessage) { if (!value.Equals(subject)) { UndoHandler.RegisterUndoableAction(target, string.Format(undoMessage, value)); subject = value; Platform.Active.SetDirty(target); } }
private void Set <T>(ref T subject, [NotNull] T value, string undoMessage) { if (!value.Equals(subject)) { UndoHandler.RegisterUndoableAction(this, string.Format(undoMessage, value)); subject = value; Platform.Active.SetDirty(this); } }
private void SetSeconds(float set) { if (SecondWithFractions != set && set >= 0 && set <= 60) { int s = Mathf.FloorToInt(set); int ms = Mathf.RoundToInt((set - s) * 1000f); var setValue = new DateTime(Year, Month, Day, Hour, Minute, s, ms); UndoHandler.RegisterUndoableAction(memberInfo, setValue, UndoHandler.GetSetValueMenuText("Second"), true); SetValue(setValue, !ReadOnly, false); } }
public void UpdateNow([NotNull] InspectorPreferences preferences) { UndoHandler.RegisterUndoableAction(preferences, "Update Inspector Preferences"); #if DEV_MODE Debug.Log("Updating preferences to version " + ToVersion + " now..."); #endif ApplyUpdates(preferences); EditorUtility.SetDirty(preferences); }
private void SetSeconds(float set) { if (set != SecondsWithFractions) { int s = Mathf.FloorToInt(set); int ms = Mathf.RoundToInt((set - s) * 1000f); if (s >= TimeSpan.MinValue.Seconds && s <= TimeSpan.MaxValue.Seconds && ms >= TimeSpan.MinValue.Milliseconds && ms <= TimeSpan.MaxValue.Milliseconds) { var setValue = new TimeSpan(Days, Hours, Minutes, s, ms); UndoHandler.RegisterUndoableAction(memberInfo, setValue, "Seconds", true); SetValue(setValue, !ReadOnly, false); } } }
public static UndoHandler Instance() { if (instance == null) { var instances = Resources.FindObjectsOfTypeAll <UndoHandler>(); if (instances.Length > 0) { instance = instances[0]; } else { instance = CreateInstance <UndoHandler>(); instance.name = "Undo Handler"; } instance.hideFlags = HideFlags.DontSave; } return(instance); }
public static void NameByType(GameObject target, Component component) { if (target != null) { UndoHandler.RegisterUndoableAction(target, "Auto-Name"); string nameFromType; if (component == null) { nameFromType = "Missing Script"; } else { var spriteRenderer = component as SpriteRenderer; if (spriteRenderer != null) { nameFromType = spriteRenderer.sprite == null ? "Sprite Renderer" : spriteRenderer.sprite.name; } else { var animator = component as Animator; if (animator != null) { var avatar = animator.avatar; nameFromType = avatar == null ? "Avatar" : avatar.name; } else { nameFromType = StringUtils.SplitPascalCaseToWords(component.GetType().Name); } } } target.name = nameFromType; if (target.IsPrefab()) { Platform.Active.SetDirty(target); } } }
public void ResetToDefaults() { UndoHandler.RegisterUndoableAction(this, "Reset To Defaults"); #if UNITY_EDITOR // in UnityEditor use Presets or EditorJsonUtility #if UNITY_2018_1_OR_NEWER // Presets were introduced in Unity 2018.1 #if UNITY_2019_3_OR_NEWER // GetDefaultForObject became obsolete in Unity 2019.3 var presets = Preset.GetDefaultPresetsForObject(this); var preset = presets.Length > 0 ? presets[0] : null; #else var preset = Preset.GetDefaultForObject(this); #endif // if no default preset has been assigned for preferences asset, then try finding a preset // in the same directory with the preferences asset if (preset == null) { var preferencesPath = AssetDatabase.GetAssetPath(this); var directoryPath = FileUtility.GetParentDirectory(preferencesPath); var updateGuids = AssetDatabase.FindAssets("t:Preset", ArrayExtensions.TempStringArray(directoryPath)); for (int n = updateGuids.Length - 1; n >= 0; n--) { var path = AssetDatabase.GUIDToAssetPath(updateGuids[n]); preset = AssetDatabase.LoadAssetAtPath <Preset>(path); if (!string.Equals(preset.GetTargetFullTypeName(), typeof(InspectorPreferences).FullName, StringComparison.OrdinalIgnoreCase)) { preset = null; } else { break; } } } if (preset != null) { preset.ApplyTo(this); } else #endif { var freshInstance = CreateInstance <InspectorPreferences>(); var jsonString = EditorJsonUtility.ToJson(freshInstance); Platform.Active.Destroy(freshInstance); EditorJsonUtility.FromJsonOverwrite(jsonString, this); } #else // at runtime use JsonUtility to reset values to those of a freshly created instance var freshInstance = CreateInstance <InspectorPreferences>(); var jsonString = JsonUtility.ToJson(freshInstance); Platform.Active.Destroy(freshInstance); JsonUtility.FromJsonOverwrite(jsonString, this); #endif setupDone = false; isFirstOnValidate = true; if (Event.current != null) { Setup(); } Platform.Active.SetDirty(this); if (onSettingsChanged != null) { onSettingsChanged(this); } }