ScriptReference/GameObject-ctor #205
Replies: 1 comment
-
|
To create a new GameObject that has a RectTransform (such as for use in UI), you should supply the RectTransform type in the second parameter, i.e. new GameObject("GameObject", typeof(RectTransform));This is particularly useful if extending Unity editor, such as through this menu item for creating a new GameObject as a sibling of the selection: [MenuItem("GameObject/Create Sibling", false, 0)]
static void CreateSibling()
{
List<UnityEngine.Object> newSelection = new List<UnityEngine.Object>();
foreach (Transform selectedTransform in Selection.transforms)
{
GameObject newSibling;
if (selectedTransform.GetComponent<RectTransform>() != null)
newSibling = new GameObject("GameObject", typeof(RectTransform));
else
newSibling = new GameObject("GameObject");
Undo.RegisterCreatedObjectUndo(newSibling, "Create Sibling");
newSibling.transform.SetParent(selectedTransform.parent, false);
newSibling.transform.SetSiblingIndex(selectedTransform.GetSiblingIndex() + 1);
newSelection.Add(newSibling);
}
Selection.objects = newSelection.ToArray();
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
ScriptReference/GameObject-ctor
https://docs.unity3d.com/ScriptReference/GameObject-ctor.html
Beta Was this translation helpful? Give feedback.
All reactions