前言
一、Unity官方給的動(dòng)態(tài)資源加載方式
二、Unity中調(diào)用windows資源管理器
三、從資源管理器獲得文件地址后復(fù)制到Unity指定文件夾
附上全部代碼(不完善,僅框架)
前言
Unity官方給的動(dòng)態(tài)資源加載方式就不贅述了。大體分為3種:(1) Resources.Load (2)AssetBundle(3) WWW加載 。 其中前兩種大多用在本地資源加載,第三種用在web端從服務(wù)器加載。前兩種都只能從特定的文件夾下面加載資源,也就是Asset下面特定名字的幾個(gè)文件夾。
項(xiàng)目需求:打開(kāi)win資源瀏覽器,從任意目錄打開(kāi)一個(gè)任意格式3D模型,加載進(jìn)Unity主界面上。
一、Unity官方給的動(dòng)態(tài)資源加載方式
給幾個(gè)關(guān)鍵的鏈接和教程(講的都很詳細(xì)),以及官方文檔(完全沒(méi)懂在講啥),有需要的自取
Unity日常 - 資源加載與文件路徑_51CTO博客_unity動(dòng)態(tài)加載資源的方式
Unity AssetBundle 打包 四種加載方式 服務(wù)器加載_荷蘭豬小灰灰的博客-CSDN博客
AssetBundle 工作流程 - Unity 手冊(cè)
二、Unity中調(diào)用windows資源管理器
關(guān)鍵教程鏈接:
Unity對(duì)于window下的資源管理器的基本操作_unity 打開(kāi)windows資源管理器_以夢(mèng)為馬,不負(fù)韶華的博客-CSDN博客
但是這個(gè)教程中的文件格式過(guò)濾器有問(wèn)題,會(huì)導(dǎo)致在文件打開(kāi)窗口右下角的“類(lèi)型選擇”下拉表出現(xiàn)亂碼以及過(guò)濾不正確等問(wèn)題,具體格式應(yīng)該如下:
_ofn.filter = "模型文件(*.obj)\0*.obj;"
? ? ? ? ? ? + "\0模型文件(*.stl)\0*.stl;"
? ? ? ? ? ? + "\0所有支持文件(*.obj *.stl)\0*.obj;*.stl\0";
說(shuō)一下格式規(guī)則:
1、 \0*.obj 中間不能有間隔。
2、說(shuō)明文字 “模型文件(*.obj)
” 和給系統(tǒng)看的?“ *.obj”一定要用 “\0” 隔開(kāi)。
3、不同的類(lèi)型之間用“;” 隔開(kāi)。
三、從資源管理器獲得文件地址后復(fù)制到Unity指定文件夾
這里建立了一個(gè)緩存文件夾“ModelsBuffer”具體代碼如下:?
// 如果讀取成功,轉(zhuǎn)成OBJ先放入緩存文件夾:Resources/ModelsBuffer;
? ? ? ? bool judge_FileBuffer = false;
? ? ? ? if (LocalDialog.GetOpenFileName(_ofn)){
? ? ? ? ? ? String path_Buffer = Application.dataPath + "/Resources/ModelsBuffer/";
? ? ? ? ? ? bool judge_Path = false;
? ? ? ? ? ? if (!Directory.Exists(path_Buffer)){
? ? ? ? ? ? ? ? Directory.CreateDirectory(path_Buffer);
? ? ? ? ? ? ? ? judge_Path = true;
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? judge_Path = true;
? ? ? ? ? ? }
? ? ? ? ? ? if (_ofn.fileFull.Length != 0 && judge_Path) {?
? ? ? ? ? ? ? ? String dir_FilenameFull = path_Buffer + _ofn.fileTitle;
? ? ? ? ? ? ? ? System.IO.File.Copy(_ofn.fileFull, dir_FilenameFull, true);
? ? ? ? ? ? ? ? AssetDatabase.Refresh();
? ? ? ? ? ? ? ? judge_FileBuffer = true;
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? Debug.Log("文件路徑為空或存在不支持的語(yǔ)言符號(hào)種類(lèi)");
? ? ? ? ? ? }
? ? ? ? }
?
切記Copy完文件一定要做一次AssetDatabase.Refresh(),刷新Unity的資產(chǎn)包,這樣Unity才能在Resources/ModelsBuffer文件夾下正確獲取文件。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-762047.html
然后再?gòu)木彺嫦录虞d,具體代碼如下:
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-762047.html
// 從緩存文件夾下加載模型;
if (judge_FileBuffer){
String _tempFN = _ofn.fileTitle.Substring(0, _ofn.fileTitle.Length - 4);
var _obj = Resources.Load<GameObject>("ModelsBuffer/" + _tempFN);
if (_obj){
var _objInstant = Instantiate(_obj);
_objInstant.transform.localScale = Vector3.one * 30;
_objInstant.transform.localPosition = new Vector3(10f, -11.7f, 174.7f);
}
}
附上全部代碼(不完善,僅框架)
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using FileIO;
// ------------------------------------------------ 主程序運(yùn)行類(lèi) ------------------------------------------------ //
public class LoadFiles_win10 : MonoBehaviour
{
// ------------------------------------------------ 打開(kāi)模型;
// 3.單擊后打開(kāi)文件瀏覽器:
public void Click_OpenFileDialog()
{
//Debug.Log("getIn Click_OpenFileDialog");
FileNameStruct _ofn = new FileNameStruct();
_ofn.structSize = Marshal.SizeOf(_ofn);
_ofn.filter = "模型文件(*.obj)\0*.obj;"
+ "\0模型文件(*.stl)\0*.stl;"
+ "\0所有支持文件(*.obj *.stl)\0*.obj;*.stl\0";
_ofn.fileFull = new string(new char[256]);
_ofn.maxFile = _ofn.fileFull.Length;
_ofn.fileTitle = new string(new char[64]);
_ofn.maxFileTitle = _ofn.fileTitle.Length;
_ofn.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默認(rèn)路徑
_ofn.title = "載入模型文件";
_ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
// 如果讀取成功,轉(zhuǎn)成OBJ先放入緩存文件夾:Resources/ModelsBuffer;
bool judge_FileBuffer = false;
if (LocalDialog.GetOpenFileName(_ofn)){
String path_Buffer = Application.dataPath + "/Resources/ModelsBuffer/";
bool judge_Path = false;
if (!Directory.Exists(path_Buffer)){
Directory.CreateDirectory(path_Buffer);
judge_Path = true;
}
else {
judge_Path = true;
}
if (_ofn.fileFull.Length != 0 && judge_Path) {
String dir_FilenameFull = path_Buffer + _ofn.fileTitle;
System.IO.File.Copy(_ofn.fileFull, dir_FilenameFull, true);
AssetDatabase.Refresh();
judge_FileBuffer = true;
}
else{
Debug.Log("文件路徑為空或存在不支持的語(yǔ)言符號(hào)種類(lèi)");
}
}
// 從緩存文件夾下加載模型;
if (judge_FileBuffer){
String _tempFN = _ofn.fileTitle.Substring(0, _ofn.fileTitle.Length - 4);
var _obj = Resources.Load<GameObject>("ModelsBuffer/" + _tempFN);
if (_obj){
var _objInstant = Instantiate(_obj);
_objInstant.transform.localScale = Vector3.one * 30;
_objInstant.transform.localPosition = new Vector3(10f, -11.7f, 174.7f);
}
}
// 模型文件讀取后轉(zhuǎn)入數(shù)據(jù)庫(kù);
if (judge_FileBuffer) {
bool judge_Read = false;
FileIO_OBJ _io = new FileIO_OBJ();
judge_Read = _io.read_OBJ(_ofn.fileFull);
}
}
// ------------------------------------------------ 保存模型;
public void Click_SaveFileDialog()
{
Debug.Log("getIn Click_SaveFileDialog");
FileNameStruct _sfn = new FileNameStruct();
if (LocalDialog.GetSaveFileName(_sfn))
{
}
}
}
// ------------------------------------------- win10 文件瀏覽器調(diào)用類(lèi) ------------------------------------------- //
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
//1.OpenFileName數(shù)據(jù)接收類(lèi):
public class FileNameStruct
{
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 fileFull = 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;
}
//2.系統(tǒng)函數(shù)調(diào)用類(lèi):
public class LocalDialog
{
//鏈接指定系統(tǒng)函數(shù) 打開(kāi)文件對(duì)話框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] FileNameStruct ofn);
public static bool GetOFN([In, Out] FileNameStruct ofn) { return GetOpenFileName(ofn); }
//鏈接指定系統(tǒng)函數(shù) 另存為對(duì)話框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] FileNameStruct ofn);
public static bool GetSFN([In, Out] FileNameStruct ofn) { return GetSaveFileName(ofn); }
}
到了這里,關(guān)于Unity動(dòng)態(tài)加載資源 - 從硬盤(pán)任意地址加載任意格式3D模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!