-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSetAssetData.cs
More file actions
141 lines (126 loc) · 5.48 KB
/
Copy pathAutoSetAssetData.cs
File metadata and controls
141 lines (126 loc) · 5.48 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#if UNITY_EDITOR
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
namespace RPGMaker.Codebase.Addon.ponApp.VirtualPad
{
public class AutoSetAssetData
{
private static AddressableAssetSettings _settings;
[InitializeOnLoadMethod]
private static void Initialize() {
SetAssetAddressProcess();
}
// アセットをアドレス登録する
private static bool SetAssetAddressProcess() {
AssetDatabase.StartAssetEditing();
bool isFirstCreate = false;
if (!Directory.Exists("Assets/AddressableAssetsData"))
{
_settings = AddressableAssetSettings.Create("Assets/AddressableAssetsData", "AddressableAssetSettings", true, true);
isFirstCreate = true;
}
else
_settings = AssetDatabase.LoadAssetAtPath<AddressableAssetSettings>("Assets/AddressableAssetsData/AddressableAssetSettings.asset");
if (_settings == null)
{
AssetDatabase.StopAssetEditing();
return false;
}
AddressableAssetSettingsDefaultObject.Settings = _settings;
string[] pathAdd =
{
"Assets/RPGMaker/Codebase/Add-ons/ponAppVirtualPad/Sprites",
"Assets/RPGMaker/Codebase/Add-ons/ponAppVirtualPad/InputAction"
};
string[] extensionAdd =
{
"*.png",
"*.inputactions"
};
bool result = true;
for (var i = 0; i < pathAdd.Length; i++)
{
if (!System.IO.Directory.Exists(pathAdd[i])) continue;
try
{
var data_path = Directory.GetFiles(pathAdd[i], extensionAdd[i]);
for (var i2 = 0; i2 < data_path.Length; i2++)
{
string path = data_path[i2].Replace("\\", "/");
int start = path.LastIndexOf("/", path.LastIndexOf("/") - 1) + 1;
string pathName = path.Substring(start);
AddressableAssetGroup targetParent;
targetParent = GetOrCreateGroup("ponapptddungeon");
if (!SetAddressToAsset(path, pathName, targetParent)) result = false;
}
}
catch (IOException)
{
result = false;
}
}
if (isFirstCreate)
GetOrCreateGroup("Asset_end");
AssetDatabase.StopAssetEditing();
return result;
}
// 指定された名前のグループを取得もしくは作成します
private static AddressableAssetGroup GetOrCreateGroup(string groupName) {
var groups = _settings.groups;
AddressableAssetGroup group = null;
for (int i = 0; i < groups.Count; i++)
if (groups[i].name == groupName)
{
group = groups[i];
break;
}
// 既に指定された名前のグループが存在する場合は
// そのグループを返します
if (group != null) return group;
// Content Packing & Loading
var bunlAssetGroupSchema = ScriptableObject.CreateInstance<BundledAssetGroupSchema>();
bunlAssetGroupSchema.BundleMode = BundledAssetGroupSchema.BundlePackingMode.PackSeparately;
// Content Update Restriction
var contentUpdateGroupSchema = ScriptableObject.CreateInstance<ContentUpdateGroupSchema>();
// AddressableAssetGroup の Inspector に表示されている Schema
var schemas = new List<AddressableAssetGroupSchema>
{
bunlAssetGroupSchema,
contentUpdateGroupSchema
};
// 指定された名前のグループを作成して返します
return _settings.CreateGroup(groupName, false, false,
true, schemas);
}
// 指定されたアセットにアドレスを割り当ててグループに追加します
private static bool SetAddressToAsset(string path, string pathName, AddressableAssetGroup targetParent) {
var guid = AssetDatabase.AssetPathToGUID(path);
if (guid == "")
{
AssetDatabase.Refresh();
guid = AssetDatabase.AssetPathToGUID(path);
}
var entry = _settings.FindAssetEntry(guid);
if (entry != null)
// すでに同じアドレスかグループが設定されている場合処理をスキップします
if (entry.address == pathName || entry.parentGroup == targetParent)
return true;
// アセットをグループに追加します
entry = _settings.CreateOrMoveEntry(guid, targetParent);
if (entry == null)
{
Debug.LogError("[AddressableUtils] Failed AddressableAssetSettings.CreateOrMoveEntry.\n" + path);
return false;
}
// アセットにアドレスを割り当てます
entry.SetAddress(pathName);
return true;
}
}
}
#endif