Hello,
I’m currently learning to use xenko and I started fiddling around with some things. Now I came to a point where I wanted to draw the name and the healthbar above an moveable entity. With some trial and error I achieved it with this code.
var uiEntity = new Entity(playerShip.Name + “Info”);
UIComponent uiComponent = new UIComponent();
uiComponent.IsFullScreen = false;
uiComponent.IsBillboard = true;
uiComponent.SnapText = true;
uiComponent.VirtualResolution = new Vector3(Game.GraphicsDevice.BackBuffer.Width, Game.GraphicsDevice.BackBuffer.Height, 1000f);
var stackPanel = new StackPanel()
{
Orientation = SiliconStudio.Xenko.UI.Orientation.Vertical,
Margin = new SiliconStudio.Xenko.UI.Thickness(5f, 2f, 5f, 5f)
};
float height = 3f;
string unitName = playerShip.Name;
unitName = Helper.TruncateString(unitName, 15, "...");
unitNameTextBlock = new TextBlock
{
Font = UnitNameFont,
Margin = new SiliconStudio.Xenko.UI.Thickness(0f, -3f, 0f, 0f),
Text = unitName,
HorizontalAlignment = SiliconStudio.Xenko.UI.HorizontalAlignment.Center,
Height = TEXT_HEIGHT
};
stackPanel.Children.Add(unitNameTextBlock);
height += TEXT_HEIGHT;
//lifeTextBlock = new TextBlock()
//{
// Font = UnitNameFont,
// Text = string.Format("{0} / {1}", Life, LifeMax),
// TextSize = 10f,
// HorizontalAlignment = SiliconStudio.Xenko.UI.HorizontalAlignment.Center,
// VerticalAlignment = SiliconStudio.Xenko.UI.VerticalAlignment.Center,
// Margin = new SiliconStudio.Xenko.UI.Thickness(0f, -2f, 0f, 0f)
//};
lifeBar = new ContentDecorator()
{
BackgroundColor = Color.DarkRed,
Content = lifeTextBlock,
Height = BAR_HEIGHT,
Width = Life / LifeMax * BAR_MAX_WIDTH,
};
stackPanel.Children.Add(lifeBar);
height += BAR_HEIGHT;
if (HasShield)
{
shieldBar = new ContentDecorator()
{
BackgroundColor = Color.DeepSkyBlue,
Height = BAR_HEIGHT,
Width = Life / LifeMax * BAR_MAX_WIDTH,
};
stackPanel.Children.Add(shieldBar);
height += BAR_HEIGHT;
}
var contentElement = new ContentDecorator
{
BackgroundColor = new Color(0.1f, 0.1f, 0.1f, 0.8f),
Content = stackPanel,
Height = height,
Width = UI_MAX_WIDTH,
};
uiSize = new Vector2(contentElement.Width, contentElement.Height);
uiComponent.RootElement = contentElement;
uiEntity.Add<UIComponent>(UIComponent.Key, uiComponent);
uiEntity.Transform.Position = calcUiPosition();
The problem with this approach is that the UI wont scale if the camera moves further away from the object. When I use IsFullscreen = true, then the UI is not affected from the camera, but I cant position it above a moveable Unit. In fact I wont be drawn at all if I set the Position of the entity.
My question is now: What’s the best approach here to get an UI which doesnt get smaller if the camera moves further away, but is able to be positioned freely on screen? Can I set some sort of absolute position? Do I need to use something completely different?
Thanks for your advice