반응형
Notice
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Do Something IT

[Unity Custom MenuItem] 계층뷰 상의 여러개 프리펩 오브젝트들을 한번에 Apply 하기 본문

Unity3D/Editor

[Unity Custom MenuItem] 계층뷰 상의 여러개 프리펩 오브젝트들을 한번에 Apply 하기

아낙시만더 2016. 10. 31. 14:50
반응형

계층뷰 상의 여러개의 프리펩 오브젝트들을 한번 Apply 하는 것을 제작 하였습니다.


사용방법은 아래와 같습니다.

1. 계층 뷰에서 갱신할 프리펩 오브젝트들을 선택합니다.

본문 이미지 1

2. 마우스 오른쪽을 클릭하여 CustomMenu > Apply Prefabs를 클릭하면 여러개의 프리펩

오브젝트들이 원클릭에 갱신이 실행됩니다.

본문 이미지 2

잘못 선택된 프리펩이아닌 오브젝트는 무시하고
부모가 아닌 프리펩의 차일들을 선택 되었을때 부모를 찾아 중복 체크를 하여 하나만 갱신됩니다.

PS. 보너스로 JGUI도 마우스 오른쪽 클릭 메뉴 아이템으로 옮겨 두었습니다.


  
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class csPrefabApplyer
{
    [MenuItem("GameObject/CustomMenu/Apply Prefabs", false, 0)]
    static void Init(MenuCommand menuCommand)
    {
        if (Selection.objects.Length > 0)
        {
            if (menuCommand.context == Selection.objects[0])
            {
                GameObject[] prefabs = Selection.gameObjects;

                Dictionary _prefabDicList = new Dictionary();
                for (int i = 0; i < prefabs.Length; i++)
                {
                    // 프리펩이 아닌 오브젝트는 무시
                    if (PrefabUtility.GetPrefabParent(prefabs[i]) != null)
                    {
                        // 최상위 부모가 아닐때 최상위 부모를 검색하여 추가
                        GameObject root = PrefabUtility.FindRootGameObjectWithSameParentPrefab(prefabs[i]);
                        if (!_prefabDicList.ContainsKey(root.name))
                        {
                            _prefabDicList.Add(root.name, root);
                        }
                    }
                    else if (PrefabUtility.GetPrefabParent(prefabs[i]) == null && PrefabUtility.GetPrefabObject(prefabs[i]) != null)
                    {
                        // 최상위 부모일때 추가
                        GameObject target = PrefabUtility.GetPrefabObject(prefabs[i]) as GameObject;
                        if (!_prefabDicList.ContainsKey(target.name))
                        {
                            _prefabDicList.Add(target.name, target);
                        }
                    }
                }

                // 갱신
                foreach (KeyValuePair prefab in _prefabDicList)
                {
                    PrefabUtility.ReplacePrefab(prefab.Value, PrefabUtility.GetPrefabParent(prefab.Value), ReplacePrefabOptions.ConnectToPrefab);
                }
                return;
            }
        }
    }
}


반응형
Comments