Unity Gui fontSize dont change - c#

I'm using this code below to add a Retry and Quit button to the GameOver screen for my android game.
//if retry button is pressed load scene 0 the game
if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2 +100,200,140),"Retry?")){
Application.LoadLevel(0);
}
//and quit button
if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2 +200,200,140),"Quit")){
Application.Quit();
}
But the fontSize of the Gui text is so small and no matter what I try then I cant make it bigger. What can I do to solve this.

Use style object for this.
GUIStyle style = new GUIStyle();
style.fontSize = 20;
if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2 +100,200,140),"Retry?", style)){
Application.LoadLevel(0);
}
//and quit button
if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2 +200,200,140),"Quit", style)){
Application.Quit();
}
But it is going to overwrite the original style so you probably want to make some other changes too. For example put these lines after declaration of ´style´ and before the first use of it:
style.alignment = TextAnchor.MiddleCenter;
RectOffset margin = new RectOffset();
margin.bottom = 10;
margin.top = 10;
style.margin = margin;
style.normal.background = new Texture2D(1, 1);
For all possible settings check unity's manuals.

Related

Xamarin Forms IOS - MKUserTrackingButton position

I currently have a Custom map renderer in my Xamarin Forms that use for each platform the native Map renderer.
For iOS, I'm trying to add a tracking button to come back to current position.
I have the code to create the button :
var button = MKUserTrackingButton.FromMapView(Map);
button.Layer.BackgroundColor = UIColor.White.CGColor;
button.Layer.BorderColor = UIColor.FromRGB(211, 211, 211).CGColor;
button.Layer.BorderWidth = 1;
button.Layer.CornerRadius = 5;
button.TranslatesAutoresizingMaskIntoConstraints = false;
Map.AddSubview(button);
But I need to move it to the bottom right corner ( see image below )
So I just need the line of code to move the button in the MAP View :)
If you want to use Frame to change a Control's position. You should delete button.TranslatesAutoresizingMaskIntoConstraints = false;. This code will disable the Frame, and use autoLayout to place your controls.
Also you can try to use autoLayout:
button.TopAnchor.ConstraintEqualTo(Map.TopAnchor, 100).Active = true;
button.LeadingAnchor.ConstraintEqualTo(Map.LeadingAnchor, 100).Active = true;
button.WidthAnchor.ConstraintEqualTo(52).Active = true;
button.HeightAnchor.ConstraintEqualTo(44).Active = true;
This will also give the tracking button on the bottom right. Note that this only works for iOS 11 and above, so be sure to put a device check in there also.
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
var button = MKUserTrackingButton.FromMapView(map);
button.Layer.BackgroundColor = UIColor.White.CGColor;
button.Layer.BorderColor = UIColor.FromRGB(0, 0, 127).CGColor;
button.Layer.BorderColor = UIColor.White.CGColor;
button.Layer.BorderWidth = 1;
button.Layer.CornerRadius = 5;
button.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddSubview(button);
NSLayoutConstraint.ActivateConstraints(new NSLayoutConstraint[]{
button.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -10),
button.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor, -10)
});
}

Turn off toggle box and graphic when game starts

