创建对话框,统一修改多个选中物体的属性
代码:
using UnityEditor;
using UnityEngine;
public class WizardEditor : ScriptableWizard
{
public int healthValue = 10;
public int speedValue = 10;
[MenuItem("Tools/CrerteWizard")]
static void CrerteWizard()
{
ScriptableWizard.DisplayWizard<WizardEditor>("title", "button name");
}
//监听按钮
void OnWizardCreate()
{
Debug.Log("OnWizardCreate");
GameObject[] objects = Selection.gameObjects;
foreach (GameObject obj in objects)
{
//记录操作,用于ctrl+z可撤销
Undo.RecordObject(obj, "change name");
obj.name = obj.name + "new name";
}
//xxxx xx =
}
}
使用helpString和errorString
//当对话框被创建,字段(如healthValue)值被修改时,会调用该方法
void OnWizardUpdate()
{
helpString = errorString = "";
if (Selection.gameObjects.Length > 0)
{
helpString = "选择了" + Selection.gameObjects.Length + "个对象";
}
else
{
errorString = "没有任何选择";
}
}
//在Hierarchy视图中选择的游戏对象改变时调用
void OnSelectionChange()
{
OnWizardUpdate();
}
完整代码:
using UnityEditor;
using UnityEngine;
public class WizardEditor : ScriptableWizard
{
public int healthValue = 10;
public int speedValue = 10;
[MenuItem("Tools/CrerteWizard")]
static void CrerteWizard()
{
ScriptableWizard.DisplayWizard<WizardEditor>("title", "button name");
}
//监听按钮
void OnWizardCreate()
{
Debug.Log("OnWizardCreate");
GameObject[] objects = Selection.gameObjects;
foreach (GameObject obj in objects)
{
//记录操作,用于ctrl+z可撤销
Undo.RecordObject(obj, "change name");
obj.name = obj.name + "new name";
}
//xxxx xx =
}
//当对话框被创建,字段(如healthValue)值被修改时,会调用该方法
void OnWizardUpdate()
{
helpString = errorString = "";
if (Selection.gameObjects.Length > 0)
{
helpString = "选择了" + Selection.gameObjects.Length + "个对象";
}
else
{
errorString = "没有任何选择";
}
}
//在Hierarchy视图中选择某游戏对象时调用
void OnSelectionChange()
{
OnWizardUpdate();
}
}