Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 59178c3

Browse files
committed
fix crash when font file doesn't exist
1 parent 0de8d5a commit 59178c3

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

com.unity.uiwidgets/Runtime/engine/UIWidgetsPanel.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.IO;
45
using Unity.UIWidgets.external.simplejson;
56
using Unity.UIWidgets.foundation;
67
using Unity.UIWidgets.rendering;
@@ -11,6 +12,7 @@
1112
using UnityEngine.Profiling;
1213
using UnityEngine.Scripting;
1314
using UnityEngine.UI;
15+
using Path = System.IO.Path;
1416
using RawImage = UnityEngine.UI.RawImage;
1517

1618
namespace Unity.UIWidgets.engine {
@@ -66,19 +68,33 @@ public object fontsToObject() {
6668
foreach (var setting in settings) {
6769
var font = new Dictionary<string, object>();
6870
font.Add("family", value: setting.Key);
69-
var dic = new Dictionary<string, object>[setting.Value.fonts.Length];
71+
var dic = new List<Dictionary<string, object>>();
7072
for (var j = 0; j < setting.Value.fonts.Length; j++) {
71-
dic[j] = new Dictionary<string, object>();
73+
var fontDic = new Dictionary<string, object>();
74+
var fileExist = false;
75+
7276
if (setting.Value.fonts[j].asset.Length > 0) {
73-
dic[j].Add("asset", value: setting.Value.fonts[j].asset);
77+
var assetPath = setting.Value.fonts[j].asset;
78+
var assetAbsolutePath = Path.Combine(Application.streamingAssetsPath, assetPath);
79+
if (!File.Exists(assetAbsolutePath)) {
80+
Debug.LogError($"The font asset (family: \"{setting.Key}\", path: \"{assetPath}\") is not found");
81+
}
82+
else {
83+
fileExist = true;
84+
}
85+
fontDic.Add("asset", value: setting.Value.fonts[j].asset);
7486
}
7587

7688
if (setting.Value.fonts[j].weight > 0) {
77-
dic[j].Add("weight", value: setting.Value.fonts[j].weight);
89+
fontDic.Add("weight", value: setting.Value.fonts[j].weight);
90+
}
91+
92+
if (fileExist) {
93+
dic.Add(fontDic);
7894
}
7995
}
8096

81-
font.Add("fonts", value: dic);
97+
font.Add("fonts", value: dic.ToArray());
8298
result[i] = font;
8399
i++;
84100
}

0 commit comments

Comments
 (0)