Editor SaveDialog等代码
首先我们查一下unity官方手册可以看到下面的画面(我是使用谷歌浏览器的翻译)。
我们点进打开文件OpenFilePanel里去看官方示例,代码如下:
using System.IO;
using UnityEngine;
using UnityEditor;
public class OpenFilePanelExample : EditorWindow
{
[MenuItem("Example/Overwrite Texture")]
static void Apply()
{
Texture2D texture = Selection.activeObject as Texture2D;
if (texture == null)
{
EditorUtility.DisplayDialog("Select Texture", "You must select a texture first!", "OK");
return;
}
string path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
if (path.Length != 0)
{
var fileContent = File.ReadAllBytes(path);
texture.LoadImage(fileContent);
}
}
}
再看一下打开文件夹OpenFolderPanel官方示例,代码如下:
-
using UnityEditor;
-
using System.IO;
-
public class OpenFolderPanelExample : EditorWindow
-
{
-
[MenuItem("Example/Load Textures To Folder")]
-
static void Apply()
-
{
-
string path = EditorUtility.OpenFolderPanel("Load png Textures", "", "");
-
string[] files = Directory.GetFiles(path);
-
foreach (string file in files)
-
if (file.EndsWith(".png"))
-
File.Copy(file, EditorApplication.currentScene);
-
}
-
}
是不是觉得大同小异呢,SaveFilePanel以及SaveFolderPanel也是同样的方式我这里就不演示。
我的应用代码如下:
-
void ConfirmBtnClick()
-
{
-
//SceneManager.LoadScene(1);
-
string AssetsPath = Application.dataPath + "/Resources/";
-
string Path = EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");
-
if (Path.Length != 0)
-
{
-
Debug.Log("开始保存数据");
-
///TODO 保存数据
-
SceneManager.LoadScene(1);
-
}
-
else
-
{
-
Debug.Log("取消保存");
-
}
-
}
因为接下来需要保存的数据还没有完成,所以这里只是debug一下。
上面的方法虽然在Unity编辑器里是可以打开文件夹的,但是我们将软件打包是会发现报错无法打包,这是因为该脚本引用using UnityEditor;只能在编辑器下使用,所以我们还是不要想着偷懒老老实实使用类吧,首先我们创建两个类。
接下来直接调用就可以了
-
/// <summary>
-
/// 打开windows保存窗口
-
/// </summary>
-
/// <returns>路径名(带名字)</returns>
-
string loadFile()
-
{
-
OpenFileName openFileName = new OpenFileName();
-
openFileName.structSize = Marshal.SizeOf(openFileName);
-
openFileName.filter = "All files (*.*)|*.*";
-
openFileName.file = new string(new char[256]);
-
openFileName.maxFile = openFileName.file.Length;
-
openFileName.fileTitle = new string(new char[64]);
-
openFileName.maxFileTitle = openFileName.fileTitle.Length;
-
openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
-
openFileName.title = "保存项目";
-
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
-
if (LocalDialog.GetSaveFileName(openFileName))
-
{
-
return openFileName.file;
-
}
-
else
-
{
-
return null;
-
}
-
}
因为我需要获取返回的路径名使用,所以我将它写成带返回值的函数。我们实际调用一下试试
-
/// <summary>
-
/// 确定保存
-
/// </summary>
-
void ConfirmBtnClick()
-
{
-
//SceneManager.LoadScene(1);
-
//string AssetsPath = Application.dataPath + "/Resources/";
-
//string Path = UnityEditor.EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");
-
string Path=loadFile();
-
if (Path != null)
-
{
-
if (Path.Length != 0)
-
{
-
Debug.Log("开始保存数据");
-
}
-
}
-
else
-
{
-
Debug.Log("取消保存");
-
}
-
}
具体代码就这样
导出 Mesh的代码
我所需要的打开目录功能,主要是为了从导出mesh到该目录
public Transform object;
private string name;
void SaveAsset()
{
Mesh mesh = object.GetComponent<MeshFilter>().mesh;
AssetDatabase.CreateAsset(mesh , "Assets/" + name + ".asset");
}