強火で進め

このブログではプログラム関連の記事を中心に書いてます。

ウィザード形式の入力フォームの作り方


ScriptableWizardというクラスの派生を作る事で簡単にウィザード形式の入力フォームが作成可能です。

Unity Script Reference – ScriptableWizard
http://unity3d.com/support/documentation/ScriptReference/ScriptableWizard.html

とは言え、複数ページのウィザードは作れない様です。

"入力項目にデータを入力 → 「Create」ボタンで作成"

という仕組みのものが作成可能です。入力項目はInspectorでデータを入力するのと同様の入力が可能です。

後、以下の様な注意点が有ります。

  • プログラムはC#しか対応していない
  • Editorの拡張なのでプログラムのファイルはEditorフォルダの下に配置

今回のサンプルではライトの作成をプログラムと成ります。作成だけでなく、ライトを選択した状態でパラメータを変更後に「適用」ボタンを押すとそのライトの設定を変更する事も出来ます。

プログラムはこの様に書きます。

【WizardCreateLight.cs】

using UnityEditor;
using UnityEngine;

class WizardCreateLight : ScriptableWizard {
	public float range = 500;
	public Color color = Color.red;
	
	[MenuItem ("GameObject/Create Light Wizard")]
	static void CreateWizard () {
		ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light", "Create", "適用");
	}
	
	void OnWizardCreate () {
		GameObject go = new GameObject ("New Light");
		go.AddComponent("Light");
		go.light.range = range;
		go.light.color = color;
	}
	
	void OnWizardUpdate () {
		helpString = "Please set the color of the light!";
	}
	
	void OnWizardOtherButton () {
		if (Selection.activeTransform == null ||
				Selection.activeTransform.light == null) return;
		Selection.activeTransform.light.color = Color.red;
	}
}

【注意】

  • メニューの GameObject → Create Light Wizard に機能が追加されます。クリックするとウィザードが立ち上がります。
  • 今回のクラス名は WizardCreateLight なのでファイル名も同じ名前に変更するのを忘れないで下さい。

主な部分を解説します。

[MenuItem ("GameObject/Create Light Wizard")]

メニューの GameObject → Create Light Wizard に機能を追加。

ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light", "Create", "適用");

引数は(タイトル, Createボタンの名前, もう一つ設定可能なボタン)。

もちろん、「もう一つ設定可能なボタン」については必要無ければ以下の様に省略可能。

ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light", "Create");
		helpString = "Please set the color of the light!";

ヘルプ情報の表示。 helpString = "" で非表示に成ります。

void OnWizardCreate () {

Createボタンが押された時に呼ばれます。指定したデータを元にコンポーネントを作成します。

	void OnWizardOtherButton () {

Createボタン以外に設定可能なボタン(今回の場合は「適用」ボタン)が押された時に呼ばれます。

	void OnWizardUpdate () {

ウィンドウが開いた時と入力データに変更が有った時に呼ばれます。


今回は使用してないですがエラーを表示したい時は errorString プロパティ、

Unity Script Reference – ScriptableWizard.errorString
http://unity3d.com/support/documentation/ScriptReference/ScriptableWizard-errorString.html

関連情報

Unity Script Reference – ScriptableWizard.helpString
http://unity3d.com/support/documentation/ScriptReference/ScriptableWizard-helpString.html