I have a controllable character that when you fall off map, a GUI will be created with a respawn button
void OnGUI() {
if(died==true){
GUI.Label(new Rect(daX, daY, 200, 40), "You died!!");
GUI.Label(new Rect(daX, daY-40, 200, 40), whyDie);
if (GUI.Button(new Rect(daX-75, daY+50, 150, 30), "Respawn!!!")){
transform.position = new Vector3(-5,20,5);
}
if (GUI.Button(new Rect(daX-75, daY+100, 150, 30), "Screw This!!!")){
Debug.Log("Back to menu");
}
}
}
The respawn and stuff works, is just that I don't know how to get rid of the GUI, so the respawn button stays there after you respawned. Does anyone know how to remove GUI objects? Thanks in advance
*EDIT: died, daX, daY, whyDie are variables I created and are valid
Just set your died variable back to false, and the GUI calls wont be made.
void OnGUI()
{
if(died)
{
GUI.Label(new Rect(daX, daY, 200, 40), "You died!!");
GUI.Label(new Rect(daX, daY-40, 200, 40), whyDie);
if (GUI.Button(new Rect(daX-75, daY+50, 150, 30), "Respawn!!!"))
{
transform.position = new Vector3(-5,20,5);
died = false; //Stop drawing died interface
}
if (GUI.Button(new Rect(daX-75, daY+100, 150, 30), "Screw This!!!"))
{
Debug.Log("Back to menu");
Application.LoadLevel(0); //Load your first scene, where the menu is.
}
}
}
Alternatively, you could disable the component/gameobject. That way your whole OnGUI method wont be called anymore.
Related
I'm trying to archive two things :
To be able to change for each individual button he own color in the inspector in runtime.
To make a color switch for example when clicking the "LOOP" button change the button color from his current color to another color like a switch between current color and blue. so i know that is loop is true or if loop pressed once make it blue if pressed again switch back to the original color/another color.
This is how the buttons looks like before using the GUI.backgroundColor in the original :
Before using the GUI.backgroundColor in the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GUIExamples : MonoBehaviour
{
public Texture btnTexture;
public Color color;
private void OnGUI()
{
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
Debug.Log("Clicked the button with an image");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 130, 170, 30), "STOP"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE"))
Debug.Log("Clicked the button with text");
}
}
Update :
This is what i tried but when i'm changing the color in the inspector in runtime it's not changing the box color :
It will change the color only if i set the color at this line like this :
currentStyle.normal.background = MakeTex( 2, 2, new Color( 0f, 1f, 0f, 0.5f ) );
But i want to change the color in runtime in real time in the inspector using the color variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GUIExamples : MonoBehaviour
{
public Texture btnTexture;
public Color color;
private GUIStyle currentStyle = null;
private Color oldColor;
private void Start()
{
oldColor = color;
}
private void OnGUI()
{
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
Debug.Log("Clicked the button with an image");
if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
Debug.Log("Clicked the button with text");
if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
Debug.Log("Clicked the button with text");
if (GUI.Button(new Rect(10, 130, 170, 30), "STOP"))
Debug.Log("Clicked the button with text");
InitStyles();
GUI.Box(new Rect(10, 170, 170, 30), "PAUSE", currentStyle);
if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE"))
Debug.Log("Clicked the button with text");
}
private void InitStyles()
{
if(oldColor != color)
{
currentStyle = null;
}
if (currentStyle == null)
{
currentStyle = new GUIStyle(GUI.skin.box);
currentStyle.normal.background = MakeTex(2, 2, new Color(color.r, color.g, color.b, color.a));
oldColor = color;
}
}
private Texture2D MakeTex(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; ++i)
{
pix[i] = col;
}
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
}
Basically, this code detects at which side of the screen the mouse is, and rotates the camera to look at it when the right mouse button is pressed, basically it aims at the direction the player is looking.
Issue I ran into is that it also rotates back based on the position of the mouse, if the player is looking to the right and press the aim button, the camera would rotate 20 degrees, and than rotate back -20 degrees when the button goes up, and it works fine until you aim to the right, and after pressing the aim button, look left, this will rotate 20 degrees, than the camera will rotate another 20 degrees, making for a total of 40 instead of the 0 it should be
So how I decided to overcome this was by setting a bool that basically tells at which side you are aiming, and will move based on that value, issue is I have no idea how to set the bool from inside the if statement so other if statements can read it.
I tried doing it the way you see below but basically it just doesn't work. I also tried to put the second if statement inside the first one, but it completely ignores it
This is what I am doing
bool isCameraRight = true;
if (Input.GetMouseButtonDown(1) && mouse.x < Screen.width / 2)
{
//Code
isCameraRight = false;
}
if (!isCameraRight && Input.GetMouseButtonUp(1))
{
//Code
}
This is something I also tried and didn't work, it just ignores the second if statement always
if (Input.GetMouseButtonDown(1) && mouse.x < Screen.width / 2)
{
//Code
if (!isCameraRight && Input.GetMouseButtonUp(1))
{
//Code
}
}
And this is how the final code in my Unity script looks like
private void Update()
{
var mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
bool isCameraRight = true;
if (Input.GetMouseButtonDown(1) && mouse.x > Screen.width / 2)
{
this.transform.Rotate(0, Mathf.Lerp(0, 25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(40, 35, 37);
isCameraRight = true;
}
if (isCameraRight && Input.GetMouseButtonUp(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, -25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(35, 40, 37);
}
if (Input.GetMouseButtonDown(1) && mouse.x < Screen.width / 2)
{
this.transform.Rotate(0, Mathf.Lerp(0, -25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(40, 35, 37); ;
isCameraRight = false;
}
if (!isCameraRight && Input.GetMouseButtonUp(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, 25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(35, 40, 37);
}
}
What is the best way to go about doing this?
Please don't delete this, I am stupid, not a coder, just trying my best and really need this to work
I am not into Unity, but I think you first need to simply set isCameraRight from the start and then just use that variable, no need to check the same thing many times.
private void Update()
{
var mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
bool isCameraRight = mouse.x > Screen.width / 2;
if (isCameraRight)
{
if (Input.GetMouseButtonDown(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, 25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(40, 35, 37);
}
else if (Input.GetMouseButtonUp(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, -25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(35, 40, 37);
}
}
else
{
if (Input.GetMouseButtonDown(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, -25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(40, 35, 37); ;
}
else if (Input.GetMouseButtonUp(1))
{
this.transform.Rotate(0, Mathf.Lerp(0, 25, 15), 0);
cinemachineVirtualCamera.m_Lens.FieldOfView = Mathf.Lerp(35, 40, 37);
}
}
}
If you need to store the value of isCameraRight from a previous click, you need to use an instance variable.
I try following code but box disappear
public class TestUIBorder : MonoBehaviour
{
public GUISkin skin;
void OnGUI()
{
skin.box.border = new RectOffset(10, 10, 10, 10);
GUI.Box(new Rect(0, 0, 200, 200), "This is a box", skin.box);
}
}
like following screenshot, without text and without outline
using UnityEngine;
using System.Collections;
public class Network1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
Debug.Log ("OnGUI()");
if (NetworkPeerType == NetworkPeerType.Disconnected){
if (GUI.Button (new Rect(10, 30, 120, 20), "Join a game")){
Network.Connect("127.0.0.1", 25001);
}
if (GUI.Button (new Rect(10, 50, 120, 20), "Host a game")){
Network.InitializeServer(32, 25001, false);
}
else if (Network.peerType == NetworkPeerType.Client){
GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as a Client");
if (GUI.Button (new Rect(10, 30, 120, 20), "Leave lobby")){
Network.Disconnect(200);
}
}
}
}
}
That's my code. It throws this error:
Assets/Network1.cs(15,21): error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected
I've googled it for a while now and can't seem to get a relevant answer.
Any help is appreciated.
if (NetworkPeerType == NetworkPeerType.Disconnected){
this should probably be:
if (Network.peerType == NetworkPeerType.Disconnected){
I am trying to set up a health bar for a 2D game using OnGUI. I have an outside texture and an inside texture for the meter. The set up I have now works partially. I am able to change the scale of the inside meter so it looks like it is depleting, however the position along the x is changing as well. This causes an awkward effect as well as the meter moving outside its container.
void Update ()
{
score = EnemyScript.killCount * 100;
powerScale -= 1;
}
void OnGUI()
{
//Score and Power Containter
GUILayout.BeginArea(new Rect(Screen.width/8, Screen.height/8, 500, 200));
GUILayout.Label("Score: " + score, style);
GUI.DrawTexture(new Rect(-10,20,200,80),powerBoundry);
GUILayout.EndArea();
//Power Meter
GUILayout.BeginArea(new Rect(Screen.width/8, Screen.height/8, 450, 200));
GUI.DrawTexture(new Rect(-10,20,powerScale,80),powerFill, ScaleMode.ScaleAndCrop);
GUILayout.EndArea();
}
Is it possible to anchor it in place? Or perhaps make it a child of a game object to anchor it?
try using DrawTextureWithTexCoords.
void OnGUI()
{
GUILayout.BeginArea(new Rect(Screen.width / 8, Screen.height / 8, 500, 200));
GUI.DrawTexture(new Rect(-10, 20, 200, 80), powerBoundry);
GUI.DrawTextureWithTexCoords(new Rect(-10, 20, powerScale, 80), powerFill, new Rect(0f, 0f, (float)powerScale / 200f, 1f));
GUILayout.EndArea();
}