unity编辑器扩展_07(创建对话框,检测按钮的点击,点击按钮后提示信息,保存设置的数据,显示点击按钮后的处理的进度条信息)
代码:
using UnityEditor;
using UnityEngine;
public class ChangeValue : ScriptableWizard {
public int health = 10;
public int speed = 23;
[MenuItem("Tools/CreateWizard")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<ChangeValue>("这是自己创建的标题", "按钮1的名字","按钮2的名字");
}
void OnWizardCreate()
{
ShowNotification(new GUIContent(Selection.gameObjects.Length + "多个游戏物体被改变"));//显示提示信息
GameObject[] gameObject = Selection.gameObjects;
EditorUtility.DisplayProgressBar("这是进度条的标题", "进度信息", 0);
int count = 0;
foreach (GameObject go in gameObject)
{
count++;
CompleteProject.PlayerHealth ph = go.GetComponent<CompleteProject.PlayerHealth>();
Undo.RecordObject(ph,"ph");//对点击事件的撤销功能
ph.startingHealth += health;
ph.flashSpeed += speed;
EditorUtility.DisplayProgressBar("这是进度条标题", "进度信息", count / Selection.gameObjects.Length);
}
EditorUtility.ClearProgressBar();
}
void OnWizardOtherButton()
{
OnWizardCreate();
}
const string healtKey = "health";
const string speedKey = "speed";
void OnEnable()
{
health = EditorPrefs.GetInt(healtKey, health);
speed = EditorPrefs.GetInt(speedKey, speed);
}
void OnWizardUpdate()
{
string helpString = null;
string errorString = null;
if (Selection.gameObjects.Length > 0)
{
helpString = "您当前选择了" + Selection.gameObjects.Length + "个游戏物体";
}
else
{
errorString = "请选择至少一个游戏物体";
}
EditorPrefs.SetInt(healtKey, health);
EditorPrefs.SetInt(speedKey, speed);
}
void OnSelectionChange()
{
OnWizardUpdate();
}
}
说明:使用ScriptableWizard.DisplayWizard创建对话框<类名>(“对话框名标题”,“按钮1”,“按钮2”),当参数不设置按钮1是,则默认按钮名字为Create。按钮2根据实际情况决定是否需要。
使用Undo.RecordObject对点击事件的撤销功能,第一个参数为需要保存的值,第二为该值保存后的名字。
使用ShowNotification显示提示信息
使用EditorPrefs保存数据
使用EditorUtility.DisplayProgressBar显示进度条