From 32aaae79f94b391af52268111aa7aa6578f1c70a Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 26 Jan 2024 01:45:55 +0530 Subject: [PATCH] Issue #121 'Button is not child of selected GO' is resolved --- Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs b/Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs index d7f4a89..6625a98 100644 --- a/Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs +++ b/Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs @@ -1,4 +1,5 @@ -using TMPro; +using Codice.CM.Client.Differences; +using TMPro; using UnityEditor; using UnityEngine; using UnityEngine.UI; @@ -155,7 +156,7 @@ public static void AddDropdown(MenuCommand menuCommand) [MenuItem("GameObject/UI/Button - RTLTMP", false, 2005)] public static void CreateButton(MenuCommand command) { - var canvas = GetOrCreateCanvasGameObject().transform; + var canvas = GetParentForNewObject().transform; var buttonGo = new GameObject("Button", typeof(RectTransform), typeof(Image), typeof(Button)); var buttonTransform = buttonGo.GetComponent(); var buttonImage = buttonGo.GetComponent(); @@ -322,9 +323,21 @@ public static GameObject GetOrCreateCanvasGameObject() canvas = FindObjectOfType(typeof(Canvas)) as Canvas; if (canvas != null && canvas.gameObject.activeInHierarchy) return canvas.gameObject; - + // No canvas in the scene at all? Then create a new one. return CreateNewUI(); } + + public static GameObject GetParentForNewObject() + { + var activeGo = Selection.activeGameObject; + + //Combine the result of the Null check and if the component Canvas exists or not in the parent. + var hasCanvasInParent = activeGo != null && activeGo.GetComponentInParent() != null; + if (hasCanvasInParent) + return activeGo; + + return GetOrCreateCanvasGameObject(); + } } } \ No newline at end of file