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();//文件流释放  

    } 
}

全部评论

相关推荐

书海为家:实习是成为大厂正式员工很好的敲门砖,看您的简历中有一段实习经历,挺好的。我来给一点点小建议,因为毕竟还在学校不像工作几年的老鸟有丰富的项目经验,面试官在面试在校生的时候更关注咱们同学的做事逻辑和思路,所以最好在简历中描述下自己实习时做过项目的完整过程,比如需求怎么来的,你对需求的解读,你想到的解决办法,遇到困难如何找人求助,最终项目做成了什么程度,你从中收获了哪些技能,你有什么感悟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务