2013년 11월 4일 월요일

Unity - 유니티 엔진 - 텍스트 파일을 이용하여 에셋 번들 한꺼번에 생성 하기(게임코디 - 태풍의그라운드님)

에셋 번들을 만들어야 하는데 파일 하나하나 찍어서 만들기가 너무 귀찮았습니다.
플랫폼마다 똑같은 노가다를 반복 해야 한다고 생각하니 눈앞이 캄캄하더군요.
그래서 텍스트 파일에 리스트를 쭉 적어놓고 읽어와서 일괄 생성하게 만들었습니다.


아래와 같이 파일의 경로를 쭉 적어줍니다.
patch_list.txt 로 저장을 하고 위치는 Resources폴더 아래에 둡니다.


Resources/Sound/main.ogg
Resources/Sound/fire.wav
Resources/character/human.prefab
Resources/character/lion.prefab
Resources/ui/shop.prefab
.
.
.
[Resources/patch_list.txt의 내용]



소스코드는 Assets/Editor 밑에 xxx.cs 로 넣어놓습니다. 파일 이름은 아무거나 넣어도 됩니다.
·미리보기 | 소스복사·
  1. [MenuItem("Assets/Generate All from file(Windows)")]    // 메뉴가 들어갈 위치입니다. Assets메뉴 하위.
  2. static void GenerateAll()  
  3. {  
  4.     // 빌드 타겟은 적절하게 맞춰주면 되겠죠.  
  5.     ExportAll(BuildTarget.StandaloneWindows);  
  6. }  
  7.   
  8.   
  9.   
  10.   
  11.   
  12. static void ExportAll(BuildTarget target)    
  13. {    
  14.     // 리스트를 읽어와서 줄 단위로 분리 해 줍니다.  
  15.     TextAsset filelist_obj = Resources.Load("patch_list"typeof(TextAsset)) as TextAsset;    
  16.     string[] filelist = filelist_obj.text.Split('\n');    
  17.     
  18.   
  19.     foreach (string original_path in filelist)    
  20.     {  
  21.         // *중요함! 개행 문자를 제거 해 줍니다.  
  22.         string path = original_path.Trim('\n');    
  23.         path = path.Trim('\r');    
  24.     
  25.         if (path.Length <= 0)    
  26.         {    
  27.             continue;    
  28.         }    
  29.   
  30.         // 확장자, 경로 다 빼고 파일 이름만 쏙 빼옵니다.    
  31.         int last_split_pos = path.LastIndexOf('/') + 1;    
  32.         int extend_length = path.Length - path.LastIndexOf('.');    
  33.         string filename = path.Substring(last_split_pos, (path.Length - last_split_pos) - extend_length);    
  34.   
  35.         // 번들 파일이 생성될 경로 입니다.
  36.         // 프로젝트 상위 폴더에 "patch/(플랫폼명)" 형태의 폴더를 미리 만들어 놓아야 합니다.
  37.         // 예)
  38.         // d:/project/patch/Android    <- 번들 파일이 생성될 폴더
  39.         // d:/project/gamecodi  <- 프로젝트 폴더
  40.         // d:/project/gamecodi/Assets
  41.         string output_path = string.Format("../patch/{0}/{1}.unity3d", target.ToString(), filename);    
  42.   
  43.         // 리스트에 기입된 경로에서 오브젝트를 로드합니다.  
  44.         string included_path = "Assets/" + path;    
  45.         UnityEngine.Object obj = Resources.LoadAssetAtPath(included_path, typeof(UnityEngine.Object));    
  46.         if (obj == null)    
  47.         {    
  48.             Debug.LogError("Cannot find the resource. " + included_path);    
  49.             continue;    
  50.         }    
  51.   
  52.         // 생성!  
  53.         BuildPipeline.BuildAssetBundle(obj, null, output_path,    
  54.             BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,    
  55.             target);    
  56.     
  57.         Debug.Log("completed... : " + included_path);    
  58.     }    
  59. }    
파일 경로를 읽어온 뒤 개행 문자를 제거 해 주는것이 굉장히 중요합니다.
이것을 빼먹고 했더니 리소스 로딩이 실패나서 한참 고생했습니다.ㅠ_ㅠ

이렇게 만들어 놓고 플랫폼만 바꿔서 클릭 한번만 하면 되겠습니다.
끝~

게임코디(gamecodi.com)에서 태풍의그라운드 닉으로 활동하시는 분의 글을 퍼왔습니다.

댓글 없음:

댓글 쓰기