Style | ウィンドウに使用するスタイル。省略された場合は、現在の GUISkin にある toggle スタイルを使用します |
id | ウィンドウの ID 番号(ただし値はユニークでなければいけません) |
clientRect | ウィンドウの位置とサイズを示すスクリーン上の Rect |
func | ウィンドウのコンテンツを表示するメソッド |
text | ウィンドウ内で描画するテキスト |
image | ウィンドウ内で描画する画像 |
content | ウィンドウ内で描画する GUIContent |
style | ウィンドウのスタイル |
title | ウィンドウのタイトルバーに表示されるテキスト |
Rect ウィンドウの位置とサイズを示すスクリーン上の Rect
ポップアップウィンドウ
Windows float above normal GUI controls, feature click-to-focus and can optionally be dragged around by the end user. Unlike other controls, you need to pass them a separate function that renders the GUI controls inside the window.
Note: If you are using GUILayout to place your components inside the window, you should use GUILayout.Window. Also, if MonoBehaviour.useGUILayout is set to false then a call to GUI.Window will not have any effect, even though it is not a GUILayout function.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public 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")) { print("Got a click"); } } }
同じ関数を使用して複数のウィンドウを作成することができます。ただし、ウィンドウごとにユニークな ID を持つ ことを確認してください。例:
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Rect windowRect0 = new Rect(20, 20, 120, 50); public Rect windowRect1 = new Rect(20, 100, 120, 50);
void OnGUI() { // Register the window. We create two windows that use the same function // Notice that their IDs differ windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window"); windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "My Window"); }
// Make the contents of the window void DoMyWindow(int windowID) { if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World")) { print("Got a click in window " + windowID); }
// Make the windows be draggable. GUI.DragWindow(new Rect(0, 0, 10000, 10000)); } }
ウィンドウの表示をやめるには、OnGUI メソッド内で GUI.Window の呼び出しを止めるだけです:
// boolean variable to decide whether to show the window or not. // Change this from the in-game GUI, scripting, the inspector or anywhere else to // decide whether the window is visible
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public bool doWindow0 = true;
// Make the contents of the window. void DoWindow0(int windowID) { GUI.Button(new Rect(10, 30, 80, 20), "Click Me!"); }
void OnGUI() { // Make a toggle button for hiding and showing the window doWindow0 = GUI.Toggle(new Rect(10, 10, 100, 20), doWindow0, "Window 0");
// Make sure we only call GUI.Window if doWindow0 is true. if (doWindow0) { GUI.Window(0, new Rect(110, 10, 200, 60), DoWindow0, "Basic Window"); } } }
To make a window that gets its size from automatic GUI layouting, use GUILayout.Window. Call Ordering Windows need to be drawn back-to-front; windows on top of other windows need to be drawn later than the ones below them. This means that you can not count on your DoWindow functions to be called in any particular order. In order for this to work seamlessly, the following values are stored when you create your window (using the Window function), and retrieved when your DoWindow gets called: GUI.skin, GUI.enabled, GUI.color, GUI.backgroundColor, GUI.contentColor, GUI.matrix.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Rect windowRect0 = new Rect(20, 20, 120, 50); public Rect windowRect1 = new Rect(20, 100, 120, 50);
void OnGUI() { // Here we make 2 windows. We set the GUI.color value to something before each. GUI.color = Color.red; windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "Red Window");
GUI.color = Color.green; windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, "Green Window"); }
// Make the contents of the window. // The value of GUI.color is set to what it was when the window // was created in the code above. void DoMyWindow(int windowID) { if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World")) { print("Got a click in window with color " + GUI.color); }
// Make the windows be draggable. GUI.DragWindow(new Rect(0, 0, 10000, 10000)); } }
Note that you can use the alpha component of GUI.color to fade windows in and out.
See Also: DragWindow, BringWindowToFront, BringWindowToBack.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.