74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.Rendering;
|
||
|
||
namespace Vanguard.Board3D
|
||
{
|
||
/// <summary>Плоские подписи на столе без TMP и Canvas.</summary>
|
||
internal static class BoardText
|
||
{
|
||
private static Font _font;
|
||
|
||
public static Font Font
|
||
{
|
||
get
|
||
{
|
||
if (_font != null)
|
||
return _font;
|
||
|
||
_font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
|
||
if (_font == null)
|
||
_font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||
if (_font == null)
|
||
_font = Resources.GetBuiltinResource<Font>("Builtin-LegacyRuntime.ttf");
|
||
return _font;
|
||
}
|
||
}
|
||
|
||
public static TextMesh Create(
|
||
Transform parent,
|
||
string text,
|
||
float characterSize,
|
||
Color color,
|
||
Vector3 localPosition,
|
||
float yaw = 0f,
|
||
TextAnchor anchor = TextAnchor.MiddleCenter)
|
||
{
|
||
var go = new GameObject("Board Label");
|
||
go.transform.SetParent(parent, false);
|
||
go.transform.localPosition = localPosition;
|
||
// TextMesh по умолчанию смотрит в -Z; +90° по X кладёт его на стол.
|
||
go.transform.localRotation = Quaternion.Euler(90f, yaw, 0f);
|
||
|
||
var mesh = go.AddComponent<TextMesh>();
|
||
mesh.font = Font;
|
||
mesh.fontSize = 64;
|
||
mesh.characterSize = characterSize;
|
||
mesh.anchor = anchor;
|
||
mesh.alignment = TextAlignment.Center;
|
||
mesh.color = color;
|
||
mesh.text = text;
|
||
mesh.richText = false;
|
||
|
||
var renderer = go.GetComponent<MeshRenderer>();
|
||
if (mesh.font != null && mesh.font.material != null)
|
||
renderer.sharedMaterial = mesh.font.material;
|
||
renderer.shadowCastingMode = ShadowCastingMode.Off;
|
||
renderer.receiveShadows = false;
|
||
renderer.sortingOrder = 1;
|
||
return mesh;
|
||
}
|
||
|
||
public static void SetText(TextMesh mesh, string text)
|
||
{
|
||
if (mesh != null)
|
||
mesh.text = text ?? string.Empty;
|
||
}
|
||
|
||
public static void SetColor(TextMesh mesh, Color color)
|
||
{
|
||
if (mesh != null)
|
||
mesh.color = color;
|
||
}
|
||
}
|
||
}
|