這是一篇站在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ì),你就是人生的贏家。