zoukankan      html  css  js  c++  java
  • Unity使用Windows弹窗保存图片

    此功能都在类EditorUtility中(using UnityEditor;)

    包括

    OpenFilePanel
    打开文件窗口
    Displays the "open file" dialog and returns the selected path name.
    展示“打开文件”对话框并返回所选择的路径名称

    OpenFilePanelWithFilters
    打开文件窗口(带过滤类型)
    Displays the "open file" dialog and returns the selected path name.
    展示“打开文件”对话框并返回所选择的路径名称

    OpenFolderPanel
    打开文件夹窗口
    Displays the "open folder" dialog and returns the selected path name.
    展示“打开文件夹”对话框并返回所选择的路径名称

    SaveFilePanel
    保存文件窗口
    Displays the "save file" dialog and returns the selected path name.
    展示“保存文件”对话框并返回所选择的路径名称

    SaveFilePanelInProject
    保存文件窗口(项目中文件)
    Displays the "save file" dialog in the Assets folder of the project and returns the selected path name.
    展示项目的Assets文件夹中的“保存文件”对话框并返回所选择的路径名称

    SaveFolderPanel
    保存文件夹窗口
    Displays the "save folder" dialog and returns the selected path name.
    展示“保存文件夹”对话框并返回所选择的路径名称

    下面的代码,将屏幕截图并保存到本地

    using System.Collections;
    using System.IO;
    using UnityEditor;
    using UnityEngine;
    
    public class SaveScreen : MonoBehaviour
    {
        private string time;
    
        // Use this for initialization
        private void Start()
        {
            StartCoroutine(Save());
        }
    
        // Update is called once per frame
        private void Update()
        {
        }
    
        private IEnumerator Save()
        {
            yield return new WaitForEndOfFrame();
    
            int width = Screen.width;
            int height = Screen.height;
            Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
    
            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            tex.Apply();
    
            byte[] bytes = tex.EncodeToPNG();
            Destroy(tex);
    
            var path = EditorUtility.SaveFolderPanel("请选择本地文件夹保存", "", "");
    
            if (path.Length != 0)
            {
                if (bytes != null)
                {
                    File.WriteAllBytes(path + "/" + time + ".png", bytes);
                }
            }
        }
    }

    最后,附上效果图

    image

    关于Unity进行更多的windows交互,请参阅官方网站

    https://docs.unity3d.com/ScriptReference/EditorUtility.html

    声明:此博客为个人学习之用,如与其他作品雷同,纯属巧合,并请明示指出
  • 相关阅读:
    CSS系列:长度单位&字体大小的关系em rem px
    CSS兼容性
    html5+css3
    将url的查询参数解析成字典对象
    SQL阻止保存要求重新创建表的更改 在哪里设置
    Jquery&JS简单选项卡
    块级&行内(内联)元素
    时间
    PHP 二维数组根据某个字段排序
    php 操作数组 (合并,拆分,追加,查找,删除等)
  • 原文地址:https://www.cnblogs.com/fws94/p/6236925.html
Copyright © 2011-2022 走看看