/* * 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); }
public void Create(ref GameObject targetObject, RenderContext renderContext, Dictionary <string, object> rootJson) { if (EditorApplication.isPlaying) { EditorApplication.isPlaying = false; } var rootElement = ElementFactory.Generate(rootJson, null); rootElement.Render(ref targetObject, renderContext, null); if (renderContext.ToggleGroupMap.Count > 0) { // ToggleGroupが作成された場合 var go = new GameObject("ToggleGroup"); go.transform.SetParent(targetObject.transform); foreach (var keyValuePair in renderContext.ToggleGroupMap) { var gameObject = keyValuePair.Value; gameObject.transform.SetParent(go.transform); } } // 使われなかったオブジェクトの退避グループ var notUsedGroupObject = ElementUtil.GetOrCreateGameObject(renderContext, null, $"[{Importer.NAME}] Unused", null); var notUsedchilds = renderContext.FreeChildObjects; if (notUsedchilds.Count > 0) { var notUsedGroupRect = ElementUtil.GetOrAddComponent <RectTransform>(notUsedGroupObject); foreach (var keyValuePair in notUsedchilds) { var go = keyValuePair.Key; if (go != null) { if (PrefabUtility.GetNearestPrefabInstanceRoot(go) == null) { // NestedPrefabの子供でなければNotUsedグループに移動できる var goRect = go.GetComponent <RectTransform>(); if (goRect != null) { goRect.SetParent(notUsedGroupRect); } } else { // NestedPrefabの子供を転送しようとするとエラーになるため、 // 非アクティブにする //TODO: unusedといった名前をつけるか検討 go.SetActive(false); } } else { // Debug.Log("既に廃棄されています"); } } // not used グループに子供があった場合のみ移動 if (notUsedGroupRect.childCount > 0) { notUsedGroupRect.SetParent(targetObject.transform); notUsedGroupObject.SetActive(false); } } else { Object.DestroyImmediate(notUsedGroupObject); } foreach (var prefab in renderContext.NewPrefabs.ToList()) { //if we haven't created a prefab out of the referenced GO we do so now if (PrefabUtility.GetPrefabAssetType(prefab) == PrefabAssetType.NotAPrefab) { //TODO: Ugly path generation var nestedPrefabDirectory = Path.Combine(Application.dataPath.Replace("Assets", ""), Path.Combine(Path.Combine(EditorUtil.GetOutputPrefabsFolderAssetPath()), "Components")); if (!Directory.Exists(nestedPrefabDirectory)) { Directory.CreateDirectory(nestedPrefabDirectory); } nestedPrefabs.Add(PrefabUtility.SaveAsPrefabAssetAndConnect(prefab, Path.Combine(nestedPrefabDirectory, prefab.name + ".prefab"), InteractionMode.AutomatedAction)); } } }