当前位置:   article > 正文

Unity动态加载资源 - 从硬盘任意地址加载任意格式3D模型_unity 载入stl

unity 载入stl

前言

一、Unity官方给的动态资源加载方式

二、Unity中调用windows资源管理器

三、从资源管理器获得文件地址后复制到Unity指定文件夹

附上全部代码(不完善,仅框架)


前言

Unity官方给的动态资源加载方式就不赘述了。大体分为3种:(1) Resources.Load (2)AssetBundle(3) WWW加载 。 其中前两种大多用在本地资源加载,第三种用在web端从服务器加载。前两种都只能从特定的文件夹下面加载资源,也就是Asset下面特定名字的几个文件夹。

项目需求:打开win资源浏览器,从任意目录打开一个任意格式3D模型,加载进Unity主界面上。


一、Unity官方给的动态资源加载方式

给几个关键的链接和教程(讲的都很详细),以及官方文档(完全没懂在讲啥),有需要的自取


Unity日常 - 资源加载与文件路径_51CTO博客_unity动态加载资源的方式
Unity AssetBundle 打包 四种加载方式 服务器加载_荷兰猪小灰灰的博客-CSDN博客
AssetBundle 工作流程 - Unity 手册

二、Unity中调用windows资源管理器

关键教程链接:

Unity对于window下的资源管理器的基本操作_unity 打开windows资源管理器_以梦为马,不负韶华的博客-CSDN博客

但是这个教程中的文件格式过滤器有问题,会导致在文件打开窗口右下角的“类型选择”下拉表出现乱码以及过滤不正确等问题,具体格式应该如下:

  1. _ofn.filter = "模型文件(*.obj)\0*.obj;"
  2.             + "\0模型文件(*.stl)\0*.stl;"
  3.             + "\0所有支持文件(*.obj *.stl)\0*.obj;*.stl\0";

说一下格式规则:
1、 \0*.obj 中间不能有间隔。
2、说明文字模型文件(*.obj)和给系统看的 “ *.obj”一定要用 “\0” 隔开。
3、不同的类型之间用“;” 隔开。

三、从资源管理器获得文件地址后复制到Unity指定文件夹


这里建立了一个缓存文件夹“ModelsBuffer”具体代码如下: 

  1. // 如果读取成功,转成OBJ先放入缓存文件夹:Resources/ModelsBuffer;
  2.         bool judge_FileBuffer = false;
  3.         if (LocalDialog.GetOpenFileName(_ofn)){
  4.             String path_Buffer = Application.dataPath + "/Resources/ModelsBuffer/";
  5.             bool judge_Path = false;
  6.             if (!Directory.Exists(path_Buffer)){
  7.                 Directory.CreateDirectory(path_Buffer);
  8.                 judge_Path = true;
  9.             }
  10.             else {
  11.                 judge_Path = true;
  12.             }
  13.             if (_ofn.fileFull.Length != 0 && judge_Path) { 
  14.                 String dir_FilenameFull = path_Buffer + _ofn.fileTitle;
  15.                 System.IO.File.Copy(_ofn.fileFull, dir_FilenameFull, true);
  16.                 AssetDatabase.Refresh();
  17.                 judge_FileBuffer = true;
  18.             }
  19.             else{
  20.                 Debug.Log("文件路径为空或存在不支持的语言符号种类");
  21.             }
  22.         }

 

切记Copy完文件一定要做一次AssetDatabase.Refresh(),刷新Unity的资产包,这样Unity才能在Resources/ModelsBuffer文件夹下正确获取文件。

然后再从缓存下加载,具体代码如下:

  1. // 从缓存文件夹下加载模型;
  2. if (judge_FileBuffer){
  3. String _tempFN = _ofn.fileTitle.Substring(0, _ofn.fileTitle.Length - 4);
  4. var _obj = Resources.Load<GameObject>("ModelsBuffer/" + _tempFN);
  5. if (_obj){
  6. var _objInstant = Instantiate(_obj);
  7. _objInstant.transform.localScale = Vector3.one * 30;
  8. _objInstant.transform.localPosition = new Vector3(10f, -11.7f, 174.7f);
  9. }
  10. }


