forked from hk1ll3r/RTLTMPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Editor Script to convert TMP to RTLTMP #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using TMPro; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
| using UnityEngine.UI; | ||
|
|
||
| namespace RTLTMPro | ||
| { | ||
| public class ConvertToTextMeshPro | ||
| { | ||
| [MenuItem("CONTEXT/Text/Convert to RTLTMPro")] | ||
| public static void ConvertLegacyText(MenuCommand command) | ||
| { | ||
| var text = (Text)command.context; | ||
| Convert(text); | ||
| } | ||
|
|
||
| private static void Convert(Text from) | ||
| { | ||
| var gameObject = from.gameObject; | ||
|
|
||
| var font = from.font; | ||
| var fontSize = from.fontSize; | ||
| var color = from.color; | ||
| var alignment = from.alignment; | ||
| var text = from.text; | ||
| var fontStyle = from.fontStyle; | ||
| var lineSpacing = from.lineSpacing; | ||
| var supportRichText = from.supportRichText; | ||
| var resizeTextMaxSize = from.resizeTextMaxSize; | ||
| var resizeTextMinSize = from.resizeTextMinSize; | ||
| var resizeTextForBestFit = from.resizeTextForBestFit; | ||
| var enabled = from.enabled; | ||
| var maskable = from.maskable; | ||
| var material = from.material; | ||
| var raycastTarget = from.raycastTarget; | ||
| var raycastPadding = from.raycastPadding; | ||
|
|
||
| Undo.DestroyObjectImmediate(from); | ||
| var to = Undo.AddComponent<RTLTextMeshPro>(gameObject); | ||
|
|
||
| EditorGUI.BeginChangeCheck(); | ||
|
|
||
| to.text = text; | ||
| to.color = color; | ||
| to.alignment = alignment switch | ||
| { | ||
| TextAnchor.UpperLeft => TextAlignmentOptions.TopLeft, | ||
| TextAnchor.UpperCenter => TextAlignmentOptions.Top, | ||
| TextAnchor.UpperRight => TextAlignmentOptions.TopRight, | ||
| TextAnchor.MiddleLeft => TextAlignmentOptions.Left, | ||
| TextAnchor.MiddleCenter => TextAlignmentOptions.Center, | ||
| TextAnchor.MiddleRight => TextAlignmentOptions.Right, | ||
| TextAnchor.LowerLeft => TextAlignmentOptions.BottomLeft, | ||
| TextAnchor.LowerCenter => TextAlignmentOptions.Bottom, | ||
| TextAnchor.LowerRight => TextAlignmentOptions.BottomRight, | ||
| _ => throw new ArgumentOutOfRangeException() | ||
| }; | ||
|
|
||
| var possibleFonts = AssetDatabase.FindAssets($"t:{nameof(TMP_FontAsset)}") | ||
| .Select(AssetDatabase.GUIDToAssetPath) | ||
| .Select(AssetDatabase.LoadAssetAtPath<TMP_FontAsset>); | ||
|
|
||
| bool IsMatchingFont(TMP_FontAsset f) | ||
| { | ||
| //We need to use reflection because the font property is null when population mode is set to static | ||
| const BindingFlags flags = BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic; | ||
| var fontSourceFile = typeof(TMP_FontAsset) | ||
| .GetField("m_SourceFontFile_EditorRef", flags) | ||
| !.GetValue(f) as Font; | ||
|
|
||
| return fontSourceFile == font; | ||
| } | ||
|
|
||
| to.font = possibleFonts.FirstOrDefault(IsMatchingFont); | ||
| to.fontSize = fontSize; | ||
|
|
||
| to.fontStyle = fontStyle switch | ||
| { | ||
| FontStyle.Normal => FontStyles.Normal, | ||
| FontStyle.Bold => FontStyles.Bold, | ||
| FontStyle.Italic => FontStyles.Italic, | ||
| FontStyle.BoldAndItalic => FontStyles.Bold | FontStyles.Italic, | ||
| _ => throw new ArgumentOutOfRangeException() | ||
| }; | ||
|
|
||
| to.lineSpacing = lineSpacing; | ||
| to.richText = supportRichText; | ||
| to.fontSizeMax = resizeTextMaxSize; | ||
| to.fontSizeMin = resizeTextMinSize; | ||
| to.enableAutoSizing = resizeTextForBestFit; | ||
| to.enabled = enabled; | ||
| to.maskable = maskable; | ||
| to.material = material; | ||
| to.raycastTarget = raycastTarget; | ||
| to.raycastPadding = raycastPadding; | ||
|
|
||
| if (EditorGUI.EndChangeCheck()) | ||
| Undo.RecordObject(to, "Copy from old text"); | ||
| } | ||
|
|
||
| [MenuItem("CONTEXT/TextMeshProUGUI/Convert to RTLTMPro", true)] | ||
| public static bool ConvertTextMeshProUGUIValidate(MenuCommand command) | ||
| => command.context is TextMeshProUGUI and not RTLTextMeshPro; | ||
|
|
||
| [MenuItem("CONTEXT/TextMeshProUGUI/Convert to RTLTMPro")] | ||
| public static void ConvertTextMeshProUGUI(MenuCommand command) | ||
| { | ||
| var text = (TextMeshProUGUI)command.context; | ||
| Convert(text); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unfortunately I'm not sure how to do GUID swapping, so references to the text component will break, but at least it should make the transition much safer and quicker | ||
| /// </summary> | ||
| /// <param name="from"></param> | ||
| private static void Convert(TextMeshProUGUI from) | ||
| { | ||
| var gameObject = from.gameObject; | ||
|
|
||
| var margins = from.margin; | ||
| var textPreprocessor = from.textPreprocessor; | ||
| var text = from.text; | ||
| var autoSizeTextContainer = from.autoSizeTextContainer; | ||
| var font = from.font; | ||
| var color = from.color; | ||
| var alignment = from.alignment; | ||
| var fontStyle = from.fontStyle; | ||
| var lineSpacing = from.lineSpacing; | ||
| var richText = from.richText; | ||
| var fontSize = from.fontSize; | ||
| var fontSizeMax = from.fontSizeMax; | ||
| var fontSizeMin = from.fontSizeMin; | ||
| var enableAutoSizing = from.enableAutoSizing; | ||
| var enabled = from.enabled; | ||
| var maskable = from.maskable; | ||
| var raycastTarget = from.raycastTarget; | ||
| var raycastPadding = from.raycastPadding; | ||
| var fontSharedMaterial = from.fontSharedMaterial; | ||
| var fontSharedMaterials = from.fontSharedMaterials; | ||
| var enableVertexGradient = from.enableVertexGradient; | ||
| var colorGradient = from.colorGradient; | ||
| var colorGradientPreset = from.colorGradientPreset; | ||
| var spriteAsset = from.spriteAsset; | ||
| var tintAllSprites = from.tintAllSprites; | ||
| var styleSheet = from.styleSheet; | ||
| var textStyle = from.textStyle; | ||
| var overrideColorTags = from.overrideColorTags; | ||
| var fontWeight = from.fontWeight; | ||
| var characterSpacing = from.characterSpacing; | ||
| var wordSpacing = from.wordSpacing; | ||
| var lineSpacingAdjustment = from.lineSpacingAdjustment; | ||
| var paragraphSpacing = from.paragraphSpacing; | ||
| var characterWidthAdjustment = from.characterWidthAdjustment; | ||
| var enableWordWrapping = from.enableWordWrapping; | ||
| var wordWrappingRatios = from.wordWrappingRatios; | ||
| var overflowMode = from.overflowMode; | ||
| var linkedTextComponent = from.linkedTextComponent; | ||
|
|
||
| Undo.DestroyObjectImmediate(from); | ||
| var to = Undo.AddComponent<RTLTextMeshPro>(gameObject); | ||
|
|
||
| EditorGUI.BeginChangeCheck(); | ||
|
|
||
| to.UpdateText(); | ||
|
|
||
| //Initialize the canvas renderer | ||
| _ = to.canvasRenderer; | ||
|
|
||
| //Initialize the mesh using reflection | ||
| var mesh = new Mesh | ||
| { | ||
| hideFlags = HideFlags.HideAndDontSave, | ||
| name = "TextMeshPro UI Mesh" | ||
| }; | ||
|
|
||
| typeof(TMP_Text) | ||
| .GetField("m_mesh", BindingFlags.Instance | BindingFlags.SetField | BindingFlags.NonPublic) | ||
| !.SetValue(to, mesh); | ||
|
|
||
| to.GetTextInfo(to.text); | ||
| // rtlText.UpdateFontAsset(); | ||
| // rtlText.UpdateVertexData(); | ||
| _ = to.GetPreferredValues(); | ||
|
|
||
|
|
||
|
|
||
| to.margin = margins; | ||
| to.fontSharedMaterials = fontSharedMaterials; | ||
| to.fontSharedMaterial = fontSharedMaterial; | ||
| to.textPreprocessor = textPreprocessor; | ||
| to.text = text; | ||
| to.autoSizeTextContainer = autoSizeTextContainer; | ||
| to.font = font; | ||
| to.color = color; | ||
| to.alignment = alignment; | ||
| to.fontStyle = fontStyle; | ||
| to.lineSpacing = lineSpacing; | ||
| to.richText = richText; | ||
| to.fontSize = fontSize; | ||
| to.fontSizeMax = fontSizeMax; | ||
| to.fontSizeMin = fontSizeMin; | ||
| to.enableAutoSizing = enableAutoSizing; | ||
| to.enabled = enabled; | ||
| to.maskable = maskable; | ||
| to.raycastTarget = raycastTarget; | ||
| to.raycastPadding = raycastPadding; | ||
| to.enableVertexGradient = enableVertexGradient; | ||
| to.colorGradient = colorGradient; | ||
| to.colorGradientPreset = colorGradientPreset; | ||
| to.spriteAsset = spriteAsset; | ||
| to.tintAllSprites = tintAllSprites; | ||
| to.styleSheet = styleSheet; | ||
| to.textStyle = textStyle; | ||
| to.overrideColorTags = overrideColorTags; | ||
| to.fontWeight = fontWeight; | ||
| to.characterSpacing = characterSpacing; | ||
| to.wordSpacing = wordSpacing; | ||
| to.lineSpacingAdjustment = lineSpacingAdjustment; | ||
| to.paragraphSpacing = paragraphSpacing; | ||
| to.characterWidthAdjustment = characterWidthAdjustment; | ||
| to.enableWordWrapping = enableWordWrapping; | ||
| to.wordWrappingRatios = wordWrappingRatios; | ||
| to.overflowMode = overflowMode; | ||
| to.linkedTextComponent = linkedTextComponent; | ||
|
|
||
| if (EditorGUI.EndChangeCheck()) | ||
| Undo.RecordObject(to, "Copy from old text"); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Assets/RTLTMPro/Scripts/Editor/ConvertToTextMeshPro.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this asset handled the conversion to TMPro without losing references. Breaking existing references is not an option, there are thousands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what this asset does. We've already handled that in https://github.com/engage-xr/Engage_Main/pull/2004
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do raise a valid point though, if we go from TMP -> RTLTMP we would still potentially lose some references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant references in other components, #2004 only handled references in code, but not from other non-code components.