So this should be a simple answer but I cannot seem to find it anywhere. In Unity, I have a toggle box I want to turn off when the game starts, but unlike most game objects or text, SetActive does not work with toggle boxes. What is the command to turn the graphic off?
public void StartGame () {
mainText.SetActive (false);
startButton.SetActive (false);
StartCoroutine (SpawnBalls ());
//Turn off toggle box graphic
playing = true;
}
public void AddBall () {
if (ballBox.isOn) {
ballNumber = 3;
//Debug.Log ("yes");
} else {
ballNumber = 2;
}
If I understand you correctly you want to be able to remove the display of the toggle on the start of the game? If so set a reference to the toggle you want to click on and off heres a small example:
Toggle T = GameObject.Find("myToggle").GetComponent<Toggle>();
T.gameObject.SetActive(false);
Make a reference to it and you should be able to click it of and on as you need it tested it out on a game I'm working and it worked for me let me.

I'm not sure if my button class file only works in one GameState

Here is a class file I have for button creation:
class Button
{
Texture2D buttonTexture;
Rectangle buttonRectangle;
Color buttonColour = new Color(255, 255, 255, 255);
public Vector2 size, buttonPosition;
public Button(Texture2D newButtonTexture, Vector2 newSize)
{
buttonTexture = newButtonTexture;
size = newSize;
}
public void setPosition(Vector2 newButtonPosition)
{
buttonPosition = newButtonPosition;
}
bool down;
public bool isClicked;
public void Update(MouseState mouse)
{
buttonRectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y, (int)size.X, (int)size.Y);
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRectangle.Intersects(buttonRectangle))
{
if (buttonColour.A == 255)
down = false;
if (buttonColour.A == 0)
down = true;
if (down)
buttonColour.A += 3;
else
buttonColour.A -= 3;
if (mouse.LeftButton == ButtonState.Pressed)
isClicked = true;
}
else if (buttonColour.A < 255)
{
buttonColour.A += 3;
isClicked = false;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(buttonTexture, buttonRectangle, buttonColour);
}
}
Below is how I create the buttons in LoadContent (Ignore the shabby parameters it's just the way I'm currently doing it):
btnResume = new Button(Content.Load<Texture2D>("Buttons/button_Resume"), new Vector2(GraphicsDevice.Viewport.Width / 1.875f, GraphicsDevice.Viewport.Height / 4));
btnResume.setPosition(new Vector2(GraphicsDevice.Viewport.Width / 2 - (0.5f*btnResume.size.X), GraphicsDevice.Viewport.Height / 3));
btnQuit = new Button(Content.Load<Texture2D>("Buttons/button_Exit"), new Vector2(GraphicsDevice.Viewport.Width / 1.875f, GraphicsDevice.Viewport.Height / 4));
btnQuit.setPosition(new Vector2(GraphicsDevice.Viewport.Width / 2 - (0.5f*btnQuit.size.X), GraphicsDevice.Viewport.Height - (GraphicsDevice.Viewport.Height / 3)));
And this is how I draw the two buttons in the draw function of Game1.cs:
case GameState.Menu:
btnResume.Draw(spriteBatch);
btnQuit.Draw(spriteBatch);
break;
This all works fine. For some reason however, when doing the same thing for a "Play" button to put on a new game state called "MainMenu", the button doesn't show up (I have tested putting the button code in the game state "menu" and the button does appear, it just won't show when in any other game state other than "menu").
Does anybody know why it won't work? I've remembered to create the button, set the position, and then draw it within the "case GameState.MainMenu" part of the draw function in Game1.cs, so I honestly have no idea why it isn't working. As a side note, I've tried drawing buttons for another game state called "CharacterSelection" and that doesn't work either, HOWEVER it does work if I use spriteBatch.Draw (so I'm pretty sure it's a problem with the button class' draw function.
I can provide screenshots of code or in-game elements, and I'm sorry if the code is messy, I tend to do that a lot. This is my first XNA game and I've not coded in c# much in the past so a lot of this is new to me.
EDIT:
Here are all my gamestates:
enum GameState
{
Opening,
MainMenu,
CharacterSelection,
Countdown,
Playing,
Menu,
Options,
}
GameState CurrentGameState = GameState.Opening;
Opening - Opening cinematic Main Menu - Title Screen basically Character Selection - select what character to play as. Countdown - 3,2,1 GO before the game starts. Playing - Game is active, players are playing e.t.c Menu - Pause menu (opens when esc is pressed) Options - When "Options" is pressed in the pause or main menu (Hasn't been set up yet).
Thanks for the help, but I've solved it. Silly mistake by me! Forgot "btnPlay.Update(mouse);" in my update function! -__-

C# Picturebox not loading that is added in code

I have a procedure which draws in the player picture box, as below:
public static PictureBox drawPlayer(Engine.Tile location, Image looks)
{
PictureBox player = new PictureBox();
player.Location = location.img.Location;
player.BackColor = Color.Yellow;
player.Size = new Size(50, 50);
player.Name = "Player";
player.Visible = true;
player.BringToFront();
return player;
}
However, it is not working, it is called as such:
t.Controls.Add(drawPlayer(location, image));
Any help with this would be great or an answer as to why this doesn't work, I have temporarily set the colour of the box to yellow just to make it really stand out when it does finally decide to load.
Thanks,
Laurence
You need to use player.BringToFront(); after the control has been added into the form, as it does not exist on the form when you call this in the method that is making the PictureBox.

Window won't draw my Sprite when releasing Mouse Button

I am new to SFML and I am just playing around with an example at the moment. I try to draw a sprite where I last clicked my mouse in a window. But some reason the event fires and no sprite is drawn. I can't really figure out why.
I've been looking around in the Documentation but I don't seem to do anything wrong. Help me pretty please? :3
The event is called OnMouseLeftClick and I am trying to draw the mouseLeftClickSprite object. I have removed the bits of code that doesn't matter like the OnMouseMoved event and the OnClose event. They have nothing to do with this.
using System;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;
namespace SFMLExample
{
class Program
{
static Text xMouseCoord;
static Text yMouseCoord;
static Text statusMsg;
static float mouseX, mouseY;
static Texture mouseLeftClickTexture;
static Sprite mouseLeftClickSprite;
static void OnMouseLeftClick(object sender, EventArgs e)
{
if (Mouse.IsButtonPressed(Mouse.Button.Left))
{
statusMsg.DisplayedString = "Console: OnMouseLeftClick() Fired";
RenderWindow window = (RenderWindow)sender;
mouseLeftClickSprite.Position = new Vector2f(mouseX, mouseY);
window.Draw(mouseLeftClickSprite);
}
}
static void Main(string[] args)
{
mouseX = 0.0f;
mouseY = 0.0f;
// Create the main window
RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML Window");
window.Closed += new EventHandler(OnClose);
window.MouseMoved += new EventHandler<MouseMoveEventArgs>(OnMouseMoved);
window.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(OnMouseLeftClick);
// Load a sprite to display
Texture texture = new Texture("cute_image.jpg");
Sprite sprite = new Sprite(texture);
mouseLeftClickTexture = new Texture("GM106.png");
mouseLeftClickSprite = new Sprite(mouseLeftClickTexture);
// Create a graphical string to display
Font font = new Font("arial.ttf");
Text text = new Text("Hello SFML.Net", font);
xMouseCoord = new Text("Mouse X: ", font);
yMouseCoord = new Text("Mouse Y: ", font);
statusMsg = new Text("Console: ", font);
// Play some Music
//Music music = new Music("nice_music.mp3");
//music.Play();
// Start the game loop
while (window.IsOpen())
{
// Process events
window.DispatchEvents();
// Clear screen
window.Clear();
sprite.Scale = new Vector2f(1.0f, 1.0f);
sprite.Position = new Vector2f(window.Size.X / 2, window.Size.Y / 2);
// Draw the sprite
window.Draw(sprite);
// Draw the strings
xMouseCoord.Position = new Vector2f(text.Position.X, text.Position.Y + 30);
yMouseCoord.Position = new Vector2f(xMouseCoord.Position.X, xMouseCoord.Position.Y + 30);
statusMsg.Position = new Vector2f(yMouseCoord.Position.X, yMouseCoord.Position.Y + 30);
window.Draw(text);
window.Draw(xMouseCoord);
window.Draw(yMouseCoord);
window.Draw(statusMsg);
// Update the window
window.Display();
}
}
}
}
Added this bit below for some clarification
static void OnMouseMoved(object sender, EventArgs e)
{
RenderWindow window = (RenderWindow)sender;
Vector2i vector = window.InternalGetMousePosition();
mouseX = vector.X;
mouseY = vector.Y;
xMouseCoord.DisplayedString = "Mouse X: " + mouseX;
yMouseCoord.DisplayedString = "Mouse Y: " + mouseY;
statusMsg.DisplayedString = "Console: ";
}
You only draw your sprite once. With approximately 60 frames per second, you might see it blink... or not. If you need it to stay permanent, you need to remember the point in your main program and then draw a sprite at that point every time your draw the other things.
You may want to look into game programming patterns. Normally, you have three big blocks that are repeated endlessly: you get user input, you act on user input, you draw the result.
In pseudo code:
while(isRunning)
{
HandleUserInput();
ChangeWorld():
Render();
}
That way, you can check yourself if what you do is correct. For example, in your scenario you would have drawn something while handling user input. Not good.
The events are dispatched and evaluated inside the so-called window.DispatchEvents(). When you click on your window, the event will be fired and your sprite will be drawn.
The thing is that you are calling window.Clear() after it, so you can not see the result. As nvoigt said, you must render everything at each frame, that is to say at each turn of your loop. This is why one uses Clear() method from Window, to clear the result of the previous frame.
What you could do is remember the position of the mouse when the event is fired or directly set the position of the sprite according to it. Then draw your sprite along with your text etc. If you don't want it to be displayed before any click of the mouse, just put its coordinates at a point outside the window so it will not be drawn.

Categories

Resources