Я пытаюсь поместить холст над персонажем в моей игре, чтобы показать некоторую информацию о его анимации / здоровье и т. Д., Чтобы отладить его.Я делаю все это по коду.
Поэтому я сначала добавляю GameObject к Персонажу.Затем я добавляю Canvas к этому GameObject.Это отлично работает.
Затем я добавляю «Панель» к этому GameObject и помещаю в него изображение.Я хочу использовать это изображение в качестве фона текста, который будет отображаться.
Мне кажется, что я не могу создать панель такого размера, чтобы она идеально вписывалась в Canvas GameObject.
То же самое касается текста.
Что я здесь не так делаю?
Большое спасибо.
private void 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);
}
