Skip to content

Commit 532c6df

Browse files
committed
修复下载
1 parent 4fde696 commit 532c6df

File tree

10 files changed

+116
-158
lines changed

10 files changed

+116
-158
lines changed

Assets/Live2D/Cubism/Framework/Expression/CubismExpressionData.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ public static CubismExpressionData CreateInstance(CubismExpressionData expressio
7474
expressionData.Type = json.Type;
7575
expressionData.FadeInTime = json.FadeInTime;
7676
expressionData.FadeOutTime = json.FadeOutTime;
77-
expressionData.Parameters = new SerializableExpressionParameter[json.Parameters.Length];
78-
77+
if (json.Parameters != null) {
78+
expressionData.Parameters = new SerializableExpressionParameter[json.Parameters.Length];
79+
}
80+
else {
81+
return expressionData;
82+
}
83+
7984
for(var i = 0; i < json.Parameters.Length; ++i)
8085
{
8186
expressionData.Parameters[i].Id = json.Parameters[i].Id;

Assets/Scenes/SampleScene.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:309d7dcaec187662a0657dc5a35fa6f2a06aacceba01173ef0aa9f431bf8cac4
3-
size 418487
2+
oid sha256:d94453170ccc59a5cbce3bf8178b82cd0a138f2922a98ae48bc6f704e9f5a3ac
3+
size 428328

Assets/Scripts/Http/AvatarManager.cs

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,12 @@ public class AvatarManager : MonoBehaviour
99
public Image avatarImage; // 在 Inspector 中绑定的 Image 组件
1010
public int width = 128;
1111
public int height = 128;
12-
// 静态字典,存储人名和对应的 URL
13-
private static Dictionary<string, string> avatarUrls = new Dictionary<string, string>();
12+
private object lockObj = new ();
1413

15-
/// <summary>
16-
/// 添加或更新人名和 URL
17-
/// </summary>
18-
public static void AddOrUpdateAvatarUrl(string name, string url)
19-
{
20-
if (avatarUrls.ContainsKey(name))
21-
{
22-
avatarUrls[name] = url; // 更新已有条目
23-
}
24-
else
25-
{
26-
avatarUrls.Add(name, url); // 添加新条目
27-
}
28-
}
29-
30-
/// <summary>
31-
/// 删除人名和 URL
32-
/// </summary>
33-
public static void RemoveAvatarUrl(string name)
34-
{
35-
if (avatarUrls.ContainsKey(name))
36-
{
37-
avatarUrls.Remove(name);
38-
}
39-
}
40-
41-
/// <summary>
42-
/// 获取人名对应的 URL
43-
/// </summary>
44-
public static string GetAvatarUrl(string name)
45-
{
46-
if (avatarUrls.TryGetValue(name, out string url))
47-
{
48-
return url;
49-
}
50-
return null; // 未找到则返回 null
51-
}
52-
53-
/// <summary>
54-
/// 根据人名下载并设置头像图片
55-
/// </summary>
56-
public void SetAvatarByName(string name)
57-
{
58-
string url = GetAvatarUrl(name);
59-
if (url != null)
60-
{
61-
SetAvatar(url); // 使用 URL 下载并设置头像
62-
}
63-
else
64-
{
65-
Debug.LogError("未找到人名对应的 URL: " + name);
66-
}
67-
}
68-
69-
/// <summary>
70-
/// 下载并设置头像图片
71-
/// </summary>
72-
public void SetAvatar(string url, string folderPath = null)
73-
{
74-
75-
HttpDownloader.Instance.Download(url, result =>
76-
{
77-
if (result.Success)
78-
{
79-
var targetPath = result.FilePath;
80-
if (folderPath != null) {
81-
Directory.CreateDirectory(folderPath ?? "");
82-
targetPath = Path.Combine(folderPath, Path.GetFileName(result.FilePath));
83-
MoveFile(result.FilePath, targetPath);
84-
}
85-
StartCoroutine(LoadAndProcessImage(targetPath));
86-
}
87-
else
88-
{
89-
var err = "下载avatar失败: " + result.ErrorMessage;
90-
DebugWrapper.Instance.Log(err, Color.red);
91-
Debug.LogError(err);
92-
}
14+
public void SetAvatar(string url, string absolutePath)
15+
{
16+
HttpDownloader.Instance.Download(url, absolutePath, r => {
17+
StartCoroutine(LoadAndProcessImage(absolutePath));
9318
});
9419
}
9520

@@ -98,16 +23,19 @@ public void SetAvatar(string url, string folderPath = null)
9823
/// </summary>
9924
private IEnumerator LoadAndProcessImage(string filePath)
10025
{
101-
Texture2D texture = LoadTextureFromFile(filePath);
102-
if (texture == null)
103-
{
104-
Debug.LogError("无法加载图片: " + filePath);
105-
yield break;
106-
}
26+
lock (lockObj) {
27+
Texture2D texture = LoadTextureFromFile(filePath);
28+
if (texture == null)
29+
{
30+
Debug.LogError("无法加载图片: " + filePath);
31+
yield break;
32+
}
10733

108-
Texture2D resizedTexture = ResizeTexture(texture, width, height);
109-
Sprite sprite = TextureToSprite(resizedTexture);
110-
avatarImage.sprite = sprite;
34+
Texture2D resizedTexture = ResizeTexture(texture, width, height);
35+
Sprite sprite = TextureToSprite(resizedTexture);
36+
avatarImage.sprite = sprite;
37+
}
38+
11139
}
11240

11341
/// <summary>

Assets/Scripts/Http/HttpDownloader.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,15 @@ public class HttpDownloader : InitOnceSingleton<HttpDownloader>
88
{
99

1010
// 下载方法,接受 URL 和回调函数
11-
public void Download(string url, Action<DownloadResult> callback)
11+
public void Download(string url, string absolutePath, Action<DownloadResult> callback = null)
1212
{
13-
StartCoroutine(DownloadCoroutine(url, callback: callback));
14-
}
15-
16-
public void Download(string url, string fileName, Action<DownloadResult> callback)
17-
{
18-
StartCoroutine(DownloadCoroutine(url, fileName, callback));
13+
StartCoroutine(DownloadCoroutine(url, absolutePath, callback));
1914
}
2015

2116
// 下载协程,处理异步下载逻辑
22-
private IEnumerator DownloadCoroutine(string url, string fileName = null, Action<DownloadResult> callback = null)
17+
private IEnumerator DownloadCoroutine(string url, string absolutePath, Action<DownloadResult> callback = null)
2318
{
24-
// 从 URL 中提取文件名,并组合保存路径
25-
if (fileName == null) {
26-
fileName = Path.GetFileName(url);
27-
}
28-
// 下载结果存在临时文件夹
29-
string filePath = Path.Combine(Application.temporaryCachePath, "tmp", fileName);
30-
// 确保目录存在
31-
string directory = Path.GetDirectoryName(filePath);
19+
string directory = Path.GetDirectoryName(absolutePath);
3220
if (!Directory.Exists(directory))
3321
{
3422
Directory.CreateDirectory(directory);
@@ -37,13 +25,13 @@ private IEnumerator DownloadCoroutine(string url, string fileName = null, Action
3725
// 创建下载结果对象
3826
DownloadResult result = new DownloadResult();
3927

40-
// 如果文件存在,先(删除) 跳过, unity读取图片时会占用,删不掉
41-
if (File.Exists(filePath))
28+
// 如果文件存在,跳过, unity读取图片时会占用,删不掉
29+
if (File.Exists(absolutePath))
4230
{
4331
result.Success = true;
44-
result.FilePath = filePath;
45-
result.FileSize = new FileInfo(filePath).Length; // 获取文件大小
46-
// 调用回调函数,返回结果
32+
result.FilePath = absolutePath;
33+
result.FileSize = new FileInfo(absolutePath).Length; // 获取文件大小
34+
result.CompletionTime = DateTime.Now; // 记录完成时间
4735
callback?.Invoke(result);
4836
yield break;
4937
}
@@ -52,15 +40,15 @@ private IEnumerator DownloadCoroutine(string url, string fileName = null, Action
5240
using (UnityWebRequest www = UnityWebRequest.Get(url))
5341
{
5442
// 设置下载处理器,直接保存到文件
55-
www.downloadHandler = new DownloadHandlerFile(filePath);
43+
www.downloadHandler = new DownloadHandlerFile(absolutePath);
5644
yield return www.SendWebRequest();
5745

5846
// 检查下载是否成功
5947
if (www.result == UnityWebRequest.Result.Success)
6048
{
6149
result.Success = true;
62-
result.FilePath = filePath;
63-
result.FileSize = new FileInfo(filePath).Length; // 获取文件大小
50+
result.FilePath = absolutePath;
51+
result.FileSize = new FileInfo(absolutePath).Length; // 获取文件大小
6452
result.CompletionTime = DateTime.Now; // 记录完成时间
6553
}
6654
else

Assets/Scripts/Live2D/DynamicExpressionSetup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ private void LoadExpressions()
4444

4545
for (int i = 0; i < expressions.Length; i++)
4646
{
47-
var exp3JsonString = File.ReadAllText(Path.Combine(modelJsonDir, expressions[i].File));
47+
var expJsonPath = Path.Combine(modelJsonDir, expressions[i].File);
48+
if (!File.Exists(expJsonPath)) {
49+
continue;
50+
}
51+
var exp3JsonString = File.ReadAllText(expJsonPath);
4852
CubismExp3Json cubismExp3Json = CubismExp3Json.LoadFrom(exp3JsonString);
4953
var exp3Instance = CubismExpressionData.CreateInstance(cubismExp3Json);
5054
tempExpressionList.Add(exp3Instance);

Assets/Scripts/Live2D/DynamicFadeMotionSetup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ private void LoadFadeMotions()
120120
for (int j = 0; j < motionArray.Length; j++)
121121
{
122122
string motionFilePath = Path.Combine(modelJsonDir, motionArray[j].File);
123+
if (!File.Exists(motionFilePath)) {
124+
continue;
125+
}
123126
var motion3JsonString = File.ReadAllText(motionFilePath);
124127
CubismMotion3Json motion3Json = CubismMotion3Json.LoadFrom(motion3JsonString);
125128
var clip = motion3Json.ToAnimationClip(poseJson: null);

0 commit comments

Comments
 (0)