Unity下用trilib動(dòng)態(tài)導(dǎo)入模型

最近有個(gè)需求是,在unity中運(yùn)行項(xiàng)目時(shí)動(dòng)態(tài)載入模型,下面簡(jiǎn)單記錄用trilib導(dǎo)入模型的流程。剛接觸c#腳本語言還不熟悉,本次僅僅在官方提供的demo對(duì)應(yīng)腳本中提取加載模型的函數(shù),屏蔽掉不需要的功能,并且加入了載入模型大小自適應(yīng)的功能。trilib官網(wǎng)銜接點(diǎn)擊這里。
trilib包和測(cè)試demo可以通過我的百度網(wǎng)盤在這里下載,提取碼【wcxo】。
新建一個(gè)unity工程,然后導(dǎo)入下載的trilib 包,步驟可以參考視頻(需要梯子。其實(shí)也很簡(jiǎn)單,不懂的話查閱資料也可以了)。打開trilib資源包中Scenes文件夾下的 Scene1 - Asset Loader 即為加載資源的demo。demo中還包括了下載在線資源,對(duì)物體翻轉(zhuǎn),重置等功能,若不需要可以在腳本中把接口屏蔽掉。修改哪些腳本呢?雙擊進(jìn)入場(chǎng)景 Scene1 - Asset Loader,在Hierarchy窗口的Canvas下有AssetLoaderErrorDialog,FileLoader三部分。AssetLoader用于加載資源,ErrorDialog用于在Canvas上顯示錯(cuò)誤信息,FileLoader可能是用于加載資源時(shí)正常顯示加載界面吧,我沒有研究過不是很清楚。本文僅需要修改AssetLoader下的對(duì)應(yīng)腳本,點(diǎn)擊AssetLoader,在屬性欄中看到需要修改的腳本如下圖所示:
AssetLoaderWindow
打開AssetLoaderWindow.cs,如果只需要Load lacal Asset Button這一功能的話,就需要在這個(gè)腳本下把其他接口屏蔽掉(當(dāng)然這是很low的做法,更好的方法是自己寫一個(gè)腳本調(diào)用該腳本對(duì)應(yīng)方法,或者簡(jiǎn)單移植自己需要的部分)。
我想實(shí)現(xiàn)的功能是,在加載模型進(jìn)來時(shí),希望模型出現(xiàn)在一個(gè)固定位置,比如桌面上,而且無論加載的模型原大小是多少,都希望顯示出來時(shí)的大小都是一樣的(便于觀察)。核心代碼如下:
_rootGameObject=assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions);                                                                                                                                                    _rootGameObject.AddComponent<Renderer>();
 Vector3 MyPosition = GetCenter(_rootGameObject);//獲取父物體包圍盒的中心點(diǎn)
Bounds bound = GetBounds(_rootGameObject, true);//獲取父物體的包圍盒

 Vector3 Obj_size = bound.size;
 /*獲取包圍盒的最大邊用于確定縮放倍數(shù)*/
 var Max = Obj_size[0];
  for (int i=0;i<3;i++)
 {
     if (Obj_size[i] > Max)
     Max = Obj_size[i];
 }                     
Vector3 Obj_Scale =_rootGameObject.transform.localScale;

/*調(diào)整父物體的縮放比例*/
Obj_Scale.x = 10 * (1f / Max);
Obj_Scale.y = 10 * (1f / Max);
Obj_Scale.z = 10 * (1f / Max);                                              
_rootGameObject.transform.localScale = Obj_Scale;

/*讓物體出現(xiàn)在固定位置*/
Vector3 pos = _rootGameObject.transform.position;
pos.x = -1153f;
pos.y = -13f;
pos.z = -439f;
_rootGameObject.transform.position = pos;
GetCenter()實(shí)現(xiàn)如下:
private Vector3 GetCenter(GameObject target)
            {
                Renderer[] mrs = target.gameObject.GetComponentsInChildren<Renderer>();
                Vector3 center = target.transform.position;
                if (mrs.Length != 0)
                {
                    Bounds bounds = new Bounds(center, Vector3.zero);
                    foreach (Renderer mr in mrs)
                    {
                        bounds.Encapsulate(mr.bounds);
                    }
                    center = bounds.center;
                }
                return center;

            }
GetBounds()實(shí)現(xiàn)如下:
private Bounds GetBounds(GameObject target, bool include_children = true)
            {

                Renderer[] mrs = target.gameObject.GetComponentsInChildren<Renderer>();
                Vector3 center = target.transform.position;
                Bounds bounds = new Bounds(center, Vector3.zero);
                if (include_children)
                {
                    if (mrs.Length != 0)
                    {
                        foreach (Renderer mr in mrs)
                        {
                            bounds.Encapsulate(mr.bounds);
                        }
                    }
                }
                else
                {
                    Renderer rend = target.GetComponentInChildren<Renderer>();
                    if (rend)
                    {
                        bounds = rend.bounds;
                    }
                }

                return bounds;

            }
獲取包圍盒代碼不是我寫的,源Blog在這里。
最終效果圖如下:
房屋.PNG
地形.PNG
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容