はじめに
「ZString」を Uniy プロジェクトに導入することで
string.Format や StringBuilder、TextMesh Pro の割り当てを削減できるようになります
string.Format
通常
using UnityEngine; public class Example : MonoBehaviour { private void Update() { var str = string.Format( "{0} / {1}", 25, 100 ); } }
ZString
using Cysharp.Text; using UnityEngine; public class Example : MonoBehaviour { private void Update() { var str = ZString.Format( "{0} / {1}", 25, 100 ); } }
StringBuilder
通常
using System.Text; using UnityEngine; public class Example : MonoBehaviour { private void Update() { var sb = new StringBuilder(); sb.AppendLine( "ピカチュウ" ); sb.AppendLine( 25 ); sb.AppendFormat( "{0} / {1}", 23, 34 ); var str = sb.ToString(); } }
ZString
using Cysharp.Text; using UnityEngine; public class Example : MonoBehaviour { private void Update() { using ( var sb = ZString.CreateStringBuilder() ) { sb.AppendLine( "ピカチュウ" ); sb.AppendLine( 25 ); sb.AppendFormat( "{0} / {1}", 23, 34 ); var str = sb.ToString(); } } }
TextMesh Pro
通常
using TMPro; using UnityEngine; public class Example : MonoBehaviour { public TMP_Text m_text; private void Update() { m_text.text = string.Format( "{0} + {1} + {2} = {3}", 1, 2, 3, 6 ); } }
ZString
using Cysharp.Text; using TMPro; using UnityEngine; public class Example : MonoBehaviour { public TMP_Text m_text; private void Update() { m_text.SetTextFormat( "{0} + {1} + {2} = {3}", 1, 2, 3, 6 ); } }