代码之家  ›  专栏  ›  技术社区  ›  tmighty

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

  •  0
  • tmighty  · 技术社区  · 6 年前

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

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

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

    文本也是如此。

    我做错了什么?

    私有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);
    }
    

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   kolodi    6 年前

    而不是

     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

    更新2: https://docs.unity3d.com/ScriptReference/RectTransform.SetInsetAndSizeFromParentEdge.html