A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.IO;
3using UnityEditor;
4using UnityEngine;
5
6namespace UnityEditor.Rendering.Utilities
7{
8 /// <summary>
9 /// Editor SampleUtilities utility class.
10 /// </summary>
11 public static class SampleUtilities
12 {
13 /// <summary>
14 /// Copy the files in the dictionary in the parentFolderName
15 /// Do not include "/" in parentFolderName
16 /// filesToImport contains the remove filepath in the Key, and the local folder path in the Value, end it with a slash
17 /// Example:
18 /// Key: "Packages/com.unity.render-pipelines.core/Samples~/Common/Models/UnityMaterialBall.fbx"
19 /// Value: "/Models/"
20 /// </summary>
21 /// <param name="parentFolderName">The name of the folder that is created locally</param>
22 /// <param name="filesToImport">The list of files that needs to be copied</param>
23 public static void CopyFilesInFolder(string parentFolderName, Dictionary<string, string> filesToImport)
24 {
25 string path = "/" + parentFolderName;
26 if (!Directory.Exists(Application.dataPath + path))
27 {
28 Directory.CreateDirectory(Application.dataPath + path);
29 }
30
31 foreach (KeyValuePair<string, string> file in filesToImport)
32 {
33 // Retrieve the filename.
34 string[] nameArray = file.Key.Split('/');
35 string filename = nameArray[nameArray.Length - 1];
36
37 string remotePath = file.Key;
38 string localFolderPath = "Assets/" + file.Value;
39 string localFilePath = localFolderPath + filename;
40
41 // Create folders recursively if the path does not exist.
42 if (!Directory.Exists(localFolderPath))
43 Directory.CreateDirectory(localFolderPath);
44
45 // Create the file if does not exist.
46 if (!File.Exists(localFilePath))
47 {
48 FileUtil.CopyFileOrDirectory(remotePath, localFilePath);
49 FileUtil.CopyFileOrDirectory(remotePath + ".meta", localFilePath + ".meta");
50 }
51 }
52 }
53
54
55 /// <summary>
56 /// Copy the folder named Common in each of the packages mentionned
57 /// </summary>
58 /// <param name="packages">A list of packages (com.unity..) </param>
59 /// <param name="parentFolderName">The name of the folder that is created locally</param>
60 /// <param name="foldersToRemove">A list of folders to delete after import to avoid conflicts</param>
61 public static void CopyCommonSampleFolders(string[] packages, string parentFolderName, string[] foldersToRemove = null)
62 {
63 // Foreach packages listed there, we need to import the common dependency folder.
64 foreach (string package in packages)
65 {
66 // Retrieve a readable name for the package.
67 string[] nameArray = package.Split('.');
68 string folderName = nameArray[nameArray.Length - 1];
69 folderName = folderName.Substring(0, 1).ToUpper() + folderName.Substring(1).ToLower();
70
71 string path = "/" + parentFolderName + "/" + folderName;
72
73 // If parentFolderName is not there locally, create it.
74 if (!Directory.Exists(Application.dataPath + path))
75 {
76 // Import the common folder of the samples if it exists.
77 string commonFolderPath = "Packages/" + package + "/Samples~/Common";
78 if (Directory.Exists(commonFolderPath))
79 {
80 Directory.CreateDirectory(Application.dataPath + path);
81 FileUtil.CopyFileOrDirectory("Packages/" + package + "/Samples~/Common", "Assets" + path + "/Common");
82
83 // This for useless folder in a specific context or to avoid conflicts by importing multiple script folders.
84 if (foldersToRemove != null)
85 {
86 foreach(string folder in foldersToRemove)
87 {
88 string folderPath = "Assets" + path + "/Common/" + folder;
89 if (Directory.Exists(folderPath))
90 {
91 FileUtil.DeleteFileOrDirectory(folderPath);
92 FileUtil.DeleteFileOrDirectory(folderPath + ".meta");
93 }
94 }
95 }
96 }
97 }
98 }
99 }
100 }
101}