diff --git a/Assets/BetterCommons/Editor/Extensions/VisualElementExtension.cs b/Assets/BetterCommons/Editor/Extensions/VisualElementExtension.cs index 85236ac..0552c11 100644 --- a/Assets/BetterCommons/Editor/Extensions/VisualElementExtension.cs +++ b/Assets/BetterCommons/Editor/Extensions/VisualElementExtension.cs @@ -2,6 +2,7 @@ using Better.Commons.EditorAddons.Enums; using Better.Commons.EditorAddons.Utility; using UnityEditor; +using UnityEngine; using UnityEngine.UIElements; namespace Better.Commons.EditorAddons.Extensions @@ -37,5 +38,12 @@ public static Image AddIcon(this VisualElement self, IconType iconType) self.Insert(0, image); return image; } + + public static Image AddIcon(this VisualElement self, Texture texture) + { + var image = VisualElementUtility.CreateLabelIcon(texture); + self.Insert(0, image); + return image; + } } } \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs b/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs new file mode 100644 index 0000000..3bc456a --- /dev/null +++ b/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Better.Commons.Runtime.Extensions; +using Better.Commons.Runtime.Utility; +using UnityEditor; +using UnityEditor.SceneManagement; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Better.Commons.EditorAddons.Utility +{ + public static class SelectionUtility + { + //TODO: Test with GameObject + public static void OpenReference(UnityEngine.Object reference) + { + if (reference.IsNullOrDestroyed()) + { + DebugUtility.LogException(nameof(reference)); + return; + } + + if (reference is Component component) + { + var transform = component.transform; + if (PrefabUtility.IsPartOfPrefabAsset(reference)) + { + var assetPath = AssetDatabase.GetAssetPath(reference); + var stage = PrefabStageUtility.OpenPrefab(assetPath); + var prefabRootTransform = stage.prefabContentsRoot.transform; + var indexes = GetParentIndices(transform); + + transform = GetChildBySiblingIndices(prefabRootTransform, indexes); + } + else + { + var countLoaded = SceneManager.sceneCount; + var loadedScenes = new Scene[countLoaded]; + + for (var i = 0; i < countLoaded; i++) + { + loadedScenes[i] = SceneManager.GetSceneAt(i); + } + + var gameObject = component.gameObject; + if (loadedScenes.Contains(gameObject.scene)) + { + EditorSceneManager.SetActiveScene(gameObject.scene); + } + else + { + EditorSceneManager.OpenScene(gameObject.scene.path); + } + } + + Selection.activeTransform = transform; + + //TODO: check if working + if (SceneView.lastActiveSceneView != null) + { + SceneView.lastActiveSceneView.MoveToView(transform); + } + + EditorGUIUtility.PingObject(transform); + } + else + { + Selection.SetActiveObjectWithContext(reference, reference); + EditorGUIUtility.PingObject(reference); + } + } + + private static List GetParentIndices(Transform instance) + { + if (instance.IsNullOrDestroyed()) + { + DebugUtility.LogException(nameof(instance)); + return new List(); + } + + var siblingIndices = new List(); + + var parent = instance.parent; + if (parent == null) + { + return siblingIndices; + } + + // Add the sibling index of the initial transform + siblingIndices.Add(instance.GetSiblingIndex()); + // Traverse up the hierarchy to add sibling indices of parent transforms + while (parent != null && parent != instance.root) + { + siblingIndices.Add(parent.GetSiblingIndex()); + parent = parent.parent; + } + + return siblingIndices; + } + + private static Transform GetChildBySiblingIndices(Transform transform, IReadOnlyList siblingIndices) + { + var child = transform; + for (var i = siblingIndices.Count - 1; i >= 0; i--) + { + var siblingIndex = siblingIndices[i]; + if (siblingIndex >= 0 && siblingIndex < child.childCount) + { + child = child.GetChild(siblingIndex); + } + else + { + // Sibling index out of range, return null + return null; + } + } + + return child; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs.meta b/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs.meta new file mode 100644 index 0000000..d0fbe14 --- /dev/null +++ b/Assets/BetterCommons/Editor/Utility/SelectionUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6d7c9b07c05d402da3e724c7fa9f5c61 +timeCreated: 1709763659 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Utility/VisualElementUtility.cs b/Assets/BetterCommons/Editor/Utility/VisualElementUtility.cs index 01597bd..a731bc3 100644 --- a/Assets/BetterCommons/Editor/Utility/VisualElementUtility.cs +++ b/Assets/BetterCommons/Editor/Utility/VisualElementUtility.cs @@ -5,6 +5,7 @@ using Better.Commons.Runtime.Utility; using UnityEditor; using UnityEditor.UIElements; +using UnityEngine; using UnityEngine.UIElements; namespace Better.Commons.EditorAddons.Utility @@ -118,19 +119,26 @@ public static VisualElement CreateHorizontalGroup() public static VisualElement CreateVerticalGroup() { var element = new VisualElement(); - element.style.flexDirection = new StyleEnum(FlexDirection.Column); + element.style.flexDirection = FlexDirection.Column; return element; } public static Image CreateLabelIcon(IconType iconType) { var icon = iconType.GetIcon(); + return CreateLabelIcon(icon); + } + + public static Image CreateLabelIcon(Texture icon) + { var image = new Image { image = icon }; - image.style.Height(StyleDefinition.SingleLineHeight).Width(StyleDefinition.SingleLineHeight).AlignSelf(new StyleEnum(Align.Center)); + image.style.Height(StyleDefinition.SingleLineHeight) + .Width(StyleDefinition.SingleLineHeight) + .AlignSelf(new StyleEnum(Align.Center)); return image; } diff --git a/Assets/BetterCommons/Runtime/Extensions/StringExtensions.cs b/Assets/BetterCommons/Runtime/Extensions/StringExtensions.cs index ae96b52..f874c3b 100644 --- a/Assets/BetterCommons/Runtime/Extensions/StringExtensions.cs +++ b/Assets/BetterCommons/Runtime/Extensions/StringExtensions.cs @@ -32,11 +32,6 @@ public static string FormatBoldItalic(this string text) return $"{text}"; } - public static string BeautifyFormat(string text) - { - return $"\"{text}\""; - } - /// /// Makes first char upper /// diff --git a/Assets/BetterCommons/Runtime/Extensions/StyleExtensions.cs b/Assets/BetterCommons/Runtime/Extensions/StyleExtensions.cs index 043b386..909a572 100644 --- a/Assets/BetterCommons/Runtime/Extensions/StyleExtensions.cs +++ b/Assets/BetterCommons/Runtime/Extensions/StyleExtensions.cs @@ -236,6 +236,15 @@ public static IStyle MarginTop(this IStyle self, StyleLength marginTop) self.marginTop = marginTop; return self; } + + public static IStyle Margin(this IStyle self, StyleLength marginTop) + { + self.marginTop = marginTop; + self.marginRight = marginTop; + self.marginBottom = marginTop; + self.marginLeft = marginTop; + return self; + } public static IStyle MaxHeight(this IStyle self, StyleLength maxHeight) { @@ -296,6 +305,15 @@ public static IStyle PaddingTop(this IStyle self, StyleLength paddingTop) self.paddingTop = paddingTop; return self; } + + public static IStyle Padding(this IStyle self, StyleLength marginTop) + { + self.paddingTop = marginTop; + self.paddingRight = marginTop; + self.paddingBottom = marginTop; + self.paddingLeft = marginTop; + return self; + } public static IStyle Position(this IStyle self, StyleEnum position) { diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index 12fc4fc..6c2e4f2 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.22", + "version": "0.0.23", "unity": "2021.3", "description": " ", "dependencies": {