// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using Doozy.Runtime.Common.Extensions;
using UnityEngine;
// ReSharper disable MemberCanBePrivate.Global
namespace Doozy.Runtime.Common.Utils
{
public static class GameObjectUtils
{
/// Adds a new GameObject with the attached MonoBehavior of type T
/// If TRUE, it will check if there isn't another GameObject with the MonoBehavior attached. If there is, it will select it (Editor only)
/// If TRUE, after creating a new GameObject, it will get automatically selected (Editor only)
/// MonoBehaviour
public static T AddToScene(bool isSingleton, bool selectGameObjectAfterCreation) where T : MonoBehaviour =>
AddToScene(ObjectNames.NicifyVariableName(typeof(T).Name), isSingleton, selectGameObjectAfterCreation);
/// Adds a new GameObject with the attached MonoBehavior of type T
/// The name of the newly created GameObject
/// If TRUE, it will check if there isn't another GameObject with the MonoBehavior attached. If there is, it will select it (Editor only)
/// If TRUE, after creating a new GameObject, it will get automatically selected (Editor only)
/// MonoBehaviour
public static T AddToScene(string gameObjectName, bool isSingleton, bool selectGameObjectAfterCreation) where T : MonoBehaviour
{
#if UNITY_2023_1_OR_NEWER
T component = Object.FindFirstObjectByType();
#else
T component = Object.FindObjectOfType();
#endif
if (component != null && isSingleton)
{
// Debugger.Log($"Cannot add another <{typeof(T).Name}> to this Scene because it's a singleton and you should not have more than one.");
#if UNITY_EDITOR
UnityEditor.Selection.activeObject = component;
#endif
return component;
}
component = new GameObject(gameObjectName, typeof(T)).GetComponent();
GameObject go = component.gameObject;
#if UNITY_EDITOR
if
(
!isSingleton &&
UnityEditor.Selection.activeObject != null &&
UnityEditor.Selection.activeObject is GameObject parent
)
{
UnityEditor.GameObjectUtility.SetParentAndAlign(go, parent.gameObject);
RectTransform parentRT = parent.GetComponent();
if (parentRT != null)
{
RectTransform childRT = go.GetComponent();
if (childRT == null) childRT = go.AddComponent();
childRT
.ResetLocalScaleToOne()
.ResetLocalPosition()
.ResetAnchoredPosition3D();
}
}
UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Created " + gameObjectName);
if (selectGameObjectAfterCreation) UnityEditor.Selection.activeObject = go;
#endif
return component;
}
}
}