android與unity交互

2017-3-6

這是一篇站在android角度寫unity的文章。
最近與unity的約會(huì)過于頻繁,導(dǎo)致矛盾出現(xiàn)。先來看下android與unity交互的操作,首先兩者要在一起。
unity3d項(xiàng)目導(dǎo)入android studio

[1]android與unity的簡(jiǎn)單約定

1、android調(diào)用unity,展示unity的方法。UnityPlayer.UnitySendMessage() 參數(shù)1表示發(fā)送游戲?qū)ο蟮拿Q(由unity方給出,而且是固定的),參數(shù)2表示對(duì)象綁定的腳本接收該消息的方法(由unity方給出,而且是固定的),參數(shù)3表示本條消息發(fā)送的字符串信息(雙方約定的,可以是一串字符,json數(shù)據(jù)等)

UnityPlayer.UnitySendMessage("Directional Light","ReceiveJson",data);

2、android下載unity文件并儲(chǔ)存到本地,以供unity讀取。

【1】android下載到的unity文件存儲(chǔ)路徑
String path=getContext().getExternalFilesDir("")+"/"+fileName;
【2】unity讀取文件的路徑
 WWW download = WWW.LoadFromCacheOrDownload ("file://"+Application.dataPath + "/fileName", 1);      

3、android與unity互傳數(shù)據(jù)格式的約定為json數(shù)據(jù)格式。

【1】創(chuàng)建javaBean類
/**
 * 全部匯總給unity
 */

public class TallScene {
    private FirstScene firstScene;
    private SecondScene secondScene;
    private ThirdScene thirdScene;
    private FourScene fourScene;

    public TallScene(){
        this.firstScene=new FirstScene();
        this.secondScene = new SecondScene();
        this.thirdScene = new ThirdScene();
        this.fourScene = new FourScene();
    }

    public TallScene(FirstScene firstScene) {
        this.firstScene = firstScene;
    }

    public FirstScene getFirstScene() {
        return firstScene;
    }

    public void setFirstScene(FirstScene firstScene) {
        this.firstScene = firstScene;
    }

    public SecondScene getSecondScene() {
        return secondScene;
    }

    public void setSecondScene(SecondScene secondScene) {
        this.secondScene = secondScene;
    }

    public ThirdScene getThirdScene() {
        return thirdScene;
    }

    public void setThirdScene(ThirdScene thirdScene) {
        this.thirdScene = thirdScene;
    }

    public FourScene getFourScene() {
        return fourScene;
    }

    public void setFourScene(FourScene fourScene) {
        this.fourScene = fourScene;
    }
}

【2】構(gòu)造json數(shù)據(jù)
public class CreateJson {


    //json對(duì)象

