【发布时间】:2019-01-31 17:13:39
【问题描述】:
我正在尝试在游戏中的角色上方放置一个画布,以显示有关其动画/健康状况等的一些信息,以便对其进行调试。 这一切我都是通过代码来完成的。
因此,我首先向角色添加了一个游戏对象。 然后我向这个游戏对象添加一个画布。 这工作正常。
然后我向这个游戏对象添加一个“面板”并将图像放入其中。我想将此图像用作将要显示的文本的背景。
我似乎无法将面板制作成完全适合 Canvas GameObject 的大小。
文本也是如此。
我在这里做错了什么?
非常感谢。
私人无效 pCreateCanvas() { 游戏对象 nCanvasGO = new GameObject("CanvasContainer"); nCanvasGO.transform.SetParent(_ThisCharacter.transform); //将游戏对象作为角色的父对象
Canvas nCanvas = nCanvasGO.AddComponent<Canvas>();//Adding a canvas to a Gameobject will automatically change the Transform to a RectTransform
nCanvas.renderMode = RenderMode.WorldSpace;
nCanvasGO.AddComponent<CanvasScaler>();
nCanvasGO.AddComponent<GraphicRaycaster>();
//CanvasContainer's RectTransform
RectTransform rtCanvasGO = nCanvasGO.GetComponent<RectTransform>();// Adding a canvas to a Gameobject will automatically change the Transform to a RectTransform
rtCanvasGO.localScale = new Vector3(0.01f, 0.01f, 1f); //scale it down so that it fits in the scene
rtCanvasGO.rotation = Quaternion.Euler(0, 180, 0);//rotate it so that it faces me
rtCanvasGO.localPosition = new Vector3(0, 2, 0); //y=2 m (place the canvas game object 2 metres of the character's feet = over it's head)
rtCanvasGO.anchorMin = new Vector2(0, 0);
rtCanvasGO.anchorMax = new Vector2(0, 0);
rtCanvasGO.sizeDelta = new Vector2(100, 10);
GameObject nPanelGO = new GameObject("Panel");
nPanelGO.transform.SetParent(nCanvasGO.transform, false);//parent it to the nCanvasGO
nPanelGO.AddComponent<RectTransform>();//wird benötigt, bisher ist es nur ein Transform, kein RectTransform (das Anchor usw. hat)
nPanelGO.AddComponent<CanvasRenderer>();
//PanelContainer's RectTransform
RectTransform rtPanelGO = nPanelGO.GetComponent<RectTransform>();
rtPanelGO.localPosition = new Vector3(0, 0, 0);
rtPanelGO.anchorMin = new Vector2(0, 0);
rtPanelGO.anchorMax = new Vector2(1, 1);
rtPanelGO.pivot = new Vector2(0, 0);
rtPanelGO.localScale = new Vector3(1, 1, 1);
rtPanelGO.sizeDelta = new Vector2(100, 10);
Image nImage = nPanelGO.AddComponent<Image>();
nImage.color = Color.red;
GameObject nTextGO = new GameObject("TextHolder");
nTextGO.transform.SetParent(nPanelGO.transform, false);//make it a child of its own
_text = nTextGO.AddComponent<Text>();
Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
_text.font = ArialFont;
_text.material = ArialFont.material;
//Text's RectTransform
RectTransform rtText = _text.GetComponent<RectTransform>();
//rtText.localPosition = Not sure what to do here
rtText.anchorMin = new Vector2(0, 0);
rtText.anchorMax = new Vector2(0, 0);
rtText.pivot = new Vector2(0, 0);
rtText.localScale = new Vector3(1, 1, 1);
rtText.sizeDelta = new Vector2(100, 10);
rtText.localPosition = new Vector3(-50, 0, 0);
}
【问题讨论】:
-
为什么不使用编辑器预先创建所有结构并添加自定义脚本来处理与显示所需视觉信息相关的所有人员,保存为预制件,然后您的角色可以实例化此预制件并使用它的 API 来可视化你需要的所有信息,而不用担心它会如何做(在你的数据可视化预制件中封装“方法”)。
-
您应该能够将
anchorMin设置为Vector.zero,将anchorMax设置为Vector.one,然后将localPosition设置为Vector3.zero。 -
@misher 我不想使用预制件,我觉得脚本更“安全”。
-
@tmighty 如果您应用 solid 原则,您将更加安全,并且统一编辑器还为您提供了您可能应该使用的有用工具,否则,您将失去拥有的好处一个引擎
-
您还可以通过在创建面板矩形变换时获取对面板矩形变换的引用来简化代码:RectTransform rtPanelGO = nPanelGO.AddComponent
();