【发布时间】:2021-08-12 21:10:39
【问题描述】:
在我的统一应用程序中,我需要制作带有字符串的小矩形组件。但是,我希望矩形的大小动态对齐其中的文本。这是我的预制件中的层次结构:
根据this article,我只需要在画布上添加一个内容调整器。我这样做了,并将水平和垂直尺寸都设置为“首选”,但显然宽度只是 0(我的 Debug.Log 打印出 0)。这是我的代码。我认为问题可能与更改渲染模式有关,但这是针对 VR 应用程序,因此需要保留:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Script : MonoBehaviour
{
public static string s_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
public Transform panel;
public GameObject wordPrefab;
private float panelWidth;
private float panelHeight;
// Start is called before the first frame update
void Start()
{
panelWidth = panel.GetComponentInParent<Canvas>().GetComponent<RectTransform>().rect.width;
panelHeight = panel.GetComponentInParent<Canvas>().GetComponent<RectTransform>().rect.height;
Vector3 nextPosition = new Vector3(-panelWidth / 2, panelHeight / 2, 0);
foreach (string word in s_text.Split(' '))
{
GameObject go = Instantiate(wordPrefab, panel, false);
go.GetComponentInChildren<Canvas>().renderMode = RenderMode.WorldSpace;
go.GetComponentInChildren<Text>().text = word;
go.transform.localPosition = nextPosition;
float wordWidth = go.GetComponentInChildren<Canvas>().GetComponent<RectTransform>().rect.width;
Debug.Log(wordWidth); // This is 0 every time when it shouldn't be!
nextPosition = 0; // I will figure this out once I can access the size of the blocks
}
}
// Update is called once per frame
void Update()
{
}
}
TL;DR:我需要正确调整父画布的大小,并且由于某种原因,在完全按照链接文章所说的内容并使用内容调整器之后,它的宽度/高度为 0。
【问题讨论】:
-
同时向画布添加水平或垂直布局组。您无需尝试动态缩放代码中的任何内容。仅使用 UI 组件就可以完全实现。如果您需要,我可以发布更具体的答案。
-
可以吗?听起来很完美
-
为了清楚起见,您是否要将整个画布重新缩放为文本对象?或者你只是想要一些父对象重新缩放到文本对象。您是否有特定原因希望重新调整画布本身的比例?
-
我只需要一些父对象来缩放那么多,不管它是否是画布。我添加了一个水平布局组并使用了设置,但我无法让它适合文本。要么是固定宽度,要么是 0
-
您希望文本为固定大小吗?还是希望对象缩放到其中的文本量?并且宽度或高度应该是固定大小。您希望它按高度增长还是按宽度增长?