yyl
2025-06-05 dfe8aab7ddab3560c12bc8b59b246969c5167535
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using System;
using UnityEngine.U2D;
using LitJson;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.Video;
 
 
#if UNITY_EDITOR
using UnityEditor;
#endif
 
using LaunchCommon;
 
public class ResManager : Singleton<ResManager>
{
    
 
    //不下载时本地安卓测试路径
    // public string assetBundlesPath = Application.dataPath + "/../AssetBundles/Android/";
    // public static string bytesFolderName = "logicbytes/";
    public string StreamingAssetPath
    {
        get
        {
            return ResourcesPath.Instance.StreamingAssetPath;
        }
    }
    public string ExternalStorePath 
    {
        get
        {
            return ResourcesPath.Instance.ExternalStorePath;
        }
    }
 
    //用于editor 下的资源路径
    public string ResourcesOutPath 
    {
        get
        {
            return ResourcesPath.ResourcesOutPath;
        }
    }
 
    public string CONFIG_FODLER
    {
        get
        {
            return ResourcesPath.CONFIG_FODLER;
        }
    }
 
    public string ResourcesOutAssetPath
    {
        get
        {
            return ResourcesPath.ResourcesOutAssetPath;
        }
    }
    
    private static readonly Dictionary<Type, string> fileExtensionDict = new Dictionary<Type, string>()
    {
        {typeof(GameObject), "prefab"},
        {typeof(Sprite), "png"},
        {typeof(Texture2D), "png"},
        {typeof(Shader), "shader"},
        {typeof(TextAsset), "txt"},
        {typeof(AudioClip), "wav"},
        {typeof(Font), "ttf"},
        {typeof(Material), "mat"},
        {typeof(VideoClip), "mp4"},
        {typeof(SpriteAtlas), "spriteatlasv2"},
    };
 
 
    public void Init()
    {
 
    }
 
    public void Release()
    {
 
    }
    
    private string GetExtension(Type type)
    {
        if (fileExtensionDict.TryGetValue(type, out string extension))
            return "." + extension;
        else
        {
            Debug.LogErrorFormat("GetExtension() => 不支持的资源类型: {0}.", type.Name);
            return "";
        }
    }
 
#if UNITY_EDITOR
    public string GetFullDirectory(string directory)
    {
        string fullDirectory = Path.Combine(ResourcesOutPath, directory);
        return fullDirectory;
    }
 
    public string FindFilePath(string directory, string fileName)
    {
        string[] files = Directory.GetFiles(GetFullDirectory(directory), fileName, SearchOption.AllDirectories);
        if (files.Length > 0)
        {
            return files[0];
        }
        return string.Empty;
    }
 
    public string GetRelativePath(string absolutePath)
    {
        string relativePath = absolutePath.Replace(ResourcesOutPath, "Assets/ResourcesOut/");
        return relativePath;
    }
#endif
 
    public T LoadAsset<T> (string directory, string name) where T : UnityEngine.Object
    {
        T asset = null;
        //  特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字
        if (typeof(T) == typeof(Sprite))
        {
            return LoadSprite(directory, name) as T;
        }
 
        return LoadAssetInternal<T>(directory, name);
    }
 
    private T LoadAssetInternal<T>(string directory, string name) where T : UnityEngine.Object
    {
        T asset = null;
 
        var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", GetExtension(typeof(T))).Replace("//", "/").Trim().Replace("\\", "/");
 
        if (AssetSource.uiFromEditor)
        {
#if UNITY_EDITOR
            asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
#endif
        }
        else
        {
            var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
            asset = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo, typeof(T)) as T;
        }
 
        if (asset == null)
        {
            Debug.LogErrorFormat("LoadAsset() => 加载不到资源: {0}", path);
        }
 
        return asset;
    }
 
    private Sprite LoadSprite(string atlasName, string spriteName)
    {
// #if !UNITY_EDITOR
        SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", ""));
        return atlas.GetSprite(spriteName);
// #else
        // return ResManager.Instance.LoadAssetInternal<Sprite>(atlasName, spriteName);
// #endif
    }
 
    public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
    {
        //  特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字
        if (typeof(T) == typeof(Sprite))
        {
            LoadSpriteAsync<T>(directory, name, callBack);
            return;
        }
 
        LoadAssetAsyncInternal<T>(directory, name, callBack);
    }
 
    private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
    {
#if !UNITY_EDITOR
        LoadAssetAsync<SpriteAtlas>(atlasName, spriteName, (isLoaded, atlas) => {
            if (isLoaded)
            {
                callBack?.Invoke(isLoaded, atlas.GetSprite(spriteName));
            }
            else
            {
                callBack?.Invoke(false, null);
            }
        });
#else
        //  编辑器下可以直接加载没啥问题
        LoadAssetAsyncInternal<T>(atlasName, spriteName, callBack);
#endif
    }
 
    private void LoadAssetAsyncInternal<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
    {
        var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", GetExtension(typeof(T))).Replace("//", "/");
 
        if (AssetSource.uiFromEditor)
        {
#if UNITY_EDITOR
            var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
            callBack?.Invoke(asset != null, asset);
#endif
        }
        else
        {
            var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
            AssetBundleUtility.Instance.Co_LoadAsset(assetInfo, callBack);
        }
    }
 
    public void UnloadAsset(string assetBundleName, string assetName)
    {
        if (AssetSource.uiFromEditor)
            return;
 
        AssetBundleUtility.Instance.UnloadAsset(assetBundleName, assetName);
    }
 
    public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects, bool includeDependenice)
    {
        if (AssetSource.uiFromEditor)
            return;
        AssetBundleUtility.Instance.UnloadAssetBundle(assetBundleName, unloadAllLoadedObjects, includeDependenice);
    }
 
    public string GetAssetFilePath(string _assetKey)
    {
        var path = Path.Combine(ExternalStorePath, _assetKey);
        if (!File.Exists(path))
        {
            path = Path.Combine(StreamingAssetPath, _assetKey);
        }
 
        return path;
    }
 
 
}