附上全部代码(不完善,仅框架)

  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Runtime.InteropServices;
  8. using FileIO;
  9. // ------------------------------------------------ 主程序运行类 ------------------------------------------------ //
  10. public class LoadFiles_win10 : MonoBehaviour
  11. {
  12. // ------------------------------------------------ 打开模型;
  13. // 3.单击后打开文件浏览器:
  14. public void Click_OpenFileDialog()
  15. {
  16. //Debug.Log("getIn Click_OpenFileDialog");
  17. FileNameStruct _ofn = new FileNameStruct();
  18. _ofn.structSize = Marshal.SizeOf(_ofn);
  19. _ofn.filter = "模型文件(*.obj)\0*.obj;"
  20. + "\0模型文件(*.stl)\0*.stl;"
  21. + "\0所有支持文件(*.obj *.stl)\0*.obj;*.stl\0";
  22. _ofn.fileFull = new string(new char[256]);
  23. _ofn.maxFile = _ofn.fileFull.Length;
  24. _ofn.fileTitle = new string(new char[64]);
  25. _ofn.maxFileTitle = _ofn.fileTitle.Length;
  26. _ofn.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
  27. _ofn.title = "载入模型文件";
  28. _ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
  29. // 如果读取成功,转成OBJ先放入缓存文件夹:Resources/ModelsBuffer;
  30. bool judge_FileBuffer = false;
  31. if (LocalDialog.GetOpenFileName(_ofn)){
  32. String path_Buffer = Application.dataPath + "/Resources/ModelsBuffer/";
  33. bool judge_Path = false;
  34. if (!Directory.Exists(path_Buffer)){
  35. Directory.CreateDirectory(path_Buffer);
  36. judge_Path = true;
  37. }
  38. else {
  39. judge_Path = true;
  40. }
  41. if (_ofn.fileFull.Length != 0 && judge_Path) {
  42. String dir_FilenameFull = path_Buffer + _ofn.fileTitle;
  43. System.IO.File.Copy(_ofn.fileFull, dir_FilenameFull, true);
  44. AssetDatabase.Refresh();
  45. judge_FileBuffer = true;
  46. }
  47. else{
  48. Debug.Log("文件路径为空或存在不支持的语言符号种类");
  49. }
  50. }
  51. // 从缓存文件夹下加载模型;
  52. if (judge_FileBuffer){
  53. String _tempFN = _ofn.fileTitle.Substring(0, _ofn.fileTitle.Length - 4);
  54. var _obj = Resources.Load<GameObject>("ModelsBuffer/" + _tempFN);
  55. if (_obj){
  56. var _objInstant = Instantiate(_obj);
  57. _objInstant.transform.localScale = Vector3.one * 30;
  58. _objInstant.transform.localPosition = new Vector3(10f, -11.7f, 174.7f);
  59. }
  60. }
  61. // 模型文件读取后转入数据库;
  62. if (judge_FileBuffer) {
  63. bool judge_Read = false;
  64. FileIO_OBJ _io = new FileIO_OBJ();
  65. judge_Read = _io.read_OBJ(_ofn.fileFull);
  66. }
  67. }
  68. // ------------------------------------------------ 保存模型;
  69. public void Click_SaveFileDialog()
  70. {
  71. Debug.Log("getIn Click_SaveFileDialog");
  72. FileNameStruct _sfn = new FileNameStruct();
  73. if (LocalDialog.GetSaveFileName(_sfn))
  74. {
  75. }
  76. }
  77. }
  78. // ------------------------------------------- win10 文件浏览器调用类 ------------------------------------------- //
  79. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  80. //1.OpenFileName数据接收类:
  81. public class FileNameStruct
  82. {
  83. public int structSize = 0;
  84. public IntPtr dlgOwner = IntPtr.Zero;
  85. public IntPtr instance = IntPtr.Zero;
  86. public String filter = null;
  87. public String customFilter = null;
  88. public int maxCustFilter = 0;
  89. public int filterIndex = 0;
  90. public String fileFull = null;
  91. public int maxFile = 0;
  92. public String fileTitle = null;
  93. public int maxFileTitle = 0;
  94. public String initialDir = null;
  95. public String title = null;
  96. public int flags = 0;
  97. public short fileOffset = 0;
  98. public short fileExtension = 0;
  99. public String defExt = null;
  100. public IntPtr custData = IntPtr.Zero;
  101. public IntPtr hook = IntPtr.Zero;
  102. public String templateName = null;
  103. public IntPtr reservedPtr = IntPtr.Zero;
  104. public int reservedInt = 0;
  105. public int flagsEx = 0;
  106. }
  107. //2.系统函数调用类:
  108. public class LocalDialog
  109. {
  110. //链接指定系统函数 打开文件对话框
  111. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  112. public static extern bool GetOpenFileName([In, Out] FileNameStruct ofn);
  113. public static bool GetOFN([In, Out] FileNameStruct ofn) { return GetOpenFileName(ofn); }
  114. //链接指定系统函数 另存为对话框
  115. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  116. public static extern bool GetSaveFileName([In, Out] FileNameStruct ofn);
  117. public static bool GetSFN([In, Out] FileNameStruct ofn) { return GetSaveFileName(ofn); }
  118. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/125128
推荐阅读
相关标签
  

闽ICP备14008679号