yyl
2025-05-30 d18d4a9dacd9557f2bc0b4cf141e99374406b4dc
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
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;
 
#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;
        }
    }
    
 
 
    public void Init()
    {
 
    }
 
    public void Release()
    {
 
    }
    
    private string GetExtension(Type type)
    {
        if (type == typeof(GameObject))
            return ".prefab";
        else if (type == typeof(Sprite))
            return ".png";
        else if (type == typeof(Texture2D))
            return ".png";
        else if (type == typeof(Shader))
            return ".shader";
        else if (type == typeof(TextAsset))
            return ".txt";
        else if (type == typeof(AudioClip))
            return ".wav";
        else if (type == typeof(Font))
            return ".ttf";
        else if (type == typeof(Material))
            return ".mat";
        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;
#if UNITY_EDITOR
        var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", GetExtension(typeof(T)));
 
        //  找不到文件时,尝试在内部继续找
        if (!File.Exists(path))
        {
            path = GetRelativePath(FindFilePath(directory, name + GetExtension(typeof(T))));
 
            if (string.IsNullOrEmpty(path))
            {
                Debug.LogErrorFormat("LoadAsset() => 加载不到资源: {1} {0}." + path, name, directory);
                return asset;
            }
        }
 
        asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
#else
        //TODO YYL ASSET BUNDLE部分要重写处理一下
        if (prefabBundle == null)
        {
            // string _path = GetAssetFilePath("builtin/prefabs");
            string _path = GetAssetFilePath($"builtin/{directory}");
            prefabBundle = AssetBundle.LoadFromFile(_path);
        }
        asset = prefabBundle.LoadAsset(name) as t;
#endif
        if (asset == null)
        {
            Debug.LogErrorFormat("LoadAsset() => 加载不到资源: {0}.", name);
        }
 
        return asset;
    }
 
 
 
    public void UnloadAsset(string assetBundleName)
    {
        
    }
 
    public string GetAssetFilePath(string _assetKey)
    {
        var path = Path.Combine(ExternalStorePath, _assetKey);
        if (!File.Exists(path))
        {
            path = Path.Combine(StreamingAssetPath, _assetKey);
        }
 
        return path;
    }
 
 
}