-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectoryDivider.cs
121 lines (110 loc) · 3.2 KB
/
DirectoryDivider.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Linq;
using UnityEditor;
public class DirectoryDivider : MonoBehaviour
{
[SerializeField]
string baseDirectoryPath;
[SerializeField]
DirecotryDivideInfo[] destDirectories;
[SerializeField]
string[] targetExtensions;
[ContextMenu("Copy")]
public void Copy()
{
Action<string, string> action = (string from, string to) =>
{
File.Copy(from, to);
};
print("called");
Process(action);
}
[ContextMenu("Move")]
public void Move()
{
Action<string, string> action = (string from, string to) =>
{
File.Move(from, to);
};
Process(action);
}
void Process(Action<string, string> action)
{
if (string.IsNullOrEmpty(baseDirectoryPath) || !Directory.Exists(baseDirectoryPath))
{
Debug.LogError("missing baseDirectory");
return;
}
for (var i = 0; i < destDirectories.Length; i++)
{
if (string.IsNullOrEmpty(destDirectories[i].Path) || !Directory.Exists(destDirectories[i].Path))
{
Debug.LogError("missing destDirectory : " + i);
return;
}
}
AdjustRatio();
var files = Directory.GetFiles(baseDirectoryPath);
print(files.Length);
for (var i = 0; i < destDirectories.Length; i++)
{
destDirectories[i].FileNUM = Mathf.FloorToInt((float)files.Length * destDirectories[i].Ratio);
print(destDirectories[i].FileNUM);
}
files = EUtils.Shuffle(files);
var directoryIndex = 0;
var num = 0;
for (var i = 0; i < files.Length; i++)
{
var extension = Path.GetExtension(files[i]);
if (targetExtensions.Length > 0)
{
if (!targetExtensions.Contains(extension))
{
continue;
}
}
var directory = destDirectories[directoryIndex];
action(files[i], Path.Combine(directory.Path, (num + 1) + extension));
var progress = (float)i / files.Length;
EditorUtility.DisplayCancelableProgressBar("Process", "processing...", progress);
num++;
if (num >= directory.FileNUM)
{
directoryIndex++;
num = 0;
if (directoryIndex >= destDirectories.Length)
{
break;
}
}
}
EditorUtility.ClearProgressBar();
print("complete");
}
[ContextMenu("AdjustRatio")]
void AdjustRatio()
{
var total = destDirectories.Sum(e => e.Ratio);
for (var i = 0; i < destDirectories.Length; i++)
{
destDirectories[i].Ratio = destDirectories[i].Ratio / total;
}
}
[Serializable]
public struct DirecotryDivideInfo
{
public string Path;
[SerializeField]
[Range(0, 1)]
public float Ratio;
[HideInInspector]
public int FileNUM;
}
}
#endif