この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| wiki:unity:tips:045 [2014/11/13 07:25] 127.0.0.1 外部編集 | — (現在) | ||
|---|---|---|---|
| ライン 1: | ライン 1: | ||
| - | ====== 045 GUIコントロールについて ====== | ||
| - | |||
| - | ===== 概要 ===== | ||
| - | UnityでGUIのコントロールは各スクリプトに記述出来るOnGUIコールバック関数で実装します。 | ||
| - | |||
| - | <code csharp> | ||
| - | using UnityEngine; | ||
| - | using System.Collections; | ||
| - | |||
| - | public class HogeScript : MonoBehaviour | ||
| - | { | ||
| - | void OnGUI() | ||
| - | { | ||
| - | GUI.Box( new Rect(10,10,100,90), "GUI Menu" ); | ||
| - | |||
| - | if(GUI.Button(new Rect(20,40,80,20), "Button1")) | ||
| - | { | ||
| - | // ボタンを押した時の処理を記述します。 | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </code> | ||
| - | {{:wiki:unity:tips:ongui_button.png?200|}} | ||
| - | GUI.Box でButton背景の四角形を配置し、その後、GUI.Button でボタンを配置しています。 | ||
| - | OnGUI 関数は毎フレームよばれる点に注意して下さい。 | ||
| - | |||
| - | ==== ボタンに画像を配置する ==== | ||
| - | GUI.Button の第2引数は文字列の代わりにテクスチャを指定することもできます。 | ||
| - | <code csharp> | ||
| - | using UnityEngine; | ||
| - | using System.Collections; | ||
| - | |||
| - | public class HogeScript : MonoBehaviour | ||
| - | { | ||
| - | public Texture2D icon; | ||
| - | |||
| - | void OnGUI() | ||
| - | { | ||
| - | GUI.Box( new Rect(10,10,100,90), "GUI Menu" ); | ||
| - | |||
| - | if(GUI.Button(new Rect(20,40,80,20), icon)) | ||
| - | { | ||
| - | // ボタンを押した時の処理を記述します。 | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | {{:wiki:unity:tips:unity_ongui_icon.png?200|}} | ||
| - | |||
| - | |||
| - | ==== ボタンにテキストと画像を配置する ==== | ||
| - | GUI.Button の第3引数にGUIContent を指定すればボタンとテキストの両方を表示できます。 | ||
| - | <code csharp> | ||
| - | GUI.Button(new Rect(20,40,80,20),  new GUIContent( "テキスト", icon ) ); | ||
| - | </code> | ||
| - | {{:wiki:unity:tips:unity_ongui_icon_with_text.png?200|}} | ||
| - | |||
| - | [[http://docs-jp.unity3d.com/Documentation/Components/gui-Controls.html|Unity - Unity Manual]] | ||
| - | |||
