Unity3D Android 91 Market Platform SDK Integration - 2. Init, Login, Logout

 유니티3D 안드로이드에 중국 91.com 플랫폼 SDK 연동 준비작업을 정리했었습니다. 이번에는 91SDK 초기화 및 로그인 로그아웃 처리를 정리해보겠습니다.


1. Init
public class MainActivity extends UnityPlayerActivity {
 private final int AppId = 123456;
 private final String AppKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
 private final String LOG_TAG = "UNITY91SDKTEST";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  init91SDK();
 }
 
 private void init91SDK() {
  // 결재와 버전 업데이트를 디버그 모드로. 개발 테스트 완료되면 삭제한다.
  //NdCommplatform.getInstance().ndSetDebugMode(0);
  
  NdAppInfo appInfo = new NdAppInfo();
  appInfo.setAppId(AppId);
  appInfo.setAppKey(AppKey);
  appInfo.setCtx(this);
  
  NdCommplatform.getInstance().initial(0, appInfo);
  NdCommplatform.getInstance().ndSetScreenOrientation(NdCommplatform.SCREEN_ORIENTATION_AUTO);
  // 게임내에서 sdk api 호출해서 로그아웃 말고 91 플랫폼안에서 로그아웃시에 대한 처리들
  // true면 계정 교체시 게임 재시작. false면 계정 교체 페이지 팝업
  NdCommplatform.getInstance().setRestartWhenSwitchAccount(false);
  // 91 플랫폼 계정 교체 감시 리스너
  NdCommplatform.getInstance().setOnSwitchAccountListener(new OnSwitchAccountListener() {

   @Override
   public void onSwitchAccount(int arg0) {
    // TODO Auto-generated method stub
    switch (arg0) {
     // setRestartWhenSwitchAccount == true. 유저가 게임을 재시작.
     // 게임 데이터를 저장해야함.
    case NdErrorCode.ND_COM_PLATFORM_ERROR_USER_RESTART:
     Log.d(LOG_TAG, "SwitchAccount ReStart Game");
     break;
     
     // setRestartWhenSwitchAccount == false. 유저가 계정 교체 시도.
     // 게임 데이터를 저장해야함.
    case NdErrorCode.ND_COM_PLATFORM_ERROR_USER_SWITCH_ACCOUNT:
     Log.d(LOG_TAG, "SwitchAccount User Switch Account");
     break;
     
     // setRestartWhenSwitchAccount == false. 계정 교체 로그인 성공
     // 새로운 계정을 위한 게임 초기화 필요.
    case NdErrorCode.ND_COM_PLATFORM_SUCCESS:
     Log.d(LOG_TAG, "SwitchAccount Success");
     break;
     
     // setRestartWhenSwitchAccount == false. 계정 교체 취소
    case NdErrorCode.ND_COM_PLATFORM_ERROR_CANCEL:
     Log.d(LOG_TAG, "SwitchAccount Cancel");
     break;
     
     // setRestartWhenSwitchAccount == false. 기타 오류로 로그인 실패
    default:
     Log.d(LOG_TAG, "SwitchAccount Error" + arg0);
     break;
    }
    UnityPlayer.UnitySendMessage("Plugin91Manager", "SwitchAccountResult_J", Integer.toString(arg0));
   }
  });
 }
 먼저 91SDK 초기화 부분입니다. 플러그인이 onCreate될 때 바로 처리하고 있습니다. appid나 appkey는 할당 받으신걸로 대체하시면됩니다. 조심해야할 것은 ndSetDebugMode 부분인데 개발시 결제와 업데이트 기능을 테스트 목적으로만 주석을 풀어 활성화하시고 실제 릴리즈때에는 해당 함수를 호출하면 안됩니다.

 또한 초기화하면서 유저가 91 플랫폼 상에서 로그아웃 진행하는 것에 대한 리스너 처리를 하고 있습니다.

 초기화가 되면 이렇게 Logcat에 남습니다. 3.2.3.2내용인데 3.2.5 이상 버전을 사용중이라면 초기화 처리에 이슈가 조금 있으니 링크에서 확인하시기 바랍니다.


2. Login
 public void Login91SDK_U() {
  runOnUiThread(new Runnable()
  {
   @Override
   public void run() {
    // TODO Auto-generated method stub
    NdCommplatform.getInstance().ndLogin(UnityPlayer.currentActivity, new OnLoginProcessListener() {
     @Override
     public void finishLoginProcess(int arg0) {
      // TODO Auto-generated method stub
      switch (arg0) {
       // 로그인 성공
      case NdErrorCode.ND_COM_PLATFORM_SUCCESS:
        String strUin = NdCommplatform.getInstance().getLoginUin();
        String strSessionId = NdCommplatform.getInstance().getSessionId();
        Log.d(LOG_TAG, "Login Success " + strUin + strSessionId);
        
        try {
   jsonObj.put("Uin", strUin);
          jsonObj.put("sessionId", strSessionId);
        } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
       }
       Log.d(LOG_TAG, "Login Success");
       break;
       // 로그인 취소
      case NdErrorCode.ND_COM_PLATFORM_ERROR_CANCEL:
       Log.d(LOG_TAG, "Login Cancel");
       break;
       // 로그인 실패
      default:
       Log.d(LOG_TAG, "Login Failed " + arg0);
       break;
      }
      try {
        jsonObj.put("result", arg0);
      } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
      }
      UnityPlayer.UnitySendMessage("Plugin91Manager", "LoginResult_J", jsonObj.toString());
     }     
    });
   }
  });
 }
 안드로이드 로그인 처리입니다.

 public void Login()
 {
  curActivity.Call("Login91SDK_U");
 }
 
 void LoginResult_J(string strResult)
{
 JsonData jData = JsonMapper.ToObject(strResult);
 int iResult = (int)jData["result"];
 
 switch(iResult)
 {
 case 0:
  //  public static final int ND_COM_PLATFORM_SUCCESS = 0;
  string strUid = jData["Uin"].ToString();
  string strSessionId = jData["sessionId"].ToString();
  SetLog("91.com Login Success");
  break;
 case -12:
  //  public static final int ND_COM_PLATFORM_ERROR_CANCEL = -12;
  SetLog("91.com Login Cancel");
  break;
 default:
  SetLog("91.com Login Failed " + strResult);
  break;
 }
}
 유니티3D Plugin91Manager 컴포넌트의 로그인 처리입니다. 자바에서 보낸 JSON 처리를 위해 LitJSON을 사용하고 있습니다.

