Ejemplo n.º 1
0
        /*
         * protected void CreateUiGameObject(RenderContext renderContext, [CanBeNull] ref GameObject selfObject, GameObject parentObject)
         * {
         *  selfObject = new GameObject(name);
         *  selfObject.AddComponent<RectTransform>();
         *  ElementUtil.SetLayer(selfObject, Layer);
         *  if (Active != null) selfObject.SetActive(Active.Value);
         * }
         */

        /// <summary>
        ///     Objectの生成か再利用 親子関係の設定、Layer、Activeの設定
        /// </summary>
        /// <param name="renderContext"></param>
        /// <param name="selfObject"></param>
        /// <param name="parentObject"></param>
        /// <returns></returns>
        protected void GetOrCreateSelfObject(RenderContext renderContext, ref GameObject selfObject,
                                             GameObject parentObject)
        {
            // 指定のオブジェクトがある場合は生成・取得せずそのまま使用する
            if (selfObject == null)
            {
                selfObject = ElementUtil.GetOrCreateGameObject(renderContext, Guid, name, parentObject);
            }

            var rect = ElementUtil.GetOrAddComponent <RectTransform>(selfObject);

            if (parentObject)
            {
                var nearestPrefabInstanceRoot = PrefabUtility.GetNearestPrefabInstanceRoot(selfObject);
                if (nearestPrefabInstanceRoot == null)
                {
                    // 子供の並び順を整えるため、親からはずしまたくっつける
                    rect.SetParent(null);
                    rect.SetParent(parentObject.transform);
                }

                if (rect.parent == null)
                {
                    Debug.LogError("使用されないオブジェクトが作成された");
                }
            }

            if (renderContext.OptionAddXdGuidComponent)
            {
                ElementUtil.SetGuid(selfObject, Guid);
            }
            ElementUtil.SetActive(selfObject, Active);
            ElementUtil.SetLayer(selfObject, Layer);
        }