標籤:unity openisntall 個人化安裝 免填邀請碼 一鍵加入遊戲房間
目的
1、Unity整合openinstall sdk?
最近在使用一個叫openinstall的SDK,通過它實現免填邀請碼的功能,整合到unity遊戲開發中。對App安裝流程的最佳化,尤其是免填寫邀請碼安裝,App推廣的有獎邀請活動更大程度的達到推廣爆炸式的效果。
在分享連結自訂各種動態參數(如推廣渠道號,邀請碼,遊戲房間號,使用者id等等)。通過在分享連結url中附帶app邀請人的使用者id,就可達到免填邀請碼的效果;或者app通過在url中附帶遊戲房間號實現直達遊戲房間也可建立上下級關係;unity開發中免不了邀請使用者獲得獎勵;新老使用者直達遊戲情境;從各種瀏覽器一鍵拉起遊戲情境等等;
那麼如何在unity整合openinstall呢?話不多說,吧~ 哈哈
2、註冊/登入openinstall開發人員平台http://developer.openinstall.io/並建立應用
先在AndroidStudio中工作
-
建立工程
只需要注意,AndroidStudio(以下簡稱AS)中建立項目的包名和Unity發布安卓時候設定的包名要一致。
2.匯入jar包
將Unity路徑下的介面classes.jar匯入到AS的libs目錄下,然後sync一下項目。介面包所在地:(E:\ProgramFiles\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar)
同樣的,到openinstall官網去下載openinstall_vx.x.x.jar放到AS的libs目錄下,sync一下項目
3.編寫MainActivity代碼
編寫MainActivity繼承自UnityPlayerActivity
import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.fm.openinstall.OpenInstall; import com.fm.openinstall.listener.AppInstallListener; import com.fm.openinstall.listener.AppWakeUpAdapter; import com.fm.openinstall.model.AppData; import com.fm.openinstall.model.Error; import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity {//1、注意修改整合的類,改成UnityPlayerActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getInstall();//個人化安裝 OpenInstall.getWakeUp(getIntent(), wakeUpAdapter);//擷取喚醒參數 } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); OpenInstall.getWakeUp(intent, wakeUpAdapter); } AppWakeUpAdapter wakeUpAdapter = new AppWakeUpAdapter() { @Override public void onWakeUp(final AppData appData) { //擷取渠道資料 String channelCode = appData.getChannel(); //擷取綁定資料 String bindData = appData.getData(); //回調資料 對象名稱,指令碼方法,返回的資料 UnityPlayer.UnitySendMessage("Main Camera","wakeup","data="+bindData+";channelCode="+channelCode); } }; /** * 在APP需要個人化安裝參數時(由web網頁中傳遞過來的,如邀請碼、遊戲房間號等自訂參數) * 調用OpenInstall.getInstall方法,在回調中擷取參數(可重複擷取) * 適用於免填邀請碼安裝,自動加好友,渠道統計等安裝來源追蹤的解決方案 */ public void getInstall() { //擷取OpenInstall資料,去掉外層的if判斷就可以重複多次調用,推薦每次需要的時候調用,而不是自己儲存資料 OpenInstall.getInstall(new AppInstallListener() { @Override public void onInstallFinish(AppData appData, Error error) { Log.d("OpenInstall ", "InstallFinish"); if (error == null) { //根據自己的業務處理返回的資料 String data = appData.getData(); String channelCode = appData.getChannel(); //回調資料 對象名稱,指令碼方法,返回的資料 UnityPlayer.UnitySendMessage("Main Camera","install","data="+data+";channelCode="+channelCode); } else { Log.e("MainActivity", "errorMsg : " + error.toString()); } } }); } //資料上報統計 public void reportRegister(){ OpenInstall.reportRegister(); } @Override protected void onDestroy() { super.onDestroy(); wakeUpAdapter = null; } }
4.自訂Application類 APP繼承Application並初始化
- 修改AndroidManifest.xml
在AndroidManifest.xml中添加許可權聲明,設定AppKey,配置scheme
在AndroidManifest.xml中的application標籤中添加 android:name=".App"指定自訂的Application類,以便程式啟動的時候初始化自訂Application類,而不是系統預設的Application類
- 修改build.gradle
7.生產arr包
Build->Build APK成功會在app\build\outputs\aar目錄下出現一個app-debug.aar包(此處是debug包),然後用解壓軟體開啟這個包,刪掉libs下的classes.jar(沒錯,就是之前重unity中拷過來的) 因為unity打包時會重新把內建的classes.jar打進去,如果不刪掉它會打包報錯,衝突然後這就是最終需要的aar包,把aar包解壓縮,看到classes.jar(此處的classes.jar是項目build出來的,不等同於AS中libs檔案中的classes.jar),把aar包下的classes.jar和AndroidManifest.xml都放在一個地方等待在unity中使用
Unity中調用
Unity建立一個工程,設定jdk,安卓的SDK。
最後在Assets目錄下建立我們需要的目錄,Assets\Plugins\Android,並把AndroidManifest.xml放到裡面。
然後在Assets目錄下建立我們需要的目錄,Assets\Plugins\Android\libs,並把aar包下的classes.jar和openinstall_vx.x.x.jar放到裡面。
Unity調用android方法
在Camera main中建立指令碼Main.cs調用android方法(反射機制擷取android方法)
根據自己的業務需求調用方法,如
public class Main : MonoBehaviour { public Text txtWakeup; public Text txtInstall; void Start() { txtWakeup = GameObject.Find("Canvas/txtWakeup").GetComponent<Text>(); txtInstall = GameObject.Find("Canvas/txtInstall").GetComponent<Text>(); AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("getInstall"); //jo.Call("reportRegister"); //上報註冊統計 txtWakeup.text = "喚醒參數"; txtInstall.text = "安裝參數"; } //接收Android回調的資料 wakeup方法 void wakeup(string str) { txtWakeup.text = str; } //接收Android回調的資料,install方法 void install(string str) { txtInstall.text = str; } }
打包上傳安裝包到openinstall後台測試:
上傳apk安裝包後,可先使用openinstall提供的線上測試功能,確保app安裝後能正確還原輸入的動態參數,能正常的拉起App。
注意:拿到openinstall回調資料後,具體的業務可具體處理!
demo:https://github.com/AnneCR/unity-openinstall/tree/master
unity整合openinstall流程