BattlehubCode / RTE_Docs

This is a repository for Runtime Editor documentation, discussion and tracking features and issues.
https://assetstore.unity.com/packages/tools/modeling/runtime-editor-64806
11 stars 0 forks source link

is there a way to select a root gameobject as the parent for everything? #69

Closed joaquingrech closed 1 year ago

joaquingrech commented 1 year ago

Is there a way to select a root gameobject as the parent for everything you do in the editor? This means that if I create anything new, duplicate, or whatever, instead of being at the root of the unity hierarchy, it would all be under my "root" gameobject.

BattlehubCode commented 1 year ago

This is a little bit hacky, and I didn't test it too much, but seems like following approach will work:

  1. Create following script:
using Battlehub.RTCommon;
using UnityEngine;

namespace Battlehub.RTEditor.Examples
{
    public class SetObjectRoot : EditorExtension
    {
        private IRTE m_rte;

        [SerializeField]
        private GameObject m_root;

        protected override void OnInit()
        {
            base.OnInit();
            m_rte = IOC.Resolve<IRTE>();
            m_rte.Object.Started += Object_Started; 
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            m_rte.Object.Started -= Object_Started;
        }

        private void Object_Started(ExposeToEditor obj)
        {
            if (obj.transform.parent == null)
            {
                obj.transform.SetParent(m_root.transform, true);
            }
        }
    }
}
  1. Create and use HierarcyRoot tag as following:

image

image