How do I use the GUI.Window in C#? Every example I can find is done in javascript. I’m having problems getting the WindowFunction parameter to work.
Should I be creating a Delegate for the WindowFunction?
thanks.
How do I use the GUI.Window in C#? Every example I can find is done in javascript. I’m having problems getting the WindowFunction parameter to work.
Should I be creating a Delegate for the WindowFunction?
thanks.
I’m doing something like this (but the Window doesn’t appear)…
private GUI.WindowFunction winFunc;
private Rect winRect;
void Start()
{
winRect = new Rect(10,10,100,100);
winFunc = new GUI.WindowFunction(this.ShowWindow,0);
}
void OnGUI()
{
winRect = GUI.Window(0,winRect,winFunc,"Window Name");
}
void ShowWindow(int windowId)
{
Debug.Log("testing. This never displays!");
}
Does GUI.WindowFunction even exist? I can’t see it in the docs.
Anyway, here’s how I’d translate the Javascript sample code for GUI.Window:
using UnityEngine;
using System.Collections;
public class GUIWindowTest : MonoBehaviour
{
Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI()
{
// Register the window. Notice the 3rd parameter
windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
}
// Make the contents of the window
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(10,20,100,20), "Hello World"))
Debug.Log("Got a click");
}
}
Neil,
Your example is the first way I tried it, but it didn’t work. So then I decided that the 3rd parameter must be a c# function pointer (aka Delegate). Yes the GUI.WindowFunction does exist.
However, so far, neither way works. The Debug text isn’t even displaying, so the “DoMyWindow” function isn’t being called.
:?: :?
Funny, that code works for me.
EDIT: I just tried it like this, and it also works:
using UnityEngine;
using System.Collections;
public class GUIWindowTest : MonoBehaviour
{
Rect windowRect = new Rect(20, 20, 120, 50);
GUI.WindowFunction windowFunction;
void Start()
{
windowFunction = DoMyWindow;
}
void OnGUI()
{
windowRect = GUI.Window(0, windowRect, windowFunction, "My Window");
}
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(10,20,100,20), "Hello World"))
Debug.Log("Got a click");
GUI.DragWindow();
}
}
I imagine GUI.WindowFunction is just delegate void Function(int), which maybe explains why it isn’t documented in the list of classes.
I think you must have some other problem that’s preventing this from working.
Your c# example, is working?
EDIT: Nevermind. I found the problem.
I just discovered that if I remove “useGUILayOut = false”, the Window is displayed! (I didn’t show this in my code example, but it was in my real code.)
void Awake()
{
useGUILayout = false; // This keeps the GUI Window from displaying!!!
}
FYI… I’m doing iPhone development, the useGUILayout=false line is recommended for performance reasons.
actually the safer line of code would be “window.Show”, the reason I suggest this is because if unity makes any updates Show may have more functionality than a simple variable…
for those of you just tuning in here is a much cleaner example of code in C#
using UnityEngine;
using UnityEditor;
public class Window : EditorWindow {
string myString = "Hello World";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
Rect windowRect = new Rect(0, 0, 100, 100);
int index = 0;
// Add menu named "My Window" to the Window menu
[MenuItem ("Window/My Window")]
static void Init () {
// Get existing open window or if none, make a new one:
Window window = (Window)EditorWindow.GetWindowWithRect(typeof(Window), new Rect(0,0,200,200), true, "Window");
window.Show ();
}
void OnGUI () {
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
myString = EditorGUILayout.TextField ("Text Field", myString);
groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle ("Toggle", myBool);
myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
EditorGUILayout.EndToggleGroup ();
}
}
that does not exist on normal gui, only on editor objects
Hey thanks man for the code.
I have been trying to do this for hours.