-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNewItemLoader.cs
More file actions
113 lines (89 loc) · 3 KB
/
Copy pathNewItemLoader.cs
File metadata and controls
113 lines (89 loc) · 3 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using ItemStatsSystem;
using UnityEngine;
namespace Loader
{
public class ModBehaviour : Duckov.Modding.ModBehaviour
{
public bool isinit;
public Harmony Harmony;
void Awake()
{
Debug.Log("DisplayItemValue Loaded!!!");
}
void OnDestroy()
{
}
void OnEnable()
{
LoadAllPrefabsFromBundleAsync();
}
void OnDisable()
{
// Harmony.UnpatchAll("fsefiewfse1");
}
async Task LoadAllPrefabsFromBundleAsync()
{
// 获取 mod DLL 路径
string dllPath = Assembly.GetExecutingAssembly().Location;
string modDirectory = Path.GetDirectoryName(dllPath);
// 假设 AssetBundle 名为 "examplebundle"
string assetBundlePath = Path.Combine(modDirectory, "itemmod");
if (!File.Exists(assetBundlePath))
{
Debug.LogError("AssetBundle 不存在: " + assetBundlePath);
return;
}
// 加载 AssetBundle 异步
var bundleRequest = AssetBundle.LoadFromFileAsync(assetBundlePath);
await AwaitUntilDone(bundleRequest);
AssetBundle bundle = bundleRequest.assetBundle;
if (bundle == null)
{
Debug.LogError("AssetBundle 加载失败");
return;
}
// 加载所有 GameObject 类型的资源(即 prefab)
var loadAllRequest = bundle.LoadAllAssetsAsync(typeof(GameObject));
await AwaitUntilDone(loadAllRequest);
UnityEngine.Object[] loadedAssets = loadAllRequest.allAssets;
if (loadedAssets == null || loadedAssets.Length == 0)
{
Debug.LogWarning("AssetBundle 中没有找到任何 prefab");
return;
}
foreach (var obj in loadedAssets)
{
GameObject prefab = obj as GameObject;
if (prefab == null) continue;
Item itemComponent = prefab.GetComponent<Item>();
if (itemComponent != null)
{
if(ItemAssetsCollection.AddDynamicEntry(itemComponent))
{
Debug.Log("AddDynamicEntry Item : " + itemComponent.DisplayName);
}
Debug.Log("Load Item : " + itemComponent.DisplayName);
}
else
{
Debug.Log("Prefab '" + prefab.name + "' 没有 Item 组件");
}
}
}
private async Task AwaitUntilDone(AsyncOperation asyncOp)
{
while (!asyncOp.isDone)
{
await Task.Yield();
}
}
}
}