ScriptableWizard (脚本向导)
ScriptableWizard (脚本向导)
从此类派生以创建编辑器向导。 继承于 EditorWindow
using UnityEditor;
using UnityEngine;
/// <summary> 创建灯光的向导程序,自定义灯光颜色和范围 </summary>
public 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", "Apply");//有两个按键 名称分别为 Create Apply
//如果不想使用第二个按钮:
//ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light", "Create");
//方法参数: (string title, string createButtonName, string otherButtonName)
}
/// <summary> 向导程序被创建时 </summary>
void OnWizardCreate()
{
GameObject go = new GameObject("New Light");
Light lt = go.AddComponent<Light>();
lt.range = range;
lt.color = color;
}
// 向导内容发生变换时调用
void OnWizardUpdate()
{
helpString = "Please set the color of the light!";
}
// 当按下 otherButtonName 对应的按钮时,该方法被调用
void OnWizardOtherButton()
{
if (Selection.activeTransform != null)
{
Light lt = Selection.activeTransform.GetComponent<Light>();
if (lt != null)
{
lt.color = Color.red;
}
}
}
// 当按下 createButtonName 对应的按钮时,该方法被调用
void OnWizardCreate()
{
// Do Something..
}
}
isValid参数 : 用来控制createButtonName 对应按钮的启用和禁用。
在游戏中的截图工具:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.IO;
public class GenCamPngEditor : ScriptableWizard
{
public int width = 1280, height = 720;
public Camera cam = null;//Camera.main;
static int wid = 0;
static int hei = 0;
[MenuItem("截图工具",false,2001)]
static void Generate()
{
wid = Screen.width;
hei = Screen.height;
ScriptableWizard.DisplayWizard("Generate Camera Png", typeof(GenCamPngEditor), "Generate");
}
void OnWizardUpdate()
{
helpString = "请填写分辨率";
if (width != 0 && height != 0 && cam != null)
{
isValid = true;
}
else
{
isValid = false;
}
//isValid参数 : 用来控制createButtonName 对应按钮的启用和禁用。
}
//按钮事件
void OnWizardCreate()
{
GenPng();
}
void GenPng()
{
//RenderTexture rt = new RenderTexture(width, height, 1, RenderTextureFormat.ARGB32);
RenderTexture rt = new RenderTexture(width, height, 1);
cam.targetTexture = rt;
cam.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D(width, height, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect(0, 0, width, height), 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
cam.targetTexture = null;
RenderTexture.active = null;
GameObject.DestroyImmediate(rt);
byte[] bytes = screenShot.EncodeToPNG();
string path = EditorUtility.OpenFolderPanel("Fold", "", "");
string file_path = path;
path += "/cam.png";
System.IO.File.WriteAllBytes(path, bytes);
Debug.Log(string.Format("Camera : {0} 截取完成: {1}", cam.name, path));
AssetDatabase.Refresh();
}
public void WriteFileByLine(string file_path, string file_name, string str_info)
{
StreamWriter sw;
if (!File.Exists(file_path + "//" + file_name))
{
sw = File.CreateText(file_path + "//" + file_name);//创建一个用于写入 UTF-8 编码的文本
Debug.Log("文件创建成功!");
}
else
{
sw = File.AppendText(file_path + "//" + file_name);//打开现有 UTF-8 编码文本文件以进行读取
}
sw.WriteLine(str_info);//以行为单位写入字符串
sw.Close();
sw.Dispose();//文件流释放
}
}
