You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.7 KiB
C#
92 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using ArabicSupport;
|
|
|
|
|
|
public class CreateScrollableText : MonoBehaviour
|
|
{
|
|
public Font fallbackFont; // Optional fallback for TMP
|
|
|
|
[Header("Localization")]
|
|
public string localizationKey = "example.key";
|
|
public Canvas canvas;
|
|
void Start()
|
|
{
|
|
|
|
// Create Scroll View
|
|
GameObject scrollViewGO = new GameObject("ScrollView", typeof(RectTransform), typeof(ScrollRect), typeof(Image));
|
|
scrollViewGO.transform.SetParent(canvas.transform, false);
|
|
RectTransform scrollViewRT = scrollViewGO.GetComponent<RectTransform>();
|
|
scrollViewRT.sizeDelta = new Vector2(500, 300);
|
|
scrollViewRT.anchoredPosition = Vector2.zero;
|
|
|
|
// Background image
|
|
scrollViewGO.GetComponent<Image>().color = new Color(0.9f, 0.9f, 0.9f, 0.5f);
|
|
|
|
// Viewport
|
|
GameObject viewportGO = new GameObject("Viewport", typeof(RectTransform), typeof(Image), typeof(Mask));
|
|
viewportGO.transform.SetParent(scrollViewGO.transform, false);
|
|
RectTransform viewportRT = viewportGO.GetComponent<RectTransform>();
|
|
viewportRT.anchorMin = Vector2.zero;
|
|
viewportRT.anchorMax = Vector2.one;
|
|
viewportRT.offsetMin = Vector2.zero;
|
|
viewportRT.offsetMax = Vector2.zero;
|
|
viewportGO.GetComponent<Image>().color = Color.white;
|
|
viewportGO.GetComponent<Mask>().showMaskGraphic = false;
|
|
|
|
// Content
|
|
GameObject contentGO = new GameObject("Content", typeof(RectTransform), typeof(ContentSizeFitter), typeof(VerticalLayoutGroup));
|
|
contentGO.transform.SetParent(viewportGO.transform, false);
|
|
RectTransform contentRT = contentGO.GetComponent<RectTransform>();
|
|
contentRT.anchorMin = new Vector2(0, 1);
|
|
contentRT.anchorMax = new Vector2(1, 1);
|
|
contentRT.pivot = new Vector2(0.5f, 1);
|
|
contentRT.anchoredPosition = Vector2.zero;
|
|
|
|
var scrollRect = scrollViewGO.GetComponent<ScrollRect>();
|
|
scrollRect.viewport = viewportRT;
|
|
scrollRect.content = contentRT;
|
|
scrollRect.horizontal = false;
|
|
scrollRect.vertical = true;
|
|
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
|
|
|
// Content Sizing
|
|
var csf = contentGO.GetComponent<ContentSizeFitter>();
|
|
csf.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
|
|
|
var layoutGroup = contentGO.GetComponent<VerticalLayoutGroup>();
|
|
layoutGroup.childControlHeight = true;
|
|
layoutGroup.childForceExpandHeight = false;
|
|
layoutGroup.childAlignment = TextAnchor.UpperLeft;
|
|
|
|
// Text Element with LocalizedTextComponent
|
|
GameObject textGO = new GameObject("LocalizedText", typeof(RectTransform), typeof(TextMeshProUGUI), typeof(LocalizedTextComponent));
|
|
textGO.transform.SetParent(contentGO.transform, false);
|
|
|
|
RectTransform textRT = textGO.GetComponent<RectTransform>();
|
|
textRT.anchorMin = new Vector2(0, 1);
|
|
textRT.anchorMax = new Vector2(1, 1);
|
|
textRT.pivot = new Vector2(0.5f, 1);
|
|
textRT.offsetMin = new Vector2(10, 0); // Padding
|
|
textRT.offsetMax = new Vector2(-10, 0);
|
|
|
|
// Configure TMP
|
|
var tmp = textGO.GetComponent<TextMeshProUGUI>();
|
|
tmp.fontSize = 24;
|
|
tmp.enableWordWrapping = true;
|
|
tmp.overflowMode = TextOverflowModes.Overflow;
|
|
|
|
if (fallbackFont)
|
|
tmp.font = TMP_FontAsset.CreateFontAsset(fallbackFont);
|
|
|
|
// LocalizedTextComponent setup
|
|
var localizedText = textGO.GetComponent<LocalizedTextComponent>();
|
|
localizedText.localizationKey = localizationKey;
|
|
|
|
// Trigger the update manually if LanguageManager is ready
|
|
if (LanguageManager.Instance != null)
|
|
localizedText.UpdateText();
|
|
}
|
|
}
|