float fYpos = 0;
  GUI.Label(new Rect(0, fYpos, 400, 100), Plugin91Manager.GetInstance().strLogMsg);
  
  fYpos += 50;
  if (GUI.Button (new Rect(0, fYpos, 100, 50), "Login") == true)
  {
   Plugin91Manager.GetInstance().Login();
  }
 로그인 호출하는 TestGUI의 OnGUI() 부분입니다.

 실행해서 Login 버튼을 누르면 위와같이 91 플랫폼의 로그인 창이 뜹니다.

 로그인에 성공한 모습입니다.

3. Logout
 public void Logout91SDK_U(final boolean bAutoLoginCancel) {
  runOnUiThread(new Runnable() {

   @Override
   public void run() {
    // TODO Auto-generated method stub
    boolean bLogin = NdCommplatform.getInstance().isLogined();
    if (bLogin == true) {
     if (bAutoLoginCancel == true) {
      Log.d(LOG_TAG, "Logout with Auto Login Cancel");
      NdCommplatform.getInstance().ndLogout(NdCommplatform.LOGOUT_TO_RESET_AUTO_LOGIN_CONFIG, UnityPlayer.currentActivity);
     } else {
      Log.d(LOG_TAG, "Logout");
      NdCommplatform.getInstance().ndLogout(NdCommplatform.LOGOUT_TO_NON_RESET_AUTO_LOGIN_CONFIG, UnityPlayer.currentActivity);
     }
    }
   }
  });
 }
 안드로이드 로그아웃 처리부분입니다. 로그아웃 처리는 크게 2가지입니다. 지금 보이는 ndLogout을 호출하는 방법과 게임 화면이 아닌 91 플랫폼 화면에서 유저가 계정 교체를 하려고 로그아웃 하는 경우죠. 후자의 경우에 대한 리스너는 위에 init부분에서 처리를 해줬습니다.

 public void Logout(bool bAutoLoginCancel = false)
 {
  curActivity.Call("Logout91SDK_U", bAutoLoginCancel);
  this.strLogMsg = "91.com Logout with AutoLoginCancel" + bAutoLoginCancel.ToString();
 }
 
 void SwitchAccountResult_J(string strResult)
 {
  // public static final int ND_COM_PLATFORM_ERROR_USER_RESTART = -51;
  // setRestartWhenSwitchAccount == true
  if (strResult == "-51")
  {
   this.strLogMsg = "User Game Restart. Require Game Save.";
  }
  // public static final int ND_COM_PLATFORM_ERROR_USER_SWITCH_ACCOUNT = -50;
  // setRestartWhenSwitchAccount == false
  else if(strResult == "-50")
  {
   this.strLogMsg = "User Try Account Switch. Require Game Save.";
  }
  // public static final int ND_COM_PLATFORM_SUCCESS = 0;
  else if(strResult == "0")
  {
   this.strLogMsg = "Account Switch Success. Require Game Init.";
  }
  // public static final int ND_COM_PLATFORM_ERROR_CANCEL = -12;
  else if(strResult == "-12")
  {
   this.strLogMsg = "Account Switch Cancel.";
  }
  else
  {
   this.strLogMsg = "Account Switch Failed " + strResult;
  }
 }
 Plugin91Manager의 로그인 처리입니다.

  fYpos += 50;
  if (GUI.Button(new Rect(0, fYpos, 100, 50), "Logout") == true)
  {
   Plugin91Manager.GetInstance().Logout(true);
  }
 TestGUI의 부분이구요. AutoLoginCancel에 true를 줬기 때문에 로그아웃 후 다시 로그인 시도하면 계정을 물어봅니다. false라면 기존에 사용했던 계정으로 바로 로그인을합니다.

 로그아웃한 화면입니다.

 다음에는 91.com 플랫폼 화면을 호출하는것과 유저 피드백, 업데이트 처리를 정리해보겠습니다.

댓글

이 블로그의 인기 게시물

'xxx.exe' 프로그램을 시작할 수 없습니다. 지정된 파일을 찾을 수 없습니다.

goorm IDE에서 node.js 프로젝트로 Hello World Simple Server 만들어 띄워보기

애드센스 수익을 웨스턴 유니온으로 수표대신 현금으로 지급 받아보자.