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
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;
}
}
I am trying to draw some geometric shapes using NGraphics in a picture box on a WinForm:
public partial class DrawingForm : Form
{
public DrawingForm()
{
InitializeComponent();
}
private void DrawingForm_Load(object sender, EventArgs e)
{
var canvas = Platforms.Current.CreateImageCanvas(new NGraphics.Size(100), scale: 2);
var skyBrush = new LinearGradientBrush(NGraphics.Point.Zero, NGraphics.Point.OneY, Colors.Blue, Colors.White);
canvas.FillRectangle(new Rect(canvas.Size), skyBrush);
canvas.FillEllipse(10, 10, 30, 30, Colors.Yellow);
canvas.FillRectangle(50, 60, 60, 40, Colors.LightGray);
canvas.FillPath(new PathOp[] { new MoveTo (40, 60),
new LineTo (120, 60),
new LineTo (80, 30),
new ClosePath ()
}, Colors.Gray);
this.pictureBox1.Image = (Image)canvas.GetImage();
}
}
This source code is giving the following exception:
Additional information: Unable to cast object of type 'NGraphics.ImageImage' to type 'System.Drawing.Image'.
This is one problem. Another question is: is it possible to draw shapes directly on the Forms?
Take a look at the source code for the NGraphics package, it looks like you should construct the GraphicsCanvas based on a standard Graphics context that you could obtain from the form.
https://github.com/praeclarum/NGraphics/blob/master/Platforms/NGraphics.Net/SystemDrawingPlatform.cs#L152
Something like this perhaps
var canvas = new GraphicsCanvas(this.CreateGraphics());
The other thing you need to do is use the Paint event
private void Form1_Paint(object sender, PaintEventArgs e)
{
var graphics = this.CreateGraphics();
var canvas = new GraphicsCanvas(graphics);
var skyBrush = new LinearGradientBrush(NGraphics.Point.Zero,
NGraphics.Point.OneY, Colors.Blue, Colors.White);
canvas.FillRectangle(new Rect(0, 0, 100, 100), skyBrush);
canvas.FillEllipse(10, 10, 30, 30, Colors.Yellow);
canvas.FillRectangle(50, 60, 60, 40, Colors.LightGray);
canvas.FillPath(new PathOp[] { new MoveTo (40, 60),
new LineTo (120, 60),
new LineTo (80, 30),
new ClosePath ()
}, Colors.Gray);
}
I have a 3 toggle buttons in my playstate as follows:
public void ShowIt()
{
HydroElectric ec = new HydroElectric();
ec.t1Bool = GUI.Toggle (new Rect (25, 55, 100, 50), ec.t1Bool, "Turbina 2 MW");
ec.t2Bool = GUI.Toggle (new Rect (25, 95, 100, 50), ec.t2Bool, "Turbina 3 MW");
ec.t3Bool = GUI.Toggle (new Rect (25, 135, 100, 50), ec.t3Bool, "Turbina 1 MW");
GUI.Box (new Rect (Screen.width - 100, 60, 80, 25), ec.prod.ToString ()); // PRODUCED ENERGY
}
This buttons should change the value of t1Bool, t2Bool and t3Bool and those changes should be reflected in this script:
using System;
using UnityEngine;
namespace Assets.Code.PowerPlants
{
public class HydroElectric
{
public bool t1Bool;
public bool t2Bool;
public bool t3Bool;
public static int turbina1;
int turbina2;
int turbina3;
public float prod;
public HydroElectric ()
{
t1Bool = true;
t2Bool = true;
t3Bool = false;
prod = 0f;
}
public float HydroControlPanel ()
{
turbina1 = t1Bool ? 2 : 0;
turbina2 = t2Bool ? 3 : 0;
turbina3 = t3Bool ? 1 : 0;
prod = turbina1 + turbina2 + turbina3;
return prod;
}
}
}
I have no errors in the console but the toggle button simply does not work in play mode, like if the values are locked, I cannot check or uncheck the buttons, they just stay the way they start.
Do you have any idea why?
You are re-creating HydroElectric every time GUI-related functions are called, obviously changes are not reflected and you get the behavior you said.
Move this line of code out of that function to somewhere else such as in the constructor and it will behave properly.
OnGui won't show up when I start the game for some reason. I've attached the script to a gameobject:
public class username : MonoBehaviour {
private static string user = "";
void OnGui()
{
GUI.Label(new Rect((Screen.width / 2), (Screen.height / 2), 300, 300), "Username");
user = GUI.TextField(new Rect(-4, 0, 100, 100), user);
if (GUI.Button(new Rect(-4, 0, 200, 30), "Continue"))
{
WWWForm form = new WWWForm();
form.AddField("", user);
WWW w = new WWW("http://site/register.php", form);
StartCoroutine(register(w));
}
}
Thanks
OnGui() is case-sensitive and needs to be OnGUI().
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html
I am rendering some text in GUI and trying to recoganize the URLs. Also I need to open the URLs in browser when click events are performed. I have attached the model code snippent below. My question is how to make these texts a valid URLs and how to make them to respond to click events. And one more thing this due to some requirements I cant change the structure of the below code snnipet.
namespace Model
{
public partial class ParentView : Form
{
public ParentView()
{
InitializeComponent();
}
private void ParentView_Paint(object sender, PaintEventArgs e)
{
GraphicsState state = e.Graphics.Save();
GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
renderer.RenderAsImage();
e.Graphics.Restore(state);
e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);
//I believe need to do something here
}
}
public class GraphicsRenderer
{
Graphics PageGraphics;
public GraphicsRenderer(Graphics g)
{
PageGraphics = g;
}
public void RenderAsImage()
{
Point currentLocation = new Point(0, 0);
PageGraphics.TranslateTransform(20, 40);
PageGraphics.DrawString("www.google.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
PageGraphics.TranslateTransform(20, -40);
PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
currentLocation = new Point(50, 60);
PageGraphics.DrawString("www.stackoverflow.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
}
}
}
Thanks,
Mkn
If you're just writing text to the screen as graphics you can't make it clickable. You'll need to position a LinkLabel or other clickable control at the right position to trigger the url opening in a browser.
Thanks for your response. I have analyzed further and found a way. Drawing rectangles over the text and finding the rectangles in mouse click events using Rectangle. Contains(MouseLeftClikLocation).
Now I am having another problem. If scale transformation is performed I could not able to get the location after restoring the graphics.
Please find the code snnipet below.
private void ParentView_Paint(object sender, PaintEventArgs e)
{
GraphicsState state = e.Graphics.Save();
GraphicsRenderer renderer = new GraphicsRenderer(e.Graphics);
renderer.RenderAsImage();
e.Graphics.Restore(state);
e.Graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, 300, 300);
//I am changing here
float[] transformPoints = renderer.transformPoints.Elements;
e.Graphics.TranslateTransform(transformPoints[4], transformPoints[5]);
Point currentLocation = renderer.currentLocation;
e.Graphics.DrawRectangle(new Pen(Brushes.Black),new Rectangle(currentLocation.X,currentLocation.Y,190,30));
}
public class GraphicsRenderer
{
Graphics PageGraphics;
internal Matrix transformPoints = new Matrix();
internal Point currentLocation = new Point(0, 0);
public GraphicsRenderer(Graphics g)
{
PageGraphics = g;
}
public void RenderAsImage()
{
currentLocation = new Point(50, 60);
//PageGraphics.ScaleTransform(3, 3);
PageGraphics.DrawString("www.bing.com", new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, currentLocation);
transformPoints = PageGraphics.Transform;
}
}