구글 드라이브에 zip파일 형태로 라이브러리를 올려 두고 신규 프로젝트 생성시 마다 다운 받기 위해서 작업함.
내가 제작한 라이브러리를 zip파일 형태로 만들어 구글드라이브에 올린다.
_Utility.zip(자기가 올린파일)의 오른쪽 끝에 점3개 모양->공유->링크 생성을 누른 다음 링크를 복사합니다.(나중에 씁니다)
유니티 프로젝트를 생성합니다.
Package Manager에서 Editor Coroutines를 설치합니다.
그런다음 NewProjectTemplate.cs파일을 하나 만듭니다.
파일 내용은 다음과 같이 채워넣습니다.
//! Package Manager에서 Editor Coroutines를 설치한다.
//! 구글 드라이브에 zip파일 형태로 파일을 올려 두면 다운 받아서 선택한 폴더 밑에 압축을 풀어준다. 처음 프로젝트 생성시 사용하면 좋다.
using System.Collections;
using System.IO.Compression;
using System.Text.RegularExpressions;
#if UNITY_EDITOR
using System.IO;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Networking;
public class NewProjectTemplate
{
#if UNITY_EDITOR
//구글 드라이브에서 링크를 걸고 나온 주소
private static string DriveLinkedUrl =
"링크복사한 url";
//압축 파일을 아래 이름으로 폴더를 만들어서 풀어준다.
private static string SaveDirectoryName = "_Utility";
private static GameObject Coroutineobj;
[MenuItem("New/프로젝트 템플릿 받기시작",false,10)]
// [MenuItem("♜JHS♜/Ms Excel 파일 읽어서 가공하여 서버에 올리기 %&E", false, 10)]
private static void CustomMenuItem1()
{
Coroutineobj = new GameObject("다운받는중");
EditorCoroutineUtility.StartCoroutine(DownloadZipFile(),Coroutineobj);
}
private static IEnumerator DownloadZipFile()
{
var ID = MakeRealID(DriveLinkedUrl);
var url = "https://drive.google.com/uc?export=download&id=" + ID ;
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError)
{
Debug.LogError(www.error);
}
else
{
SaveZipFile(www.downloadHandler.data);
}
}
}
private static void SaveZipFile(byte[] fileBytes)
{
Debug.Log(fileBytes.Length.ToString());
var Path = GetSelectedOrParentFolderPath();
string selectedFolder = GetSelectedOrParentFolderPath(); // 이전 답변에서 제공한 FolderUtilities 클래스의 함수
UnzipFileBytes(fileBytes, selectedFolder);
GameObject.DestroyImmediate(Coroutineobj);
Debug.Log("Coroutineobj 삭제 완료");
}
public static void GetSelectedFolderPath()
{
string path = GetSelectedOrParentFolderPath();
if (string.IsNullOrEmpty(path))
{
Debug.Log("Assets");
}
else
{
Debug.Log(path);
}
}
public static string GetSelectedOrParentFolderPath()
{
string selectedPath = AssetDatabase.GetAssetPath(Selection.activeObject);
if (string.IsNullOrEmpty(selectedPath))
{
return "Assets";
}
if (Directory.Exists(selectedPath))
{
return selectedPath;
}
else if (File.Exists(selectedPath))
{
return Directory.GetParent(selectedPath).ToString();
}
return "Assets";
}
public static void UnzipFileBytes(byte[] fileBytes, string folderPath)
{
// 바이트 배열을 임시 zip 파일로 저장
string tempZipFilePath = Path.Combine(folderPath, "temp.zip");
File.WriteAllBytes(tempZipFilePath, fileBytes);
// 임시 zip 파일 압축 해제
string extractPath = Path.Combine(folderPath, SaveDirectoryName);
Directory.CreateDirectory(extractPath); // 압축 해제할 디렉토리 생성
// ZipFile 클래스 사용 (System.IO.Compression 네임스페이스)
ZipFile.ExtractToDirectory(tempZipFilePath, extractPath);
// 임시 zip 파일 삭제
File.Delete(tempZipFilePath);
AssetDatabase.Refresh();
Debug.Log("Unzipped to: " + extractPath);
}
public static string MakeRealID(string url)
{
// 정규 표현식 패턴
string pattern = @"/d/([a-zA-Z0-9_-]+)";
Match match = Regex.Match(url, pattern);
// 패턴에 일치하는 경우
if (match.Success && match.Groups.Count > 1)
{
return match.Groups[1].Value;
}
else
{
return url;
}
}
#endif
}
유니티에디터에 New메뉴가 생겼습니다.
프로젝트 템플릿 받기시작도 생겼습니다.
우선 저장한 위치를 프로젝트탭에서 선택을 해줍니다.전 Resources폴더를 선택했습니다.
선택을 하지 않으면 Assets폴더에 압축 파일을 풉니다.
다운받는중이라는 오브젝트가 생겼다가 사라지면 다운로드가 완료 된겁니다.
완료가 된 제 프로젝트 입니다.