c# - 将图像附加到 Canvas 并将其渲染为与 Canvas 相同的大小
问题描述
我试图在我的游戏中的角色上方放置一个画布,以显示有关其动画/健康等的一些信息,以便对其进行调试。我正在通过代码完成所有这些工作。
因此,我首先向角色添加了一个游戏对象。然后我向这个游戏对象添加一个画布。这工作正常。
然后我向这个游戏对象添加一个“面板”并将图像放入其中。我想将此图像用作将要显示的文本的背景。
我似乎无法将面板制作成完全适合 Canvas GameObject 的大小。
文本也是如此。
我在这里做错了什么?
非常感谢。
私人无效 pCreateCanvas() { GameObject 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);
}
解决方案
代替
rtPanelGO.anchorMin = new Vector2(0, 0);
rtPanelGO.anchorMax = new Vector2(1, 1);
尝试
rtPanelGO.offsetMin= new Vector2(0, 0);
rtPanelGO.offsetMax = new Vector2(0, 0);
更新:关键是使用 recttransform,当然,请记住,您对其所做的一些更改不会影响您创建它的框架。当画布和所有游戏对象有效添加到场景中时,您可以尝试等待 1 帧,然后将所有更改应用到面板。例如,使用协程。
有关设置顶部和底部偏移的更多信息,请参阅统一论坛上的https://forum.unity.com/threads/setting-top-and-bottom-on-a-recttransform.265415 。
UPDATE2:也看看这个方法: https ://docs.unity3d.com/ScriptReference/RectTransform.SetInsetAndSizeFromParentEdge.html
推荐阅读
- javascript - 使用 vue 在元素 ui 树的子项中显示嵌套文件
- python - 无法读取从 python 上的 TCP 客户端发送到 C# 中的 TCP 服务器的数据
- batch-file - 如何检查变量是否批量包含字母/短语?
- awk - 如何检查AWK脚本中的字符串数组是否包含值
- c - C 宏中的参数名称是否会对以前的定义产生副作用?
- java - 将 Jackson 与具有私有 Builder 的不可变类一起使用
- scala - 使用类型类时类型不匹配
- c++ - C++ 模板覆盖规则
- python-3.x - Discord.py 禁止不在我服务器中的成员
- java - AWS Lambda Java 错误:ClassNotFoundException