Skip to content

Commit

Permalink
Issue pnarimani#121 'Button is not child of selected GO' is resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 25, 2024
1 parent 217d9dc commit 32aaae7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Assets/RTLTMPro/Scripts/Editor/ContextMenu.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TMPro;
using Codice.CM.Client.Differences;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -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<RectTransform>();
var buttonImage = buttonGo.GetComponent<Image>();
Expand Down Expand Up @@ -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<Canvas>() != null;
if (hasCanvasInParent)
return activeGo;

return GetOrCreateCanvasGameObject();
}
}
}

0 comments on commit 32aaae7

Please sign in to comment.