如何实现Unity C# GetSaveFileName()
如何实现Unity C# GetSaveFileName()?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
先放个效果图:
首先需要定义一个OpenFileName的类:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]public class OpenFileName{public int structSize = 0;public IntPtr dlgOwner = IntPtr.Zero;public IntPtr instance = IntPtr.Zero;public String filter = null;public String customFilter = null;public int maxCustFilter = 0;public int filterIndex = 0;public String file = null;public int maxFile = 0;public String fileTitle = null;public int maxFileTitle = 0;public String initialDir = null;public String title = null;public int flags = 0;public short fileOffset = 0;public short fileExtension = 0;public String defExt = null;public IntPtr custData = IntPtr.Zero;public IntPtr hook = IntPtr.Zero;public String templateName = null;public IntPtr reservedPtr = IntPtr.Zero;public int reservedInt = 0;public int flagsEx = 0;}
当然,也不是都用到了,只用到了一小部分,有性趣的童鞋可以个性化一下保存对话框,
然后是最重要的委托GetSaveName()方法:
public class DllUse{[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);}
挡挡挡,最后调用一下即可:
public static void OpenDialog(Action<Stream> onSave) { OpenFileName ofn = new OpenFileName(); ofn.structSize = Marshal.SizeOf(ofn); ofn.filter = "Excel (*.xls)\0*.xls\0\0"; ofn.file = new string(new char[256]); ofn.maxFile = ofn.file.Length; ofn.fileTitle = new string(new char[64]); ofn.maxFileTitle = ofn.fileTitle.Length; ofn.initialDir = UnityEngine.Application.dataPath;//默认路径ofn.title = "保存文件"; ofn.defExt = ".xls";//显示文件的类型ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (DllTest.GetSaveFileName(ofn)) { dosomething();string Savepath = Path.GetDirectoryName (ofn.file); Process.Start (Savepath); } }
看完上述内容,你们掌握如何实现Unity C# GetSaveFileName()的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。