本地化问题修改
This commit is contained in:
@@ -16,6 +16,7 @@ namespace AibisDream.FixSystem
|
||||
|
||||
public float rotateDuration = 0.5f;
|
||||
public float typeSpeed = 0.05f;
|
||||
public int maxTextLine;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -26,9 +27,12 @@ namespace AibisDream.FixSystem
|
||||
[SerializeField] private TMP_Text text;
|
||||
[SerializeField] private SpriteRenderer screenImage;
|
||||
private Material _imageMaterial;
|
||||
private Action _nextStep;
|
||||
|
||||
private TextGenerator _generator;
|
||||
private TextGenerationSettings _generationSettings;
|
||||
|
||||
private IUnRegister _plugOutRmv;
|
||||
private Action _nextStep;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -42,6 +46,18 @@ namespace AibisDream.FixSystem
|
||||
{
|
||||
_textScreen = transform.Find("旋转屏幕");
|
||||
_imageMaterial = screenImage.material;
|
||||
|
||||
_generator = new TextGenerator();
|
||||
_generationSettings = new TextGenerationSettings
|
||||
{
|
||||
font = text.font.sourceFontFile,
|
||||
fontSize = Mathf.RoundToInt(text.fontSize),
|
||||
lineSpacing = text.lineSpacing,
|
||||
horizontalOverflow = HorizontalWrapMode.Wrap,
|
||||
verticalOverflow = VerticalWrapMode.Truncate,
|
||||
generationExtents = new Vector2(text.rectTransform.rect.width, Mathf.Infinity),
|
||||
resizeTextForBestFit = false
|
||||
};
|
||||
}
|
||||
|
||||
private void Register()
|
||||
@@ -76,18 +92,43 @@ namespace AibisDream.FixSystem
|
||||
{
|
||||
text.text += "\n";
|
||||
}
|
||||
// 先估算新增行数
|
||||
var targetLineNum = EstimateLineNum(targetText);
|
||||
// 截断超出屏幕的部分
|
||||
var oldText = "";
|
||||
if (targetLineNum + text.textInfo.lineCount > maxTextLine)
|
||||
{
|
||||
var cutLineNum = targetLineNum + text.textInfo.lineCount - maxTextLine;
|
||||
if (cutLineNum <= text.textInfo.lineCount)
|
||||
{
|
||||
var lastCharIndex = text.textInfo.lineInfo[cutLineNum - 1].lastCharacterIndex;
|
||||
oldText = text.text[lastCharIndex..];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
oldText = text.text;
|
||||
}
|
||||
|
||||
var oldText = text.text;
|
||||
var targetLength = text.text.Length + targetText.Length;
|
||||
|
||||
var targetLength = oldText.Length + targetText.Length;
|
||||
|
||||
// 逐个字符添加
|
||||
yield return DOTween.To(() => text.text.Length, x => UpdateText(x, targetText, oldText), targetLength,
|
||||
yield return DOTween.To(() => oldText.Length, x => UpdateText(x, targetText, oldText), targetLength,
|
||||
targetText.Length * typeSpeed).WaitForCompletion();
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
|
||||
_nextStep.Invoke();
|
||||
}
|
||||
|
||||
private int EstimateLineNum(string str)
|
||||
{
|
||||
var height = _generator.GetPreferredHeight(str, _generationSettings);
|
||||
var lineHeight = text.fontSize * 1.2f; // 简单估算行高
|
||||
|
||||
return Mathf.CeilToInt(height / lineHeight);
|
||||
}
|
||||
|
||||
private void UpdateText(int currentLength, string nextText, string oldText)
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace AibisDream.Framework
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!sprite) return "";
|
||||
|
||||
|
||||
// 先读Psd的路径
|
||||
string psdPath = AssetDatabase.GetAssetPath(sprite);
|
||||
var psdResPath = GetResourcePath(psdPath);
|
||||
@@ -28,13 +28,12 @@ namespace AibisDream.Framework
|
||||
|
||||
// 提取文件名(不包括扩展名)
|
||||
var purePath = psdResPath.Substring(0, lastDotIndex);
|
||||
|
||||
|
||||
// 添加后缀
|
||||
return purePath + "/" + sprite.name;
|
||||
#else
|
||||
return null;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
private static string GetResourcePath(string absolutePath)
|
||||
@@ -42,15 +41,7 @@ namespace AibisDream.Framework
|
||||
const string resourcesFolderName = "Resources/";
|
||||
int index = absolutePath.IndexOf(resourcesFolderName, StringComparison.Ordinal);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
// 获取 "Resources/" 之后的部分
|
||||
return absolutePath.Substring(index + resourcesFolderName.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return absolutePath;
|
||||
}
|
||||
return index >= 0 ? absolutePath[(index + resourcesFolderName.Length)..] : absolutePath;
|
||||
}
|
||||
|
||||
public static Sprite LoadSpriteFromPsd(string path)
|
||||
@@ -58,7 +49,7 @@ namespace AibisDream.Framework
|
||||
int lastIndex = path.LastIndexOf('/');
|
||||
string spriteName = path.Substring(lastIndex + 1);
|
||||
string resPath = path.Substring(0, lastIndex);
|
||||
|
||||
|
||||
// 加载
|
||||
var sprites = Resources.LoadAll<Sprite>(resPath);
|
||||
|
||||
@@ -72,39 +63,14 @@ namespace AibisDream.Framework
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static T LoadAddressableSync<T>(string key)
|
||||
|
||||
public static T LoadAssetSync<T>(string key) where T : UnityEngine.Object
|
||||
{
|
||||
T result = default;
|
||||
var operation = Addressables.LoadAssetAsync<T>(key);
|
||||
|
||||
// 运行协程等待加载完成
|
||||
GameManager.Instance.StartCoroutine(LoadCoroutine(operation, loadedObject => result = loadedObject));
|
||||
|
||||
// 等待协程完成(阻塞主线程)
|
||||
while (!operation.IsDone)
|
||||
{
|
||||
// 主动让出一帧
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
operation.WaitForCompletion();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static IEnumerator LoadCoroutine<T>(AsyncOperationHandle<T> operation, Action<T> onComplete)
|
||||
{
|
||||
yield return operation;
|
||||
|
||||
if (operation.Status == AsyncOperationStatus.Succeeded)
|
||||
{
|
||||
onComplete?.Invoke(operation.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load addressable");
|
||||
}
|
||||
|
||||
Addressables.Release(operation);
|
||||
return operation.Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization.Components;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
[RequireComponent(typeof(TMP_Text))]
|
||||
[RequireComponent(typeof(LocalizeStringEvent))]
|
||||
public class LocalizedText : MonoBehaviour
|
||||
{
|
||||
private TMP_Text _text;
|
||||
private LocalizeStringEvent _localizeEvent;
|
||||
|
||||
private void OnUpdateString(string text)
|
||||
{
|
||||
var textArr = text.Split(":");
|
||||
_text.text = textArr.Length > 1 ? textArr[1].Trim() : text;
|
||||
}
|
||||
|
||||
public void SetLocalizedText(string table, string entry)
|
||||
{
|
||||
_text = GetComponent<TMP_Text>();
|
||||
_localizeEvent = GetComponent<LocalizeStringEvent>();
|
||||
|
||||
_localizeEvent.OnUpdateString.AddListener(OnUpdateString);
|
||||
|
||||
_localizeEvent.SetTable(table);
|
||||
_localizeEvent.SetEntry(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c9c71a7e9c9455d955ff20b5db823a1
|
||||
timeCreated: 1746702086
|
||||
@@ -7,24 +7,36 @@ using AibisDream.Utility;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization.Components;
|
||||
using Yarn.Unity;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class TaskPanel : MonoBehaviour, IUIPanel, IDialogView
|
||||
{
|
||||
private RectTransform _rectTransform;
|
||||
private Transform _taskListParent;
|
||||
private GameObject _taskItemPrefab;
|
||||
|
||||
private readonly Dictionary<string, GameObject> _taskDict = new();
|
||||
private GameObject TaskItemPrefab
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_taskItemPrefab == null)
|
||||
{
|
||||
_taskItemPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.TaskItemName);
|
||||
}
|
||||
|
||||
return _taskItemPrefab;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, LocalizedText> _taskDict = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rectTransform = GetComponent<RectTransform>();
|
||||
_taskListParent = transform.Find("Record");
|
||||
_taskItemPrefab = ResourceKit.LoadAddressableSync<GameObject>(ConstRef.TaskItemName);
|
||||
EnumEventSystem.Global.Register(GameLoopEnum.GameQuit, ClearTasks);
|
||||
DialogCanvasManager.Instance.RegisterDialogView(DialogViewType.Task, this);
|
||||
}
|
||||
|
||||
#region 诊疗单功能
|
||||
@@ -33,7 +45,9 @@ namespace AibisDream
|
||||
{
|
||||
if (_taskDict.TryGetValue(lineId, out var taskItem))
|
||||
{
|
||||
taskItem.GetComponent<TMP_Text>().fontStyle |= FontStyles.Strikethrough;
|
||||
var taskText = taskItem.GetComponent<TMP_Text>();
|
||||
taskText.fontStyle |= FontStyles.Strikethrough;
|
||||
taskText.color = Color.gray;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,14 +66,15 @@ namespace AibisDream
|
||||
|
||||
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId, bool isAutoSkip = false)
|
||||
{
|
||||
var taskItem = Instantiate(_taskItemPrefab, _taskListParent);
|
||||
var taskItem = Instantiate(TaskItemPrefab, _taskListParent);
|
||||
// 设置本地化数据
|
||||
var localize = taskItem.GetComponent<LocalizeStringEvent>();
|
||||
localize.SetTable(DialogController.Instance.GetCurLocalizedTableName());
|
||||
localize.SetEntry(lineId);
|
||||
_taskDict[lineId] = taskItem;
|
||||
|
||||
ActionKit.Delay(0.5f, nextStep);
|
||||
var localize = taskItem.GetComponent<LocalizedText>();
|
||||
localize.SetLocalizedText(DialogController.Instance.GetCurLocalizedTableName(), lineId);
|
||||
_taskDict[lineId] = localize;
|
||||
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
|
||||
nextStep.Invoke();
|
||||
}
|
||||
|
||||
public void ShowOptions(DialogOption[] dialogueOptions)
|
||||
@@ -85,7 +100,6 @@ namespace AibisDream
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 打开和关闭
|
||||
|
||||
public bool IsOpen { get; private set;}
|
||||
@@ -95,7 +109,7 @@ namespace AibisDream
|
||||
gameObject.SetActive(true);
|
||||
AudioManager.Instance.PlaySfx("event:/Scriptal/taskmanager");
|
||||
// 下移
|
||||
transform.DOLocalMoveY(-350, 1f).OnComplete(() =>
|
||||
_rectTransform.DOAnchorPosY(-350, 1f).OnComplete(() =>
|
||||
{
|
||||
IsOpen = true;
|
||||
});
|
||||
@@ -105,7 +119,7 @@ namespace AibisDream
|
||||
{
|
||||
// 上移
|
||||
AudioManager.Instance.PlaySfx("event:/Scriptal/taskmanager");
|
||||
transform.DOLocalMoveY(0, 1f).OnComplete(() =>
|
||||
_rectTransform.DOAnchorPosY(0, 1f).OnComplete(() =>
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
IsOpen = false;
|
||||
@@ -117,7 +131,7 @@ namespace AibisDream
|
||||
|
||||
public static class TaskYarnCommand
|
||||
{
|
||||
private static TaskPanel TaskPanel => UIManager.Instance.GetComponent<TaskPanel>();
|
||||
private static TaskPanel TaskPanel => UIManager.Instance.GetPanel<TaskPanel>();
|
||||
|
||||
[YarnCommand("complete_task")]
|
||||
public static void CompleteTask(string lineId)
|
||||
|
||||
@@ -206,7 +206,7 @@ namespace AibisDream.Utility
|
||||
}
|
||||
}
|
||||
|
||||
result.Append(substring);
|
||||
result.Append(substring.TrimStart());
|
||||
currentIndex += length;
|
||||
|
||||
if (currentIndex < line.Length)
|
||||
|
||||
Reference in New Issue
Block a user