C# 将图像附加到画布并以与画布相同的大小渲染

C# 将图像附加到画布并以与画布相同的大小渲染,c#,unity3d,C#,Unity3d,我试图在游戏中的角色上方放置一块画布,以显示有关其动画/健康状况等的一些信息,以便对其进行调试。 我是按代码来做的 因此,我首先向角色添加一个游戏对象。 然后我向这个游戏对象添加一个画布。 这个很好用 然后我向这个游戏对象添加一个“面板”,并在其中放入一个图像。我想使用此图像作为将要显示的文本的背景 我似乎无法使面板的大小使其完全适合画布游戏对象 文本也是如此 我做错了什么 多谢各位 私有void pCreateCanvas() { GameObject nCanvasGO=新游戏对象(“Can

我试图在游戏中的角色上方放置一块画布,以显示有关其动画/健康状况等的一些信息,以便对其进行调试。 我是按代码来做的

因此,我首先向角色添加一个游戏对象。 然后我向这个游戏对象添加一个画布。 这个很好用

然后我向这个游戏对象添加一个“面板”,并在其中放入一个图像。我想使用此图像作为将要显示的文本的背景

我似乎无法使面板的大小使其完全适合画布游戏对象

文本也是如此

我做错了什么

多谢各位

私有void pCreateCanvas() { GameObject nCanvasGO=新游戏对象(“CanvasContainer”); nCanvasGO.transform.SetParent(_thischacter.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);
}
Canvas nCanvas=nCanvasGO.AddComponent();//向游戏对象添加画布将自动将转换更改为矩形转换
nCanvas.renderMode=renderMode.WorldSpace;
nCanvasGO.AddComponent();
nCanvasGO.AddComponent();
//画布容器的矩形变换
RectTransform rtCanvasGO=nCanvasGO.GetComponent();//向游戏对象添加画布将自动将转换更改为RectTransform
rtCanvasGO.localScale=new Vector3(0.01f,0.01f,1f);//将其缩小以适合场景
rtCanvasGO.rotation=Quaternion.Euler(0,180,0);//旋转它,使它面向我
rtCanvasGO.localPosition=new Vector3(0,2,0);/y=2米(将画布游戏对象放置在角色脚的2米处=其头部上方)
rtCanvasGO.anchorMin=新向量2(0,0);
rtCanvasGO.anchorMax=新向量2(0,0);
rtCanvasGO.sizeDelta=新向量2(100,10);
游戏对象nPanelGO=新游戏对象(“面板”);
nPanelGO.transform.SetParent(nCanvasGO.transform,false);//将其设置为nCanvasGO的父对象
nPanelGO.AddComponent();//wird benötigt,bisher是一个nur-ein变换,kein RectTransform(das Anchor usw.hat)
nPanelGO.AddComponent();
//PanelContainer的矩形变换
RectTransform rtPanelGO=nPanelGO.GetComponent();
rtPanelGO.localPosition=newvector3(0,0,0);
rtPanelGO.anchorMin=新向量2(0,0);
rtPanelGO.anchorMax=新向量2(1,1);
rtPanelGO.pivot=新向量2(0,0);
rtPanelGO.localScale=新向量3(1,1,1);
rtPanelGO.sizeDelta=新向量2(100,10);
Image nImage=nPanelGO.AddComponent();
nImage.color=color.red;
GameObject nTextGO=新游戏对象(“文本持有者”);
nTextGO.transform.SetParent(nPanelGO.transform,false);//使其成为自己的子级
_text=nTextGO.AddComponent();
Font-ArialFont=(Font)Resources.GetBuiltinResource(typeof(Font),“Arial.ttf”);
_text.font=ArialFont;
_text.material=ArialFont.material;
//文本的矩形变换
RectTransform rtText=_text.GetComponent();
//rtText.localPosition=不确定在此执行什么操作
rtText.anchorMin=新向量2(0,0);
rtText.anchorMax=新向量2(0,0);
rtText.pivot=新向量2(0,0);
rtText.localScale=newvector3(1,1,1);
rtText.sizeDelta=新向量2(100,10);
rtText.localPosition=newvector3(-50,0,0);
}
而不是

 rtPanelGO.anchorMin = new Vector2(0, 0);
 rtPanelGO.anchorMax = new Vector2(1, 1);
试一试

更新: 关键是使用recttransform,当然,请记住,对它所做的某些更改可能对创建它的帧没有影响。当画布和所有游戏对象有效地添加到场景中时,您可以尝试等待1帧,然后将所有更改应用到面板。例如,使用协同程序

有关设置顶部和底部偏移的更多信息,请参阅unity论坛上的

更新2: Talke也看看这个方法:

为什么不先使用编辑器创建所有结构,并添加自定义脚本来处理与显示所需视觉信息相关的所有人员,另存为预设,然后角色可以实例化此预设,并使用其API来可视化所需的所有信息,而无需担心如何操作(包括“如何操作”)在你的数据可视化预设中)。你应该能够将
anchorMin
设置为
Vector.zero
anchorMax
设置为
Vector.one
,然后将
localPosition
设置为
Vector3.zero
@misher我不想使用预设,我感觉更“安全”使用script@tmighty,如果应用可靠的原则,您将更加安全,unity editor还为您提供了可能应该使用的有用工具,否则,您失去了拥有引擎的好处您还可以通过在创建panel rect transform时获取对panel rect transform的引用来简化代码:RectTransform rtPanelGO=nPanelGO.AddComponent();工作很好,非常感谢!!你不知道我今天试了多少小时。
 rtPanelGO.offsetMin= new Vector2(0, 0);
 rtPanelGO.offsetMax = new Vector2(0, 0);