Skip to content

Instantly share code, notes, and snippets.

@esperecyan
Last active January 9, 2021 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esperecyan/c62b5dbb06e7b70383dd255bf106a2e2 to your computer and use it in GitHub Desktop.
Save esperecyan/c62b5dbb06e7b70383dd255bf106a2e2 to your computer and use it in GitHub Desktop.
『ReplaceToTextMeshPro.cs』VCI用にTextコンポートをTextMeshProコンポーネントへ簡易的に置き換えます。シーン内のオブジェクトを選択してからコンテキストメニューの UnityEditorScripts メニュー内から使用できます。 https://pokemori.booth.pm/items/2662865
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using TMPro;
namespace Esperecyan.Unity.ReplaceToTextMeshPro
{
/// <summary>
/// VCI用にTextコンポートをTextMeshProコンポーネントへ簡易的に置き換えます。
/// </summary>
/// <remarks>
/// 動作確認バージョン: Unity 2018.4.20f1, TextMesh Pro 1.4.1
/// SPDX-License-Identifier: MPL-2.0
/// (Mozilla Public License 2.0) <https://spdx.org/licenses/MPL-2.0.html>
/// Author: 100の人
/// 配布元: <https://gist.github.com/esperecyan/>
/// </remarks>
public class ReplaceToTextMeshPro
{
/// <summary>
/// VCIで使用される「Assets/Fonts/NotoSansCJK-Bold_SDF.asset」のGUID。
/// </summary>
/// <remarks>
/// フォントに関して | VCI Text (TextMeshPro) | バーチャルキャスト公式Wiki
/// https://virtualcast.jp/wiki/doku.php?id=vci:sdk:text:introduction#%E3%83%95%E3%82%A9%E3%83%B3%E3%83%88%E3%81%AB%E9%96%A2%E3%81%97%E3%81%A6
/// </remarks>
private static readonly string VCIFontAssetGUID = "9da32abc4280dd3488ba8ae9c73d2fcc";
/// <summary>
/// 追加するメニューアイテムの、「UnityEditorScripts」メニュー内の位置。
/// </summary>
private const int Priority = 22;
[MenuItem("GameObject/UnityEditorScripts/オブジェクト内のTextコンポーネントをText Mesh Proへ置換する", false, ReplaceToTextMeshPro.Priority)]
private static void Replace()
{
var font = AssetDatabase
.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(ReplaceToTextMeshPro.VCIFontAssetGUID));
foreach (var text
in Selection.transforms.SelectMany(transform => transform.GetComponentsInChildren<Text>(true)))
{
var tmpText = Object.Instantiate(text);
var gameObject = text.gameObject;
Undo.DestroyObjectImmediate(text);
var textMeshPro = Undo.AddComponent<TextMeshPro>(gameObject);
try
{
textMeshPro.font = font;
}
catch (System.NullReferenceException)
{
// 原因不明の例外を抑制
}
textMeshPro.text = tmpText.text;
textMeshPro.fontStyle = ReplaceToTextMeshPro.ToFontStyles(tmpText.fontStyle);
textMeshPro.enableAutoSizing = tmpText.resizeTextForBestFit;
textMeshPro.color = tmpText.color;
textMeshPro.alignment = ReplaceToTextMeshPro.ToTextAlignmentOptions(tmpText.alignment);
textMeshPro.enableWordWrapping = tmpText.horizontalOverflow == HorizontalWrapMode.Wrap;
textMeshPro.richText = tmpText.supportRichText;
// 拡大/縮小のリセット
var globalScaleX = gameObject.transform.lossyScale.x;
gameObject.transform.localScale = new Vector3(1 / globalScaleX, 1 / globalScaleX, 1 / globalScaleX);
textMeshPro.rectTransform.sizeDelta = tmpText.rectTransform.sizeDelta * globalScaleX;
textMeshPro.fontSize = 1;
if (textMeshPro.richText)
{
textMeshPro.text = Regex.Replace(textMeshPro.text, "<size=[0-9]+>", "<size=1>");
}
EditorUtility.SetDirty(textMeshPro);
Object.DestroyImmediate(tmpText.gameObject);
}
}
private static FontStyles ToFontStyles(FontStyle fontStyle)
{
switch (fontStyle)
{
case FontStyle.Bold:
return FontStyles.Bold;
case FontStyle.Italic:
return FontStyles.Italic;
case FontStyle.BoldAndItalic:
return FontStyles.Bold | FontStyles.Italic;
default:
return FontStyles.Normal;
}
}
private static TextAlignmentOptions ToTextAlignmentOptions(TextAnchor textAnchor)
{
switch (textAnchor)
{
case TextAnchor.UpperCenter:
return TextAlignmentOptions.Top;
case TextAnchor.UpperRight:
return TextAlignmentOptions.TopRight;
case TextAnchor.MiddleLeft:
return TextAlignmentOptions.MidlineLeft;
case TextAnchor.MiddleCenter:
return TextAlignmentOptions.Midline;
case TextAnchor.MiddleRight:
return TextAlignmentOptions.MidlineRight;
case TextAnchor.LowerLeft:
return TextAlignmentOptions.BottomLeft;
case TextAnchor.LowerCenter:
return TextAlignmentOptions.Bottom;
case TextAnchor.LowerRight:
return TextAlignmentOptions.BottomRight;
default:
return TextAlignmentOptions.TopLeft;
}
}
}
}
fileFormatVersion: 2
guid: 54b1487a6e22dc1438bf5447afc5aa41
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment