using Unity.Animations.SpringBones.GameObjectExtensions; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; using UnityEditor.Localization.Editor; namespace Unity.Animations.SpringBones { public static class SpringBoneEditorActions { private static class Styles { public static readonly string logStopPlayMode = Localization.Tr("You must stop Playmode first."); public static readonly string logSelectOneOrMoreObjects = Localization.Tr("Select one or more objects."); public static readonly string logSelectOnlyOneSpringManager = Localization.Tr("Select only one SpringManager"); public static readonly string textDelete = Localization.Tr("Delete"); public static readonly string textCancel = Localization.Tr("Cancel"); public static readonly string textUpdate = Localization.Tr("Update"); public static readonly string textDeleteSpringBoneAndManager = Localization.Tr("Delete SpringBone and Manager"); public static readonly string textDeleteSelectedBones = Localization.Tr("Delete selected bones"); public static readonly string textUpdateFromBoneList = Localization.Tr("Update from bone list"); public static readonly string textConfirmRemoveAllBoneAndManagerFormat = Localization.Tr( "Do you really want to remove all\n" + "SpringBones and managers under this object?\n" + "\n{0}"); public static readonly string textConfirmUpdateBonesFromListFormat = Localization.Tr( "Do you want to update secondary bones from bone list?\n" + "\nThis will remove all SpringBones that are not listed,\n" + "and will add SpringBones missing in model.\n" + "\nSpringManager: {0}\n"); } public static void ShowSpringBoneWindow() { SpringBoneWindow.ShowWindow(); } public static void AssignSpringBonesRecursively() { if (Application.isPlaying) { Debug.LogError(Styles.logStopPlayMode); return; } if (Selection.gameObjects.Length < 1) { Debug.LogError(Styles.logSelectOneOrMoreObjects); return; } var springManagers = new HashSet(); foreach (var gameObject in Selection.gameObjects) { SpringBoneSetup.AssignSpringBonesRecursively(gameObject.transform); var manager = gameObject.GetComponentInParent(); if (manager != null) { springManagers.Add(manager); } } foreach (var manager in springManagers) { SpringBoneSetup.FindAndAssignSpringBones(manager, true); } AssetDatabase.Refresh(); } public static void CreatePivotForSpringBones() { if (Application.isPlaying) { Debug.LogError(Styles.logStopPlayMode); return; } if (Selection.gameObjects.Length < 1) { Debug.LogError(Styles.logSelectOneOrMoreObjects); return; } var selectedSpringBones = Selection.gameObjects .Select(gameObject => gameObject.GetComponent()) .Where(bone => bone != null); foreach (var springBone in selectedSpringBones) { SpringBoneSetup.CreateSpringPivotNode(springBone); } } public static void AddToOrUpdateSpringManagerInSelection() { if (Application.isPlaying) { Debug.LogError(Styles.logStopPlayMode); return; } if (Selection.gameObjects.Length <= 0) { Debug.LogError(Styles.logSelectOneOrMoreObjects); return; } foreach (var gameObject in Selection.gameObjects) { var manager = gameObject.GetComponent(); if (manager == null) { manager = gameObject.AddComponent(); } SpringBoneSetup.FindAndAssignSpringBones(manager, true); } } public static void SelectChildSpringBones() { var springBoneObjects = Selection.gameObjects .SelectMany(gameObject => gameObject.GetComponentsInChildren(true)) .Select(bone => bone.gameObject) .Distinct() .ToArray(); Selection.objects = springBoneObjects; } public static void DeleteSpringBonesAndManagers() { if (Application.isPlaying) { Debug.LogError(Styles.logStopPlayMode); return; } if (Selection.gameObjects.Length != 1) { Debug.LogError(Styles.logSelectOneOrMoreObjects); return; } var rootObject = Selection.gameObjects.First(); var queryMessage = string.Format(Styles.textConfirmRemoveAllBoneAndManagerFormat, rootObject.name); if (EditorUtility.DisplayDialog( Styles.textDeleteSpringBoneAndManager, queryMessage, Styles.textDelete, Styles.textCancel)) { SpringBoneSetup.DestroySpringManagersAndBones(rootObject); AssetDatabase.Refresh(); } } public static void DeleteSelectedBones() { var springBonesToDelete = GameObjectUtil.FindComponentsOfType() .Where(bone => Selection.gameObjects.Contains(bone.gameObject)) .ToArray(); var springManagersToUpdate = GameObjectUtil.FindComponentsOfType() .Where(manager => manager.springBones.Any(bone => springBonesToDelete.Contains(bone))) .ToArray(); Undo.RecordObjects(springManagersToUpdate, Styles.textDeleteSelectedBones); foreach (var boneToDelete in springBonesToDelete) { Undo.DestroyObjectImmediate(boneToDelete); } foreach (var manager in springManagersToUpdate) { manager.FindSpringBones(true); } } public static void PromptToUpdateSpringBonesFromList() { if (Application.isPlaying) { Debug.LogError(Styles.logStopPlayMode); return; } var selectedSpringManagers = Selection.gameObjects .Select(gameObject => gameObject.GetComponent()) .Where(manager => manager != null) .ToArray(); if (!selectedSpringManagers.Any()) { selectedSpringManagers = GameObjectUtil.FindComponentsOfType().ToArray(); } if (selectedSpringManagers.Count() != 1) { Debug.LogError(Styles.logSelectOnlyOneSpringManager); return; } var springManager = selectedSpringManagers.First(); var queryMessage = string.Format(Styles.textConfirmUpdateBonesFromListFormat, springManager.name); if (EditorUtility.DisplayDialog(Styles.textUpdateFromBoneList, queryMessage, Styles.textUpdate, Styles.textCancel)) { AutoSpringBoneSetup.UpdateSpringManagerFromBoneList(springManager); } } } }