UnityEditor编辑器扩展开发_SaveDialog保存到目录代码实现

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官方示例,代码如下:

 
  1. using UnityEditor;

  2. using System.IO;

  3.  
  4. public class OpenFolderPanelExample : EditorWindow

  5. {

  6. [MenuItem("Example/Load Textures To Folder")]

  7. static void Apply()

  8. {

  9. string path = EditorUtility.OpenFolderPanel("Load png Textures", "", "");

  10. string[] files = Directory.GetFiles(path);

  11.  
  12. foreach (string file in files)

  13. if (file.EndsWith(".png"))

  14. File.Copy(file, EditorApplication.currentScene);

  15. }

  16. }

 

是不是觉得大同小异呢,SaveFilePanel以及SaveFolderPanel也是同样的方式我这里就不演示。

我的应用代码如下:

 
  1.  
  2. void ConfirmBtnClick()

  3. {

  4.  
  5. //SceneManager.LoadScene(1);

  6. string AssetsPath = Application.dataPath + "/Resources/";

  7. string Path = EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");

  8. if (Path.Length != 0)

  9. {

  10. Debug.Log("开始保存数据");

  11. ///TODO 保存数据

  12.  
  13. SceneManager.LoadScene(1);

  14. }

  15. else

  16. {

  17. Debug.Log("取消保存");

  18. }

  19.  
  20. }

因为接下来需要保存的数据还没有完成,所以这里只是debug一下。

上面的方法虽然在Unity编辑器里是可以打开文件夹的,但是我们将软件打包是会发现报错无法打包,这是因为该脚本引用using UnityEditor;只能在编辑器下使用,所以我们还是不要想着偷懒老老实实使用类吧,首先我们创建两个类。

 

 

接下来直接调用就可以了

 
  1. /// <summary>

  2. /// 打开windows保存窗口

  3. /// </summary>

  4. /// <returns>路径名(带名字)</returns>

  5. string loadFile()

  6. {

  7. OpenFileName openFileName = new OpenFileName();

  8. openFileName.structSize = Marshal.SizeOf(openFileName);

  9. openFileName.filter = "All files (*.*)|*.*";

  10. openFileName.file = new string(new char[256]);

  11. openFileName.maxFile = openFileName.file.Length;

  12. openFileName.fileTitle = new string(new char[64]);

  13. openFileName.maxFileTitle = openFileName.fileTitle.Length;

  14. openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径

  15. openFileName.title = "保存项目";

  16. openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

  17.  
  18. if (LocalDialog.GetSaveFileName(openFileName))

  19. {

  20. return openFileName.file;

  21.  
  22. }

  23. else

  24. {

  25. return null;

  26. }

  27.  
  28.  
  29. }

因为我需要获取返回的路径名使用,所以我将它写成带返回值的函数。我们实际调用一下试试

 
  1. /// <summary>

  2. /// 确定保存

  3. /// </summary>

  4. void ConfirmBtnClick()

  5. {

  6.  
  7. //SceneManager.LoadScene(1);

  8. //string AssetsPath = Application.dataPath + "/Resources/";

  9. //string Path = UnityEditor.EditorUtility.SaveFolderPanel("请选择文件夹", AssetsPath, "");

  10. string Path=loadFile();

  11. if (Path != null)

  12. {

  13. if (Path.Length != 0)

  14. {

  15. Debug.Log("开始保存数据");

  16.  
  17. }

  18. }

  19.  
  20. else

  21. {

  22. Debug.Log("取消保存");

  23. }

  24.  
  25. }

具体代码就这样

导出 Mesh的代码

我所需要的打开目录功能,主要是为了从导出mesh到该目录

public Transform object;
private string name;
void SaveAsset()
	{
		Mesh mesh = object.GetComponent<MeshFilter>().mesh;
		AssetDatabase.CreateAsset(mesh , "Assets/" + name + ".asset");
	}

去除代码空行方法(黑科技)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值