You've already forked ParticleEffectForUGUI
mirror of
https://github.com/mob-sakai/ParticleEffectForUGUI.git
synced 2026-05-15 20:50:08 +00:00
resharp
This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Coffee.NanoMonitor{
|
||||
namespace Coffee.NanoMonitor
|
||||
{
|
||||
public class FixedFont
|
||||
{
|
||||
private readonly UIVertex[] _tmpVerts = new UIVertex[4];
|
||||
private static readonly Dictionary<Font, FixedFont> _fonts = new Dictionary<Font, FixedFont>();
|
||||
private static readonly TextGenerator _textGenerator = new TextGenerator(100);
|
||||
private const string k_Characters =
|
||||
"_!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
||||
|
||||
private static TextGenerationSettings _settings = new TextGenerationSettings
|
||||
private static readonly Dictionary<Font, FixedFont> s_Fonts = new Dictionary<Font, FixedFont>();
|
||||
private static readonly TextGenerator s_TextGenerator = new TextGenerator(100);
|
||||
|
||||
private static TextGenerationSettings s_Settings = new TextGenerationSettings
|
||||
{
|
||||
scaleFactor = 1,
|
||||
horizontalOverflow = HorizontalWrapMode.Overflow,
|
||||
@@ -19,25 +22,33 @@ namespace Coffee.NanoMonitor{
|
||||
color = Color.white
|
||||
};
|
||||
|
||||
private int _resolution = 0;
|
||||
private readonly Font _font;
|
||||
private UIVertex[] _verts;
|
||||
private readonly UIVertex[] _tmpVerts = new UIVertex[4];
|
||||
private UICharInfo[] _charInfos;
|
||||
|
||||
private int _resolution;
|
||||
private UIVertex[] _verts;
|
||||
|
||||
private FixedFont(Font font)
|
||||
{
|
||||
_font = font;
|
||||
}
|
||||
|
||||
public int fontSize => _font.dynamic ? 32 : _font.fontSize;
|
||||
public int fontSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return _font.dynamic ? 32 : _font.fontSize;
|
||||
}
|
||||
}
|
||||
|
||||
public static FixedFont GetOrCreate(Font font)
|
||||
{
|
||||
if (font == null) return null;
|
||||
if (_fonts.TryGetValue(font, out var data)) return data;
|
||||
if (s_Fonts.TryGetValue(font, out var data)) return data;
|
||||
|
||||
data = new FixedFont(font);
|
||||
_fonts.Add(font, data);
|
||||
s_Fonts.Add(font, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -61,18 +72,20 @@ namespace Coffee.NanoMonitor{
|
||||
if (_resolution == currentResolution) return;
|
||||
_resolution = currentResolution;
|
||||
|
||||
_settings.font = _font;
|
||||
_textGenerator.Invalidate();
|
||||
_textGenerator.Populate("_!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", _settings);
|
||||
s_Settings.font = _font;
|
||||
s_TextGenerator.Invalidate();
|
||||
s_TextGenerator.Populate(k_Characters, s_Settings);
|
||||
|
||||
_verts = _textGenerator.GetVerticesArray();
|
||||
_charInfos = _textGenerator.GetCharactersArray();
|
||||
_verts = s_TextGenerator.GetVerticesArray();
|
||||
_charInfos = s_TextGenerator.GetCharactersArray();
|
||||
|
||||
float offsetX = 0;
|
||||
for (var i = 0; i < _verts.Length; i++)
|
||||
{
|
||||
if ((i & 3) == 0)
|
||||
{
|
||||
offsetX = _verts[i].position.x;
|
||||
}
|
||||
|
||||
var v = _verts[i];
|
||||
v.position -= new Vector3(offsetX, 0);
|
||||
@@ -118,18 +131,27 @@ namespace Coffee.NanoMonitor{
|
||||
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
_tmpVerts[i] = new UIVertex();
|
||||
_tmpVerts[i].uv0 = uv;
|
||||
_tmpVerts[i].color = color;
|
||||
_tmpVerts[i] = new UIVertex
|
||||
{
|
||||
uv0 = uv,
|
||||
color = color
|
||||
};
|
||||
|
||||
if (i == 0)
|
||||
_tmpVerts[i].position = new Vector2(-size.x, -size.y) + offset;
|
||||
else if (i == 1)
|
||||
_tmpVerts[i].position = new Vector2(-size.x, size.y) + offset;
|
||||
else if (i == 2)
|
||||
_tmpVerts[i].position = new Vector2(size.x, size.y) + offset;
|
||||
else
|
||||
_tmpVerts[i].position = new Vector2(size.x, -size.y) + offset;
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
_tmpVerts[i].position = new Vector2(-size.x, -size.y) + offset;
|
||||
break;
|
||||
case 1:
|
||||
_tmpVerts[i].position = new Vector2(-size.x, size.y) + offset;
|
||||
break;
|
||||
case 2:
|
||||
_tmpVerts[i].position = new Vector2(size.x, size.y) + offset;
|
||||
break;
|
||||
case 3:
|
||||
_tmpVerts[i].position = new Vector2(size.x, -size.y) + offset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
toFill.AddUIVertexQuad(_tmpVerts);
|
||||
|
||||
@@ -1,50 +1,46 @@
|
||||
using System.Text;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Coffee.NanoMonitor{
|
||||
namespace Coffee.NanoMonitor
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
|
||||
[CustomEditor(typeof(MonitorUI))]
|
||||
public class MonitorTextEditor : Editor
|
||||
{
|
||||
private SerializedProperty m_Mode;
|
||||
private SerializedProperty m_TextAnchor;
|
||||
private SerializedProperty m_FontSize;
|
||||
private SerializedProperty m_Font;
|
||||
private SerializedProperty m_Text;
|
||||
private SerializedProperty m_Color;
|
||||
private SerializedProperty _color;
|
||||
private SerializedProperty _font;
|
||||
private SerializedProperty _fontSize;
|
||||
private SerializedProperty _mode;
|
||||
private SerializedProperty _text;
|
||||
private SerializedProperty _textAnchor;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
m_Mode = serializedObject.FindProperty("m_Mode");
|
||||
|
||||
m_Text = serializedObject.FindProperty("m_Text");
|
||||
m_Color = serializedObject.FindProperty("m_Color");
|
||||
_mode = serializedObject.FindProperty("m_Mode");
|
||||
_text = serializedObject.FindProperty("m_Text");
|
||||
_color = serializedObject.FindProperty("m_Color");
|
||||
var fontData = serializedObject.FindProperty("m_FontData");
|
||||
m_FontSize = fontData.FindPropertyRelative("m_FontSize");
|
||||
m_Font = fontData.FindPropertyRelative("m_Font");
|
||||
m_TextAnchor = serializedObject.FindProperty("m_TextAnchor");
|
||||
_fontSize = fontData.FindPropertyRelative("m_FontSize");
|
||||
_font = fontData.FindPropertyRelative("m_Font");
|
||||
_textAnchor = serializedObject.FindProperty("m_TextAnchor");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
|
||||
EditorGUILayout.PropertyField(m_Mode);
|
||||
if ((MonitorUI.Mode) m_Mode.intValue == MonitorUI.Mode.Text)
|
||||
EditorGUILayout.PropertyField(_mode);
|
||||
if ((MonitorUI.Mode)_mode.intValue == MonitorUI.Mode.Text)
|
||||
{
|
||||
EditorGUILayout.PropertyField(m_Text);
|
||||
EditorGUILayout.PropertyField(m_FontSize);
|
||||
EditorGUILayout.PropertyField(m_TextAnchor);
|
||||
|
||||
//EditorGUI.BeginDisabledGroup(true);
|
||||
EditorGUILayout.PropertyField(m_Font);
|
||||
//EditorGUI.EndDisabledGroup();
|
||||
EditorGUILayout.PropertyField(_text);
|
||||
EditorGUILayout.PropertyField(_fontSize);
|
||||
EditorGUILayout.PropertyField(_textAnchor);
|
||||
EditorGUILayout.PropertyField(_font);
|
||||
}
|
||||
|
||||
EditorGUILayout.PropertyField(m_Color);
|
||||
EditorGUILayout.PropertyField(_color);
|
||||
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
@@ -56,7 +52,7 @@ namespace Coffee.NanoMonitor{
|
||||
public enum Mode
|
||||
{
|
||||
Text,
|
||||
Fill,
|
||||
Fill
|
||||
}
|
||||
|
||||
public enum TextAnchor
|
||||
@@ -74,26 +70,38 @@ namespace Coffee.NanoMonitor{
|
||||
[SerializeField] private TextAnchor m_TextAnchor;
|
||||
|
||||
|
||||
//################################
|
||||
// Private Members.
|
||||
//################################
|
||||
private readonly StringBuilder _sb = new StringBuilder(64);
|
||||
|
||||
|
||||
//################################
|
||||
// Public Members.
|
||||
//################################
|
||||
public override string text
|
||||
{
|
||||
get => m_StringBuilder.ToString();
|
||||
get
|
||||
{
|
||||
return _sb.ToString();
|
||||
}
|
||||
set
|
||||
{
|
||||
m_Text = value;
|
||||
if (m_StringBuilder.IsEqual(m_Text)) return;
|
||||
if (_sb.IsEqual(m_Text)) return;
|
||||
|
||||
m_StringBuilder.Length = 0;
|
||||
m_StringBuilder.Append(m_Text);
|
||||
_sb.Length = 0;
|
||||
_sb.Append(m_Text);
|
||||
SetVerticesDirty();
|
||||
}
|
||||
}
|
||||
|
||||
public TextAnchor textAnchor
|
||||
{
|
||||
get => m_TextAnchor;
|
||||
get
|
||||
{
|
||||
return m_TextAnchor;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_TextAnchor == value) return;
|
||||
@@ -105,44 +113,11 @@ namespace Coffee.NanoMonitor{
|
||||
|
||||
public override bool raycastTarget
|
||||
{
|
||||
get => m_Mode == Mode.Fill;
|
||||
set { }
|
||||
}
|
||||
|
||||
public void SetText(string format, double arg0 = 0, double arg1 = 0, double arg2 = 0, double arg3 = 0)
|
||||
{
|
||||
m_StringBuilder.Length = 0;
|
||||
m_StringBuilder.AppendFormatNoAlloc(format, arg0, arg1, arg2, arg3);
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
public void SetText(StringBuilder builder)
|
||||
{
|
||||
m_StringBuilder.Length = 0;
|
||||
m_StringBuilder.Append(builder);
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
|
||||
//################################
|
||||
// Private Members.
|
||||
//################################
|
||||
private readonly StringBuilder m_StringBuilder = new StringBuilder(64);
|
||||
|
||||
private void UpdateFont()
|
||||
{
|
||||
//var globalFont = NNanoMonitorttings.Instance.font;
|
||||
//if (globalFont)
|
||||
//{
|
||||
// font = globalFont;
|
||||
//}
|
||||
|
||||
var fontData = FixedFont.GetOrCreate(font);
|
||||
if (fontData != null)
|
||||
get
|
||||
{
|
||||
fontData.Invalidate();
|
||||
fontData.UpdateFont();
|
||||
return m_Mode == Mode.Fill;
|
||||
}
|
||||
set { }
|
||||
}
|
||||
|
||||
|
||||
@@ -151,19 +126,17 @@ namespace Coffee.NanoMonitor{
|
||||
//################################
|
||||
protected override void OnEnable()
|
||||
{
|
||||
//NaNanoMonitortings.Instance.onFontChanged += UpdateFont;
|
||||
RegisterDirtyMaterialCallback(UpdateFont);
|
||||
|
||||
base.OnEnable();
|
||||
raycastTarget = false;
|
||||
maskable = false;
|
||||
m_StringBuilder.Length = 0;
|
||||
m_StringBuilder.Append(m_Text);
|
||||
_sb.Length = 0;
|
||||
_sb.Append(m_Text);
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
//NanNanoMonitorings.Instance.onFontChanged -= UpdateFont;
|
||||
UnregisterDirtyMaterialCallback(UpdateFont);
|
||||
|
||||
base.OnDisable();
|
||||
@@ -174,14 +147,38 @@ namespace Coffee.NanoMonitor{
|
||||
{
|
||||
base.OnValidate();
|
||||
|
||||
if (!m_StringBuilder.IsEqual(m_Text))
|
||||
if (!_sb.IsEqual(m_Text))
|
||||
{
|
||||
m_StringBuilder.Length = 0;
|
||||
m_StringBuilder.Append(m_Text);
|
||||
_sb.Length = 0;
|
||||
_sb.Append(m_Text);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public void SetText(string format, double arg0 = 0, double arg1 = 0, double arg2 = 0, double arg3 = 0)
|
||||
{
|
||||
_sb.Length = 0;
|
||||
_sb.AppendFormatNoAlloc(format, arg0, arg1, arg2, arg3);
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
public void SetText(StringBuilder builder)
|
||||
{
|
||||
_sb.Length = 0;
|
||||
_sb.Append(builder);
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
private void UpdateFont()
|
||||
{
|
||||
var fontData = FixedFont.GetOrCreate(font);
|
||||
if (fontData != null)
|
||||
{
|
||||
fontData.Invalidate();
|
||||
fontData.UpdateFont();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper toFill)
|
||||
{
|
||||
toFill.Clear();
|
||||
@@ -197,7 +194,7 @@ namespace Coffee.NanoMonitor{
|
||||
return;
|
||||
}
|
||||
|
||||
var scale = (float) fontSize / fontData.fontSize;
|
||||
var scale = (float)fontSize / fontData.fontSize;
|
||||
float offset = 0;
|
||||
switch (textAnchor)
|
||||
{
|
||||
@@ -205,20 +202,26 @@ namespace Coffee.NanoMonitor{
|
||||
offset = rectTransform.rect.xMin;
|
||||
break;
|
||||
case TextAnchor.Center:
|
||||
for (var i = 0; i < m_StringBuilder.Length; i++)
|
||||
offset = fontData.Layout(m_StringBuilder[i], offset, scale);
|
||||
for (var i = 0; i < _sb.Length; i++)
|
||||
{
|
||||
offset = fontData.Layout(_sb[i], offset, scale);
|
||||
}
|
||||
|
||||
offset = -offset / 2;
|
||||
break;
|
||||
case TextAnchor.Right:
|
||||
for (var i = 0; i < m_StringBuilder.Length; i++)
|
||||
offset = fontData.Layout(m_StringBuilder[i], offset, scale);
|
||||
for (var i = 0; i < _sb.Length; i++)
|
||||
{
|
||||
offset = fontData.Layout(_sb[i], offset, scale);
|
||||
}
|
||||
|
||||
offset = rectTransform.rect.xMax - offset;
|
||||
break;
|
||||
}
|
||||
|
||||
for (var i = 0; i < m_StringBuilder.Length; i++)
|
||||
for (var i = 0; i < _sb.Length; i++)
|
||||
{
|
||||
offset = fontData.Append(toFill, m_StringBuilder[i], offset, scale, color);
|
||||
offset = fontData.Append(toFill, _sb[i], offset, scale, color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user