Unity添加小米游戏SDK

因为游戏要上线小米的平台,所以游戏就要添加小米SDK,整了3天总算是把小米SDK添加上了~~

多亏找到了这个帖子:Unity3D接入小米盒子SDK

(小米人家论坛有官方贴出来的其他开发者的接入经验~妈蛋的360!论坛里面找不到,微信客服回复让我发邮件,QQ客服根本是死人,加了N次就没通过过,发邮件回复的是让我加入5万元推广计划!狗屁的360!!!)

骂完感觉舒服多了...虽然360的还是要做接入...TMD~~

主要就是记录一下自己添加过程中的一些要注意的东西~~

首先游戏的包名要符合他小米的要求:

 

 

因为我们的是游戏,所以包名:com.HorizonGame.Crisis.mi

然后按照Unity3D接入小米盒子SDK帖子里面说的,在Eclipse里面打包我们要的Jar,

不过我的代码稍微跟他的有点区别,因为他的是小米盒子不存在旋转屏幕的问题,但是手机不行,

要是不处理的话,在手机旋转屏幕后,会遇到关闭Activity(也就是说我们的游戏被关闭了)或者OnCreate函数重复执行(也就是说我们的游戏重启了)

 

MainActivity.Java的代码如下:

  1 package com.HorizonGame.Crisis.mi;
  2 
  3 import java.util.UUID;
  4 
  5 import android.content.pm.ActivityInfo;
  6 import android.content.res.Configuration;
  7 import android.os.Bundle;
  8 
  9 import com.unity3d.player.UnityPlayer;
 10 import com.unity3d.player.UnityPlayerActivity;
 11 import com.xiaomi.gamecenter.sdk.MiCommplatform;
 12 import com.xiaomi.gamecenter.sdk.MiErrorCode;
 13 import com.xiaomi.gamecenter.sdk.OnLoginProcessListener;
 14 import com.xiaomi.gamecenter.sdk.OnPayProcessListener;
 15 import com.xiaomi.gamecenter.sdk.entry.MiAccountInfo;
 16 import com.xiaomi.gamecenter.sdk.entry.MiAppInfo;
 17 import com.xiaomi.gamecenter.sdk.entry.MiBuyInfoOffline;
 18 import com.xiaomi.gamecenter.sdk.entry.MiAppType;
 19 
 20 public class MainActivity extends UnityPlayerActivity implements OnLoginProcessListener,OnPayProcessListener {
 21 
 22     public static MiAppInfo appInfo;
 23     
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         appInfo = new MiAppInfo();
 28         appInfo.setAppId("***************");// 这个根据自己的游戏而定
 29         appInfo.setAppKey("*************");// 这个根据自己的游戏而定
 30         appInfo.setAppType(MiAppType.offline);
 31         MiCommplatform.Init(this, appInfo);
 32     }
 33     
 34     @Override 
 35     public void onConfigurationChanged(Configuration newConfig)
 36     { 
 37         super.onConfigurationChanged(newConfig); 
 38     }
 39     
 40     public void Login() {
 41         MiCommplatform.getInstance().miLogin(MainActivity.this,MainActivity.this);
 42     }
 43 
 44     public void Buy(String m_str0, int m_int0) {
 45         MiBuyInfoOffline offline = new MiBuyInfoOffline();
 46         offline.setCpOrderId(UUID.randomUUID().toString());
 47         offline.setProductCode(m_str0);
 48         offline.setCount(m_int0);
 49         try {
 50             // 调用SDK执行购买操作
 51             MiCommplatform.getInstance().miUniPayOffline(MainActivity.this,offline, MainActivity.this);
 52         } 
 53         catch (Exception e) {
 54             e.printStackTrace();
 55         }
 56     }
 57     
 58     @Override
 59     public void finishLoginProcess(int arg0, MiAccountInfo arg1) {
 60 
 61         if (MiErrorCode.MI_XIAOMI_GAMECENTER_SUCCESS == arg0) 
 62         {
 63             UnityPlayer.UnitySendMessage("MI_SDK", "Result","GAMECENTER_SUCCESS");
 64         } 
 65         else if (MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_LOGINOUT_SUCCESS == arg0) 
 66         {
 67             UnityPlayer.UnitySendMessage("MI_SDK", "messgae","ERROR_LOGINOUT_SUCCESS");
 68         } 
 69         else if (MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_LOGINOUT_FAIL == arg0) 
 70         {
 71             UnityPlayer.UnitySendMessage("MI_SDK", "Result","ERROR_LOGINOUT_FAIL");
 72         } 
 73         else if (MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_ACTION_EXECUTED == arg0) 
 74         {
 75             UnityPlayer.UnitySendMessage("MI_SDK", "Result","ERROR_ACTION_EXECUTED");
 76         } 
 77         else {
 78             UnityPlayer.UnitySendMessage("MI_SDK", "Result", "else");
 79         }
 80     }
 81     
 82     @Override
 83     public void finishPayProcess(int arg0) {
 84         if (arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_SUCCESS)// 成功
 85         {
 86             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_SUCCESS");
 87         } 
 88         else if (arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_CANCEL|| arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_PAY_CANCEL)// 取消
 89         {
 90             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_ERROR_CANCEL");
 91         } 
 92         else if (arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_PAY_FAILURE)// 失败
 93         {
 94             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_ERROR_PAY_FAILURE");
 95         } 
 96         else if (arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_PAY_REPEAT)
 97         {
 98             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_ERROR_PAY_REPEAT");
 99         } 
100         else if (MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_ACTION_EXECUTED == arg0) 
101         {
102             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_ERROR_ACTION_EXECUTED");
103         } 
104         else if (arg0 == MiErrorCode.MI_XIAOMI_GAMECENTER_ERROR_LOGIN_FAIL)
105         {
106             UnityPlayer.UnitySendMessage("MI_SDK", "Result","MI_XIAOMI_GAMECENTER_ERROR_LOGIN_FAIL");
107         }
108     }
109 }

其实主要就是添加了下面这个函数:

    @Override 
    public void onConfigurationChanged(Configuration newConfig)
    { 
        super.onConfigurationChanged(newConfig); 
    }

然后AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.HorizonGame.Crisis.mi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.HorizonGame.Crisis.mi.MainActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在AndroidManifest.xml里面主要是在activity中添加了

 android:configChanges="orientation|keyboardHidden|screenSize"
 android:screenOrientation="landscape"

尤其是Android4.0往上,一定要添加screenSize这项,要不然无效的~~

 

然后其他的就继续参考Unity3D接入小米盒子SDK

该打包的打包,该在Unity里面写代码的写代码就行了,

然后测试一下~

终于成功了~

posted @ 2015-05-08 12:04  Ray Yang  阅读(4210)  评论(0编辑  收藏  举报