    public static String createJson(TallScene scene) {


        // TODO Auto-generated method stub

        try {
            // 第一個(gè)場(chǎng)景的json
            JSONObject firstJson = new JSONObject();

            firstJson.put("sexman", scene.getFirstScene().getSexman());
            firstJson.put("sexwoman", scene.getFirstScene().getSexwoman());

            // 第二個(gè)場(chǎng)景的json
            JSONObject secondJson = new JSONObject();
            secondJson.put("action", scene.getSecondScene().getAction());
            secondJson.put("background", scene.getSecondScene().getBackground());

            // 第三個(gè)場(chǎng)景的json
            JSONObject thirdJson = new JSONObject();
            thirdJson.put("background", scene.getThirdScene().getBackground());
            thirdJson.put("action", scene.getThirdScene().getAction());
            thirdJson.put("text", scene.getThirdScene().getText());
            thirdJson.put("time", scene.getThirdScene().getTime());
            // 第四個(gè)場(chǎng)景的json
            JSONObject fourScene = new JSONObject();
            fourScene.put("injection", scene.getFourScene().getInjection());
            fourScene.put("text", scene.getFourScene().getText());

            // 所有場(chǎng)景的json
            JSONObject allSceneJson = new JSONObject();

            allSceneJson.put("firstscene", firstJson);
            allSceneJson.put("secondscene",secondJson);
            allSceneJson.put("thirdscene",thirdJson);
            allSceneJson.put("fourthscene",fourScene);

            L.i("這個(gè)是所有場(chǎng)景的json", allSceneJson.toString() );
            return allSceneJson.toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
 }
[3]實(shí)例化json數(shù)據(jù),傳送給unity
 TallScene scene = new TallScene();
        scene.getFirstScene().setSexwoman(sex2);
        scene.getFirstScene().setSexman(sex1);

        scene.getSecondScene().setAction("secondanimation.assetbundle");
        scene.getSecondScene().setBackground("");

        scene.getThirdScene().setAction("ACHuge.assetbundle");
        scene.getThirdScene().setBackground("BGYinXing.assetbundle");
        scene.getThirdScene().setText("you are my heart!");
        scene.getThirdScene().setTime("ACHuge.assetbundle");

        scene.getFourScene().setText("自定義文字");
        scene.getFourScene().setInjection("huapen.assetbundle");

        String first=CreateJson.createJson(scene);

        UnityPlayer.UnitySendMessage("Directional Light","ReceiveJson",first);

[2]總結(jié)

1、在實(shí)現(xiàn)二者間的交互過程中,需要雙方都打印出log,這樣才能更好的看出問題在哪。關(guān)鍵點(diǎn)一是調(diào)用的方法,二是傳送的數(shù)據(jù)格式,三是文件存儲(chǔ)的路徑和讀取的路徑。
2、溝通很重要,文字表達(dá)要準(zhǔn)確,是下載文件打印log就說下載文件,是讀取文件就打印讀取文件,即使方法名叫download,但以實(shí)現(xiàn)的功能為標(biāo)準(zhǔn)。
3、unity在android中調(diào)試會(huì)不停的更改unity的版本,這時(shí)你只需要按照unity3d項(xiàng)目導(dǎo)入android studio,在這個(gè)的基礎(chǔ)上將main文件下assets文件和jniLibs文件delete,然后將新的unity3d項(xiàng)目libs文件里的三個(gè)文件拷貝到android下libs文件下覆蓋掉原來的即可。

[3]分享

開學(xué)了,作為一名大三的學(xué)生,即將走出社會(huì),有迷茫,有夢(mèng)想,卻困在其中走也走不出去。
最近看了一本書叫《因?yàn)橥矗越星啻骸罚芟矚g里面的一句話

人生從不嫌棄太年輕或太老。

還有一句也很喜歡,不過我在后面加了句話

你就是人生的贏家!我沒有統(tǒng)治世界的愿望,只有想要改變自己命運(yùn)的決心,我想做人生的大贏家。

精心準(zhǔn)備,等待屬于我的時(shí)機(jī)出現(xiàn),把握住每一個(gè)機(jī)會(huì),你就是人生的贏家。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,337評(píng)論 25 708
  • 111. [動(dòng)畫系統(tǒng)]如何將其他類型的動(dòng)畫轉(zhuǎn)換成關(guān)鍵幀動(dòng)畫? 動(dòng)畫->點(diǎn)緩存->關(guān)鍵幀 112. [動(dòng)畫]Unit...
    胤醚貔貅閱讀 13,203評(píng)論 3 89
  • 現(xiàn)在android開發(fā)都轉(zhuǎn)到android studio上了, unity與android交互,如unity打開a...
    菲得更高閱讀 7,707評(píng)論 2 13
  • 電動(dòng)車靈巧的拐進(jìn)小巷子,停在巷子中另一個(gè)分叉口上。走上兩級(jí)水泥臺(tái)階,是一個(gè)兩三米寬局促而不大體面的一個(gè)門面。原本這...
    阿饅仔閱讀 724評(píng)論 5 2
  • 白天在家無所事事的一天 晚上又開始失眠 有關(guān)抑郁的問題有一篇文章說的很對(duì) 大多數(shù)抑郁癥患者對(duì)自己對(duì)認(rèn)知非常清晰 而...
    寫給巧克力的歌閱讀 193評(píng)論 0 0