The BIG Unity 2022 Text reorganisation

pull/413/head
Simon Jackson 2023-02-04 11:22:31 +00:00
parent 1002db24f2
commit 8d18515a4a
21 changed files with 239 additions and 64 deletions

View File

@ -5,7 +5,11 @@
namespace UnityEngine.UI.Extensions.ColorPicker
{
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
public class ColorLabel : MonoBehaviour
{
public ColorPickerControl picker;
@ -18,12 +22,18 @@ namespace UnityEngine.UI.Extensions.ColorPicker
public int precision = 0;
#if UNITY_2022_1_OR_NEWER
private TMPro.TMP_Text label;
#else
private Text label;
#endif
private void Awake()
{
#if UNITY_2022_1_OR_NEWER
label = GetComponent<TMPro.TMP_Text>();
#else
label = GetComponent<Text>();
#endif
}
private void OnEnable()
@ -47,7 +57,11 @@ namespace UnityEngine.UI.Extensions.ColorPicker
#if UNITY_EDITOR
private void OnValidate()
{
#if UNITY_2022_1_OR_NEWER
label = GetComponent<TMPro.TMP_Text>();
#else
label = GetComponent<Text>();
#endif
UpdateValue();
}
#endif

View File

@ -299,8 +299,11 @@ namespace UnityEngine.UI.Extensions
if (i < AvailableOptions.Count)
{
itemObjs[i].name = "Item " + i + " " + _panelItems[i];
#if UNITY_2022_1_OR_NEWER
itemObjs[i].transform.Find("Text").GetComponent<TMPro.TMP_Text>().text = AvailableOptions[i]; //set the text value
#else
itemObjs[i].transform.Find("Text").GetComponent<Text>().text = AvailableOptions[i]; //set the text value
#endif
Button itemBtn = itemObjs[i].GetComponent<Button>();
itemBtn.onClick.RemoveAllListeners();
string textOfItem = _panelItems[i]; //has to be copied for anonymous function or it gets garbage collected away

View File

@ -213,8 +213,11 @@ namespace UnityEngine.UI.Extensions
if (i < AvailableOptions.Count)
{
itemObjs[i].name = "Item " + i + " " + _panelItems[i];
#if UNITY_2022_1_OR_NEWER
itemObjs[i].transform.Find("Text").GetComponent<TMPro.TMP_Text>().text = AvailableOptions[i]; //set the text value
#else
itemObjs[i].transform.Find("Text").GetComponent<Text>().text = AvailableOptions[i]; //set the text value
#endif
Button itemBtn = itemObjs[i].GetComponent<Button>();
itemBtn.onClick.RemoveAllListeners();
string textOfItem = _panelItems[i]; //has to be copied for anonymous function or it gets garbage collected away

View File

@ -5,7 +5,11 @@ namespace UnityEngine.UI.Extensions
{
public class ReorderableListDebug : MonoBehaviour
{
#if UNITY_2022_1_OR_NEWER
public TMPro.TMP_Text DebugLabel;
#else
public Text DebugLabel;
#endif
void Awake()
{

View File

@ -1,9 +1,7 @@
/// Credit David Gileadi
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/12
using System;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
@ -205,7 +203,11 @@ namespace UnityEngine.UI.Extensions
void ChangeTextColor(Color targetColor)
{
#if UNITY_2022_1_OR_NEWER
var text = GetComponentInChildren<TMPro.TMP_Text>();
#else
var text = GetComponentInChildren<Text>();
#endif
if (!text)
return;

View File

@ -40,13 +40,21 @@ namespace UnityEngine.UI.Extensions
//We want the test object to be either a UI element, a 2D element or a 3D element, so we'll get the appropriate components
SpriteRenderer spriteRenderer;
Image image;
#if UNITY_2022_1_OR_NEWER
TMPro.TMP_Text text;
#else
Text text;
#endif
void Start()
{
spriteRenderer = transform.GetComponent<SpriteRenderer>();
image = transform.GetComponent<Image>();
#if UNITY_2022_1_OR_NEWER
text = transform.GetComponent<TMPro.TMP_Text>();
#else
text = transform.GetComponent<Text>();
#endif
}
void Update()
@ -86,8 +94,6 @@ namespace UnityEngine.UI.Extensions
{
GetComponent<UnityEngine.Renderer>().material.color = color;
}
}
}
}

View File

@ -16,7 +16,12 @@ namespace UnityEngine.UI.Extensions {
[AddComponentMenu("UI/Extensions/TextPic")]
[ExecuteInEditMode] // Needed for culling images that are not used //
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler {
#if UNITY_2022_1_OR_NEWER
public class TextPic : TMPro.TMP_Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
#else
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
#endif
{
// Icon entry to replace text with
[Serializable]
public struct IconName {
@ -465,14 +470,21 @@ namespace UnityEngine.UI.Extensions {
/// UNITY METHODS ///
protected override void OnPopulateMesh(VertexHelper toFill) {
#if UNITY_2022_1_OR_NEWER
originalText = text;
text = GetOutputText();
base.OnPopulateMesh(toFill);
text = originalText;
#else
originalText = m_Text;
m_Text = GetOutputText();
base.OnPopulateMesh(toFill);
m_DisableFontTextureRebuiltCallback = true;
m_DisableFontTextureRebuiltCallback = true;
m_Text = originalText;
#endif
positions.Clear();
@ -539,8 +551,10 @@ namespace UnityEngine.UI.Extensions {
// Update the quad images
updateQuad = true;
#if !UNITY_2022_1_OR_NEWER
m_DisableFontTextureRebuiltCallback = false;
}
#endif
}
/// <summary>
/// Click event is detected whether to click a hyperlink text
@ -641,7 +655,7 @@ namespace UnityEngine.UI.Extensions {
#endif
protected override void OnEnable() {
#if UNITY_2019_1_OR_NEWER
#if UNITY_2019_1_OR_NEWER
// Here is the hack to see if Unity is using the new rendering system for text
usesNewRendering = false;
@ -660,13 +674,14 @@ namespace UnityEngine.UI.Extensions {
else {
usesNewRendering = true;
}
#endif
#endif
base.OnEnable();
#if !UNITY_2022_1_OR_NEWER
supportRichText = true;
alignByGeometry = true;
#endif
// Enable images on TextPic disable
if (m_ImagesPool.Count >= 1) {
for (int i = 0; i < m_ImagesPool.Count; i++) {

View File

@ -1,6 +1,8 @@
/// Credit Melang
/// Sourced from - http://forum.unity3d.com/members/melang.593409/
/// NOT supported in Unity 2022
#if !UNITY_2022_1_OR_NEWER
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
@ -60,3 +62,4 @@ namespace UnityEngine.UI.Extensions
}
}
}
#endif

View File

@ -4,14 +4,24 @@
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(RectTransform))]
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
[AddComponentMenu("UI/Effects/Extensions/Curly UI Text")]
public class CUIText : CUIGraphic
{
public override void ReportSet()
{
if (uiGraphic == null)
{
#if UNITY_2022_1_OR_NEWER
uiGraphic = GetComponent<TMPro.TMP_Text>();
#else
uiGraphic = GetComponent<Text>();
#endif
}
base.ReportSet();
}

View File

@ -3,7 +3,12 @@
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(Text), typeof(RectTransform))]
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/Effects/Extensions/Curved Text")]
public class CurvedText : BaseMeshEffect
{

View File

@ -4,7 +4,12 @@
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(Text), typeof(RectTransform))]
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/Effects/Extensions/Cylinder Text")]
public class CylinderText : BaseMeshEffect
{

View File

@ -81,8 +81,12 @@ namespace UnityEngine.UI.Extensions
List<UIVertex> verts = new List<UIVertex>();
vh.GetUIVertexStream(verts);
Text text = GetComponent<Text>();
if (text == null)
#if UNITY_2022_1_OR_NEWER
var text = GetComponent<TMPro.TMP_Text>();
#else
var text = GetComponent<Text>();
#endif
if (text == null)
{
Debug.LogWarning("LetterSpacing: Missing Text component");
return;
@ -93,29 +97,52 @@ namespace UnityEngine.UI.Extensions
float letterOffset = spacing * (float)text.fontSize / 100f;
float alignmentFactor = 0;
int glyphIdx = 0;
switch (text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.MiddleLeft:
case TextAnchor.UpperLeft:
alignmentFactor = 0f;
break;
case TextAnchor.LowerCenter:
case TextAnchor.MiddleCenter:
case TextAnchor.UpperCenter:
alignmentFactor = 0.5f;
break;
case TextAnchor.LowerRight:
case TextAnchor.MiddleRight:
case TextAnchor.UpperRight:
alignmentFactor = 1f;
break;
}
for (int lineIdx=0; lineIdx < lines.Length; lineIdx++)
#if UNITY_2022_1_OR_NEWER
switch (text.alignment)
{
case TMPro.TextAlignmentOptions.BottomLeft:
case TMPro.TextAlignmentOptions.MidlineLeft:
case TMPro.TextAlignmentOptions.TopLeft:
alignmentFactor = 0f;
break;
case TMPro.TextAlignmentOptions.BottomJustified:
case TMPro.TextAlignmentOptions.MidlineJustified:
case TMPro.TextAlignmentOptions.TopJustified:
alignmentFactor = 0.5f;
break;
case TMPro.TextAlignmentOptions.BottomRight:
case TMPro.TextAlignmentOptions.MidlineRight:
case TMPro.TextAlignmentOptions.TopRight:
alignmentFactor = 1f;
break;
}
#else
switch (text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.MiddleLeft:
case TextAnchor.UpperLeft:
alignmentFactor = 0f;
break;
case TextAnchor.LowerCenter:
case TextAnchor.MiddleCenter:
case TextAnchor.UpperCenter:
alignmentFactor = 0.5f;
break;
case TextAnchor.LowerRight:
case TextAnchor.MiddleRight:
case TextAnchor.UpperRight:
alignmentFactor = 1f;
break;
}
#endif
for (int lineIdx=0; lineIdx < lines.Length; lineIdx++)
{
string line = lines[lineIdx];
float lineOffset = (line.Length -1) * letterOffset * alignmentFactor;

View File

@ -46,7 +46,11 @@ using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
[AddComponentMenu("UI/Effects/Extensions/Mono Spacing")]
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
[RequireComponent(typeof(RectTransform))]
///Summary
/// Note, Vertex Count has changed in 5.2.1+, is now 6 (two tris) instead of 4 (tri strip).
@ -58,13 +62,21 @@ namespace UnityEngine.UI.Extensions
public bool UseHalfCharWidth = false;
private RectTransform rectTransform;
#if UNITY_2022_1_OR_NEWER
private TMPro.TMP_Text text;
#else
private Text text;
#endif
protected MonoSpacing() { }
protected override void Awake()
{
#if UNITY_2022_1_OR_NEWER
text = GetComponent<TMPro.TMP_Text>();
#else
text = GetComponent<Text>();
#endif
if (text == null)
{
Debug.LogWarning("MonoSpacing: Missing Text component");
@ -104,29 +116,51 @@ namespace UnityEngine.UI.Extensions
float letterOffset = Spacing * (float)text.fontSize / 100f;
float alignmentFactor = 0;
int glyphIdx = 0;
switch (text.alignment)
#if UNITY_2022_1_OR_NEWER
switch (text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.MiddleLeft:
case TextAnchor.UpperLeft:
case TMPro.TextAlignmentOptions.BottomLeft:
case TMPro.TextAlignmentOptions.MidlineLeft:
case TMPro.TextAlignmentOptions.TopLeft:
alignmentFactor = 0f;
break;
case TextAnchor.LowerCenter:
case TextAnchor.MiddleCenter:
case TextAnchor.UpperCenter:
case TMPro.TextAlignmentOptions.BottomJustified:
case TMPro.TextAlignmentOptions.MidlineJustified:
case TMPro.TextAlignmentOptions.TopJustified:
alignmentFactor = 0.5f;
break;
case TextAnchor.LowerRight:
case TextAnchor.MiddleRight:
case TextAnchor.UpperRight:
case TMPro.TextAlignmentOptions.BottomRight:
case TMPro.TextAlignmentOptions.MidlineRight:
case TMPro.TextAlignmentOptions.TopRight:
alignmentFactor = 1f;
break;
}
for (int lineIdx=0; lineIdx < lines.Length; lineIdx++)
#else
switch (text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.MiddleLeft:
case TextAnchor.UpperLeft:
alignmentFactor = 0f;
break;
case TextAnchor.LowerCenter:
case TextAnchor.MiddleCenter:
case TextAnchor.UpperCenter:
alignmentFactor = 0.5f;
break;
case TextAnchor.LowerRight:
case TextAnchor.MiddleRight:
case TextAnchor.UpperRight:
alignmentFactor = 1f;
break;
}
#endif
for (int lineIdx=0; lineIdx < lines.Length; lineIdx++)
{
string line = lines[lineIdx];
float lineOffset = (line.Length - 1) * letterOffset * (alignmentFactor) - (alignmentFactor - 0.5f) * rectTransform.rect.width;

View File

@ -1,7 +1,9 @@
/// Credit Melang, Lee Hui
/// Sourced from - http://forum.unity3d.com/members/melang.593409/
/// GC Alloc fix - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/130
/// NOT supported in Unity 2022
#if !UNITY_2022_1_OR_NEWER
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
@ -192,4 +194,5 @@ namespace UnityEngine.UI.Extensions
}
#endif
}
}
}
#endif

View File

@ -1,4 +1,6 @@
/// Credit NemoKrad (aka Charles Humphrey) / valtain

using static System.Net.Mime.MediaTypeNames;
/// Credit NemoKrad (aka Charles Humphrey) / valtain
/// Sourced from - http://www.randomchaos.co.uk/SoftAlphaUIMask.aspx
/// Updated by valtain - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/pull-requests/33
@ -45,7 +47,11 @@ namespace UnityEngine.UI.Extensions
MaskArea = GetComponent<RectTransform>();
}
#if UNITY_2022_1_OR_NEWER
var text = GetComponent<TMPro.TMP_Text>();
#else
var text = GetComponent<Text>();
#endif
if (text != null)
{
mat = new Material(ShaderLibrary.GetShaderInstance("UI Extensions/SoftMaskShader"));

View File

@ -210,10 +210,10 @@ namespace UnityEngine.UI.Extensions
{
FocusedElementIndex = i;
#if UNITY_2022_1_OR_NEWER
var textComponentTxtMeshPro = _arrayOfElements[i].GetComponentInChildren<TMPro.TMP_Text>();
var textComponentTxtMeshPro = arrayOfElements[i].GetComponentInChildren<TMPro.TMP_Text>();
if (textComponentTxtMeshPro != null)
{
result = textComponentTxtMeshPro.text;
Result = textComponentTxtMeshPro.text;
}
#else
var textComponent = arrayOfElements[i].GetComponentInChildren<Text>();

View File

@ -213,10 +213,10 @@ namespace UnityEngine.UI.Extensions
{
FocusedElementIndex = i;
#if UNITY_2022_1_OR_NEWER
var textComponentTxtMeshPro = _arrayOfElements[i].GetComponentInChildren<TMPro.TMP_Text>();
var textComponentTxtMeshPro = arrayOfElements[i].GetComponentInChildren<TMPro.TMP_Text>();
if (textComponentTxtMeshPro != null)
{
result = textComponentTxtMeshPro.text;
Result = textComponentTxtMeshPro.text;
}
#else
var textComponent = arrayOfElements[i].GetComponentInChildren<Text>();

View File

@ -11,7 +11,11 @@ namespace UnityEngine.UI.Extensions
public int verticalPadding;
//tooltip text
#if UNITY_2022_1_OR_NEWER
public TMPro.TMP_Text thisText;
#else
public Text thisText;
#endif
//horizontal layout of the tooltip
public HorizontalLayoutGroup hlG;

View File

@ -27,7 +27,11 @@ namespace UnityEngine.UI.Extensions
public class ToolTip : MonoBehaviour
{
//text of the tooltip
#if UNITY_2022_1_OR_NEWER
private TMPro.TMP_Text _text;
#else
private Text _text;
#endif
private RectTransform _rectTransform, canvasRectTransform;
[Tooltip("The canvas used by the tooltip as positioning and scaling reference. Should usually be the root Canvas of the hierarchy this component is in")]
@ -114,7 +118,11 @@ namespace UnityEngine.UI.Extensions
canvasRectTransform = canvas.GetComponent<RectTransform>();
_layoutGroup = GetComponentInChildren<LayoutGroup>();
#if UNITY_2022_1_OR_NEWER
_text = GetComponentInChildren<TMPro.TMP_Text>();
#else
_text = GetComponentInChildren<Text>();
#endif
_inside = false;

View File

@ -6,15 +6,27 @@ Simply place the script on A Text control in the scene to display the current PP
namespace UnityEngine.UI.Extensions
{
#if UNITY_2022_1_OR_NEWER
[RequireComponent(typeof(TMPro.TMP_Text))]
#else
[RequireComponent(typeof(Text))]
#endif
[AddComponentMenu("UI/Extensions/PPIViewer")]
public class PPIViewer : MonoBehaviour
{
#if UNITY_2022_1_OR_NEWER
private TMPro.TMP_Text label;
#else
private Text label;
#endif
void Awake()
{
label = GetComponent<Text>();
#if UNITY_2022_1_OR_NEWER
label = GetComponentInChildren<TMPro.TMP_Text>();
#else
label = GetComponentInChildren<Text>();
#endif
}
void Start()

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6f5aae3077b0a914bb69e41bb26605de
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: