前言
近期需要查看三維模型,以前使用OpenGL ES進行開發,加載并顯示,這次需要使用unity來進行場景的呼叫,由于主戰場還是在手機端開發,這只是一個小功能,首先直接排除unity打包成apk的想法,然后上網找了很多的安卓呼叫unity,包括:as打包成aar給unity,相互傳jar包,感覺很是麻煩,并且都是以前的版本了,unity2018和unity2019差距很大,沒有找到合適的解決方案,
近期在bilibili翻到了將unity打包成庫給as的教學視頻,受益匪淺,記錄一下,
環境
as版本: 4.0.1
unity版本:2019.4.8f1
參考學習地址
官方案例github地址:https://github.com/Unity-Technologies/uaal-example
官方案例檔案:https://forum.unity.com/threads/integration-unity-as-a-library-in-native-android-app-version-2.751712/
官網案例的方法在這里不在贅述,以下為學習參考的地方:
bilibili講解視頻:https://www.bilibili.com/video/BV1QA411Y7N8/?spm_id_from=333.788.videocard.0
參考博客1(講了一半):https://blog.csdn.net/qq_45888298/article/details/104209044
參考博客2(在官方代碼的基礎上做了一些更改):https://blog.csdn.net/qq_40028144/article/details/108058407
創建自己的工程并呼叫自己的unity
創建自己的安卓工程沒有什么需要注意的,就正常創建就可以了,
創建自己的unity工程,為了方便操作,在這里我將安卓工程和unity工程放在一起了,
檔案目錄如下:

第一步我們需要先在我們的unity中進行操作:
打開File-》Build Settings
選擇安卓,如果是沒轉變成安卓平臺的專案還得轉換成安卓平臺:

點擊 add open scenes 加入自己制作的場景,選擇export project

然后點擊左下角的
更改名字:
一般other settings中的包名會自動更改,如果沒有自己更改,則自己手動更改,選擇最低sdk,最好是和as中的最低相同,

繼續更改other settings:兩者都選上,官網上有問題說是AR崩潰的,解決方案就是選擇IL2PP,選上ARM64,

更改publishing settings,必須有這個,不然不成功(可能吧),原來有的就引入,沒有的就創建,

到這里設定完成,點擊剛開始的export來匯出:


在剛才創建的unity工程下新建一個檔案androidBuild,用來存盤匯出的unity,
匯出成功之后的androidBuild檔案夾之中應該有以下東西:

可以看到這里的檔案結構跟unity2018中匯出的檔案結構已經有了很大的不同了,
第二步我們需要在我們的as中進行操作:
這里的操作跟官網的案例一樣,畢竟我也是照著學來的,,這里用照片來顯示了,想要復制粘貼的話請翻到前面的鏈接,下載官方案例查看或者博客里面找,
修改settings.gradle

注意藍色框中,我們需要寫上自己剛才匯出的unityLibrary路徑,
修改bulid.gradle(:app)

修改bulid.gradle(UnityLibraryTest)
UnityLibraryTest是我的as工程名字,

然后點擊左上角的
同步完成之后,如果成功了,你將會擁有一個

到這里我們的unity的庫就匯入成功了,那么怎么用呢?
我們需要定義一個類:

這個類中可以添加一些對unity界面中的一些操作:
比如:
添加一個按鈕并加入操作

在unity界面中通過手機回傳鍵回傳時需要重寫:

呼叫在MainActivity中寫:

在布局檔案中進行函式呼叫:

最后一項更改Strings.xml

加入紅色框的這一項,unity在自己的包中有這一項是固定的,需要在as中同樣定義這一個字串才能夠成功的呼叫,(可能吧)
到這里不出什么意外的話應該大功告成,可以連上手機進行呼叫試試了
那么,這樣呼叫,我們如果想要調整unity代碼的話應該怎么辦呢?總不能每次改一次代碼重新引入一次吧,
我的方法是在原來引入的unity工程的基礎之上,正常更改這個unity工程的代碼,然后像上述的那樣匯出操作一樣匯出到原來的檔案夾進行覆寫,

其余保持不變,然后as重新編譯連接,unity的代碼就改變了,as端不受影響,可以繼續開發as端,
官方案例中的一些問題整理(機器翻譯)
Q: @btristan:Looks like the way the unloading/quitting issues were addressed is by having Unity run in a separate process and explicitly killing it.
This was the workaround that my team had investigated but ultimately our client is unwilling to operate with the JVM split into multiple processes. This is because they use Android UI elements over the Unity view that need to access the objects from the app’s memory. To continue doing this, they would need to replicate their main process memory in the Unity process.
Ultimately we found other workarounds to use Unity in the same process but workarounds are not what we like to ship in production code. Will there be any solution (like tightening up Unity’s shut-down sequence) for apps that cannot be split?
看起來卸載/退出問題的解決方式是讓Unity在單獨的行程中運行并顯式地終止它,
這是我的團隊調查過的解決方案,但最終我們的客戶不愿意將JVM拆分為多個行程,這是因為他們在Unity視圖上使用Android UI元素,這些元素需要從應用程式的記憶體中訪問物件,為了繼續這樣做,他們需要在Unity行程中復制他們的主行程記憶體,
最終我們找到了在同一個程序中使用Unity的其他解決方案,但這些方案并不是我們希望在生產代碼中提供的,對于不能拆分的應用程式,會有什么解決方案(比如收緊Unity的關閉程式)?
A: IUnityPlayerLifecycleEvents introduced since 2019.3.b4+ provides easy way to react to two important lifecycle events of Unity Player:
? Unload - IUnityPlayerLifecycleEvents.onUnityPlayerUnloaded will be called when unity player will be unloaded though Application.Unload or UnityPlayer.unload(). (UnityPlayer in unloaded/paused state atm.)
? Quit - IUnityPlayerLifecycleEvents.onUnityPlayerQuitted is called when Unity player did quit. Please note process where unity was running will be killed after this call.
You can pass instance of IUnityPlayerLifecycleEvents to UnityPlayer constructor or override methods in subclasses of UnityPlayer or UnityPlayerActivity
@btristan you can try keep activity in same process and relay on Unload
IUnityPlayerLifecycleEvents自從2019.3.b4+提供了對Unity Player的兩個重要生命周期事件做出反應的簡單方法:
Unload卸載-當通過Application unload 或者UnityPlayer.unload(). (UnityPlayer處于卸載/暫停狀態atm)時IUnityPlayerLifecycleEvents.onUnityPlayerRunLoaded將會被呼叫
Quit退出-當Unity player退出時,IUnityPlayerLifecycleEvents.onUnityPlayerQuitd將會被呼叫,請注意,unity運行的行程將在這個呼叫后被終止,
您可以將IUnityPlayerLifecycleEvents的實體傳遞給UnityPlayer的建構式或者重寫UnityPlayer 或者UnityPlayerActivity的子類中的方法
@btristan您可以嘗試將activity保持在同一行程中,并relay on Unload
將這個unity視圖作為子視圖以及傳遞更多的互動資訊:
學習參考:https://www.cnblogs.com/zhxmdefj/p/13273560.html
UnitySendMessagehttps://stackoverflow.com/questions/45775916/call-unity-function-from-java/45776104#45776104
利用介面安卓和unity傳參呼叫https://stackoverflow.com/questions/32944478/callback-listener-in-unity-how-to-call-script-file-method-from-unityplayeracti/41018028#41018028
結語
本人是新手,不知道安卓在呼叫unity這方面的開發有沒有完整詳細的教程,自學一臉懵逼,希望有耐心大神指點,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/241371.html
標籤:其他
