Commit 3e267a81 authored by hex's avatar hex

Optimized GameTextureManager

parent a992169d
Pipeline #39190 failed
...@@ -367,7 +367,13 @@ public class Program : MonoBehaviour ...@@ -367,7 +367,13 @@ public class Program : MonoBehaviour
InterString.initialize("config/translation.conf"); InterString.initialize("config/translation.conf");
// 显示一个简单的加载提示 // 显示一个简单的加载提示
PrintToChat(InterString.Get("正在加载本地数据...")); PrintToChat(InterString.Get("正在加载本地数据..."));
// GameTextureManager.initialize();
if (!GameTextureManager.IsInitialized)
{
GameTextureManager.InitializeAssets(); // 1. 同步加载核心资源
GameTextureManager.StartBackgroundThread(); // 2. 启动后台图片加载线程
}
Config.initialize("config/config.conf"); Config.initialize("config/config.conf");
// [新增] 封装文件加载逻辑,避免重复检查 // [新增] 封装文件加载逻辑,避免重复检查
...@@ -1205,6 +1211,39 @@ public class Program : MonoBehaviour ...@@ -1205,6 +1211,39 @@ public class Program : MonoBehaviour
Resources.UnloadUnusedAssets(); Resources.UnloadUnusedAssets();
onRESIZED(); onRESIZED();
} }
if (GameTextureManager.IsInitialized)
{
// 1. 检查并处理下载请求
if (GameTextureManager.HasDownloadRequests())
{
var request = GameTextureManager.GetNextDownloadRequest();
if(!string.IsNullOrEmpty(request.Url))
{
// Program 自身就是 MonoBehaviour,所以可以直接启动协程
StartCoroutine(DownloadAndProcessFile(request));
}
}
// 2. 检查并处理纹理创建任务 (分帧处理避免卡顿)
int tasksProcessedThisFrame = 0;
int maxTasksPerFrame = 5;
while (GameTextureManager.HasMainThreadTasks() && tasksProcessedThisFrame < maxTasksPerFrame)
{
var resource = GameTextureManager.GetNextMainThreadTask();
if (resource != null)
{
GameTextureManager.CreateTextureFromResource(resource);
tasksProcessedThisFrame++;
}
else
{
break;
}
}
}
fixALLcamerasPreFrame(); fixALLcamerasPreFrame();
wheelValue = UICamera.GetAxis("Mouse ScrollWheel") * 50; wheelValue = UICamera.GetAxis("Mouse ScrollWheel") * 50;
pointedGameObject = null; pointedGameObject = null;
...@@ -1269,6 +1308,15 @@ public class Program : MonoBehaviour ...@@ -1269,6 +1308,15 @@ public class Program : MonoBehaviour
} }
} }
// NEW: 这个辅助协程也从 Runner 移到了这里
private static IEnumerator DownloadAndProcessFile(GameTextureManager.DownloadRequest request)
{
yield return UnityFileDownloader.DownloadFileAsync(request.Url, request.FilePath, (success) =>
{
GameTextureManager.OnDownloadComplete(success, request);
});
}
private void onRESIZED() private void onRESIZED()
{ {
preWid = Screen.width; preWid = Screen.width;
...@@ -1330,6 +1378,10 @@ public class Program : MonoBehaviour ...@@ -1330,6 +1378,10 @@ public class Program : MonoBehaviour
{ {
servants[i].OnQuit(); servants[i].OnQuit();
} }
// 在应用退出时,优雅地关闭 GameTextureManager 的后台线程
GameTextureManager.Shutdown();
Running = false; Running = false;
try try
{ {
......
using System.Collections;
using UnityEngine;
/// <summary>
/// 这是一个 MonoBehaviour 帮助器,专为静态的 GameTextureManager 服务。
/// 它的职责是在主线程上执行任务,例如轮询下载队列和启动协程。
/// 这个类的实例由 GameTextureManager 通过 [RuntimeInitializeOnLoadMethod] 自动创建。
/// </summary>
public class GameTextureManagerRunner : MonoBehaviour
{
void Update()
{
// 在主线程的每一帧检查是否有待处理的下载请求
if (GameTextureManager.HasDownloadRequests())
{
// 从 GameTextureManager 获取一个请求
var request = GameTextureManager.GetNextDownloadRequest();
// 使用这个 MonoBehaviour 实例来启动下载协程
StartCoroutine(GameTextureManager.DownloadAndProcessFile(request));
}
}
}
fileFormatVersion: 2
guid: cb7d20dd5a7074bc089a6c75c87e34da
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -16,10 +16,20 @@ public class UnityFileDownloader ...@@ -16,10 +16,20 @@ public class UnityFileDownloader
Action<float> onProgress = null Action<float> onProgress = null
) )
{ {
string directoryPath = Path.GetDirectoryName(filePath); // 确保目录存在
if (!Directory.Exists(directoryPath)) try
{ {
Directory.CreateDirectory(directoryPath); string directoryPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
catch (Exception e)
{
Debug.LogError("Failed to create directory for " + filePath + ". Error: " + e.Message);
if (onComplete != null) onComplete.Invoke(false);
yield break; // 提前退出协程
} }
string tempFilePath = filePath + ".tmp"; string tempFilePath = filePath + ".tmp";
...@@ -28,6 +38,8 @@ public class UnityFileDownloader ...@@ -28,6 +38,8 @@ public class UnityFileDownloader
File.Delete(tempFilePath); File.Delete(tempFilePath);
} }
Debug.Log(string.Format("Downloading: {0} -> {1}", url, filePath));
using (UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) using (UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
{ {
uwr.downloadHandler = new DownloadHandlerFile(tempFilePath); uwr.downloadHandler = new DownloadHandlerFile(tempFilePath);
...@@ -80,15 +92,15 @@ public class UnityFileDownloader ...@@ -80,15 +92,15 @@ public class UnityFileDownloader
switch (extension) switch (extension)
{ {
case ".png": case ".png":
return 20; return 40;
case ".jpg": case ".jpg":
return 20; return 40;
case ".cdb": case ".cdb":
return 50; return 80;
case ".conf": case ".conf":
return 20; return 40;
default: default:
return 20; return 40;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment