用 Unity 遇到的坑太多,写个文章提醒自己。
Text Mesh Pro
部分汉字显示为方框
相关字体勾选 Multi Atlas Textures(默认未勾选)。
显示中文或日文时卡顿
相关字体取消勾选 Clear Dynamic Data On Build(默认勾选)。
Audio Mixer
线性调节分贝值
Mathf.Max(Mathf.Log10(volume) * 20, -80f)
Camera
摄像机固定 16 : 9 比例
using UnityEngine;
public class CameraRescaler : SingletonMono<CameraRescaler> {
private int ScreenSizeX = 0;
private int ScreenSizeY = 0;
private void RescaleCamera() {
int width = Screen.width;
int height = Screen.height;
if(Screen.fullScreen) {
width = Screen.mainWindowDisplayInfo.width;
height = Screen.mainWindowDisplayInfo.height;
}
if(width == ScreenSizeX && height == ScreenSizeY) {
return;
}
if(Screen.width != width) {
Screen.SetResolution(width, height, Screen.fullScreen);
}
float targetaspect = 16.0f / 9.0f;
float windowaspect = (float)width / height;
float scaleheight = windowaspect / targetaspect;
Camera camera = Camera.main;
if(scaleheight < 1.0f) {
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
camera.rect = rect;
}
else {
float scalewidth = 1.0f / scaleheight;
Rect rect = camera.rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
camera.rect = rect;
}
ScreenSizeX = width;
ScreenSizeY = height;
}
private void Start() {
RescaleCamera();
}
private void Update() {
RescaleCamera();
}
}
Localization
本地化文本初次加载有延迟
相关 Table 勾选 Preload,或手动 Preload。
预加载时报错 OperationException : Failed to initialize localization, could not preload asset tables
在尝试使用 LocalizationSettings 前,等待其初始化完成。
LocalizationSettings.InitializationOperation.WaitForCompletion();
(这会导致主线程堵塞,有必要时需要用协程等方法处理。)
System.Diagnotics
运行中打开文件夹
System.Diagnostics.Process.Start(folder)
EventSystems
PointerEventData 获取鼠标输入按键
eventData.button
Screeen
禁止响应系统全屏快捷键
(6.0) Player > Resolution and Presentation > Allow Fullscreen Switch 取消勾选。

Comments NOTHING