-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVT0BuildProcess.cs
More file actions
64 lines (59 loc) · 2.17 KB
/
VT0BuildProcess.cs
File metadata and controls
64 lines (59 loc) · 2.17 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
using System.Text.RegularExpressions;
using UnityEditor.Build.Reporting;
namespace VT0
{
/// <summary>
/// Moves VT0 textures to the Resources folder before a build, so the engine
/// can find them at runtime.
/// </summary>
public class VT0BuildProcess : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder => 0;
private List<ValueTuple<string, string>> _movedFiles;
public void OnPreprocessBuild(BuildReport report)
{
const string resourcesDir = "Assets/Resources/" + TextureData.ResourcesDir;
Directory.CreateDirectory(resourcesDir);
_movedFiles = new List<ValueTuple<string, string>>();
foreach (var oldPath in EditorBuildSettings.scenes
.SelectMany(s => AssetDatabase.GetDependencies(s.path, true))
.Where(IsVT0Placeholder)
.Distinct())
{
var obj = AssetDatabase.LoadAssetAtPath<Texture2D>(oldPath);
var fileName = obj.GetImageHash().ToString("x8");
if (Directory.GetFiles(resourcesDir, fileName + ".*").Length == 0)
{
var newPath = resourcesDir + "/" + fileName;
File.Move(oldPath, newPath);
File.Move(oldPath + ".meta", newPath + ".meta");
_movedFiles.Add(ValueTuple.Create(oldPath, newPath));
}
}
AssetDatabase.Refresh();
}
public void OnPostprocessBuild(BuildReport report)
{
foreach (var pair in _movedFiles)
{
File.Move(pair.Item2, pair.Item1);
File.Move(pair.Item2 + ".meta", pair.Item1 + ".meta");
}
_movedFiles.Clear();
AssetDatabase.Refresh();
}
private bool IsVT0Placeholder(string path)
{
return Path.GetDirectoryName(path).Replace('\\', '/') == VT0Drawer.PlaceholderLocation;
}
}
}
#endif