欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity for易接sdk接入文档

程序员文章站 2022-04-20 17:22:59
...

一、登录易接官网下载安卓sdk

Unity for易接sdk接入文档

1、下载后对文件进行解压,YiJieSDKV2.7\网游\demo\u3d\game\Android的文件导入到unity

Unity for易接sdk接入文档

2、新建脚本文件YijieHelper.cs 

using UnityEngine;
using System.Collections;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System;
using System.IO;
using System.Net;
using Common;
using System.Globalization;
using InterSource;

public class YijieHelper : MonoBehaviour
{
    public static YijieHelper Instance;
    private AndroidJavaObject curActivity;
#if zs
    public static string LoginUrl = "http://center.tlly.yx285.com/iface/yijiesdk/sdklogin.php";
    public static string CP_PAY_SYNC_URL = "http://center.tlly.yx285.com/iface/yijiesdk/pay.php";
#else
    public static string LoginUrl = "http://112.73.72.23/iface/yijiesdk/sdklogin.php";
    public static string CP_PAY_SYNC_URL = "http://112.73.72.23/iface/yijiesdk/pay.php";
#endif
    //自建服务器 测服
    //	public static string CP_LOGIN_CHECK_URL = "http://testomsdk.xiaobalei.com:5555/cp/user/paylog/checkLoginZijian";
    //自建服务器 正服 
    //  public static string CP_LOGIN_CHECK_URL = "http://testomsdk.xiaobalei.com:5555/cp/user/paylog/checkLoginZijianP";
    //test
    //public static string CP_LOGIN_CHECK_URL = "http://testomsdk.xiaobalei.com:5555/cp/user/paylog/checkLogin";
    //release 
    public static string CP_LOGIN_CHECK_URL = "http://testomsdk.xiaobalei.com:5555/cp/user/paylog/checkLoginP";
	public static string CP_PAY_CHECK_URL = "http://testomsdk.xiaobalei.com:5555/cp/user/paylog/get?orderId=";
	
	public static string PAYTYPE_PAY = "pay";
	public static string PAYTYPE_CHARGE = "charge";
	public static string PAYTYPE_PAY_EXTEND = "payextend";
	public static string PAYTYPE_ROLE = "setData";
	string goodName = "";
	string keyName = "";
	int money = 0;
	string desc = "";
	string paytype = "";
	string	setDataType = "";
	Rect windowRect = new Rect (20, 20, 400, 600);
	string str = "MainCamera";
	public SFOnlineUser user;
	public static Boolean bLogined = false;
	public Boolean isQuerying = false;
	public Boolean isDebug = false;
	public Boolean isPause = false;
	public Boolean isFocus = false;
	public ArrayList orderIds = new ArrayList ();
	struct OderId
	{
		public int retry;
		public string orderId;
	}
	public GUISkin guiSkin;

	
	/**
	 * exit接口用于系统全局退出
	 * @param context      上下文Activity
	 * @param gameObject   游戏场景中的对象
	 * @param listener     退出的监听函数,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到退出通知后触发
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void exit (IntPtr context, string gameObject, string listener);

	/**
	 * onCreate_listener接口用于初始化回调
	 * @param context      上下文Activity
	 * @param gameObject   游戏场景中的对象
	 * @param listener     退出的监听函数,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到退出通知后触发
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void onCreate_listener (IntPtr context, string gameObject, string listener);
	
	/**
	 * login接口用于SDK登陆
	 * @param context      上下文Activity
	 * @param customParams 自定义参数
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void login (IntPtr context, string customParams);
	
	/**
	 * logout接口用于SDK登出
	 * @param context      上下文Activity
	 * @param customParams 自定义参数
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void logout (IntPtr context, string customParams);
	
	/**
	 * charge接口用于用户触发非定额计费
	 * @param context      上下文Activity
	 * @param gameObject   游戏场景中的对象
	 * @param itemName     虚拟货币名称
	 * @param unitPrice    游戏道具单位价格,单位-分
	 * @param count        商品或道具数量
	 * @param callBackInfo 由游戏开发者定义传入的字符串,会与支付结果一同发送给游戏服务器,
	 *                     游戏服务器可通过该字段判断交易的详细内容(金额角色等)
	 * @param callBackUrl  将支付结果通知给游戏服务器时的通知地址url,交易结束后,
	 * 					   系统会向该url发送http请求,通知交易的结果金额callbackInfo等信息
	 * @param payResultListener  支付监听接口,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到通知后触发
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void charge (IntPtr context, string gameObject, string itemName, int unitPrice,
            int count, string callBackInfo, string callBackUrl, string payResultListener);
	
	/**
	 * pay接口用于用户触发定额计费
	 * @param context      上下文Activity
	 * @param gameObject   游戏场景中的对象
	 * @param unitPrice    游戏道具单位价格,单位-分
	 * @param unitName     虚拟货币名称
	 * @param count        商品或道具数量
	 * @param callBackInfo 由游戏开发者定义传入的字符串,会与支付结果一同发送给游戏服务器,
	 *                     游戏服务器可通过该字段判断交易的详细内容(金额角色等)
	 * @param callBackUrl  将支付结果通知给游戏服务器时的通知地址url,交易结束后,
	 * 					   系统会向该url发送http请求,通知交易的结果金额callbackInfo等信息
	 * @param payResultListener  支付监听接口,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到通知后触发
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void pay (IntPtr context, string gameObject, int unitPrice, string unitName,
            int count, string callBackInfo, string callBackUrl, string payResultListener);
	
	/**
	 * payExtend接口用于用户触发定额计费
	 * @param context      上下文Activity
	 * @param gameObject   游戏场景中的对象
	 * @param unitPrice    游戏道具单位价格,单位-分
	 * @param unitName     虚拟货币名称
	 * @param itemCode     商品ID
	 * @param remain       商品自定义参数
	 * @param count        商品或道具数量
	 * @param callBackInfo 由游戏开发者定义传入的字符串,会与支付结果一同发送给游戏服务器,
	 *                     游戏服务器可通过该字段判断交易的详细内容(金额角色等)
	 * @param callBackUrl  将支付结果通知给游戏服务器时的通知地址url,交易结束后,
	 * 					   系统会向该url发送http请求,通知交易的结果金额callbackInfo等信息
	 * @param payResultListener  支付监听接口,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到通知后触发
	 * */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void payExtend (IntPtr context, string gameObject, int unitPrice, string unitName,
		string itemCode, string remain, int count, string callBackInfo, string callBackUrl, string payResultListener);
	
	/**
	 * 部分渠道如UC渠道,要对游戏人物数据进行统计,而且为接入规范,调用时间:在游戏角色登录成功后调用
	 *  public static void setRoleData(Context context, String roleId,
	 *  	String roleName, String roleLevel, String zoneId, String zoneName)
	 *  
	 *  @param context   上下文Activity
	 *  @param roleId    角色唯一标识
	 *  @param roleName  角色名
	 *  @param roleLevel 角色等级
	 *  @param zoneId    区域唯一标识
	 *  @param zoneName  区域名称
     * */
    //setRoleData接口用于部分渠道如UC渠道,要对游戏人物数据进行统计,接入规范:在游戏角色登录成功后
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void setRoleData (IntPtr context, string roleId,
            string roleName, string roleLevel, string zoneId, string zoneName);
	
	//备用接口
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void setData (IntPtr context, string key, string value);
	
	/**
	 *	setLoginListener方法用于设置登陆监听
	 * 初始化SDK
	 *  @param context      上下文Activity
	 *  @param gameObject	游戏场景中的对象,SDK内部完成计费逻辑后,
	 * 						并把计费结果通过Unity的
	 * 						API(com.unity3d.player.UnityPlayer.UnitySendMessage(String gameObject,
	 * 								StringruntimeScriptMethod,Stringargs)
	 * 						通知到Unity,故游戏开发者需要指定一个游戏对象和该对象的运行脚本,用于侦听SDK的计费结果
	 * @param listener      登录的监听函数,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到通知后触发
	 */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void setLoginListener (IntPtr context, string gameObject, string listener);

	/**
	 *	extend扩展接口
	 * 扩展接口
	 *  @param context      上下文Activity
	 * @param data          data
	 *  @param gameObject	游戏场景中的对象,
	 * @param listener      扩展的监听函数,隶属于gameObject对象的运行时脚本的方法名称,该方法会在收到通知后触发
	 */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern void extend (IntPtr context, string data,string gameObject, string listener);
	
	/**
	 *	isMusicEnabled方法用于判断SDK是否需要打开游戏声音,目前只有移动基地需要此接口,
	 *  游戏开发者需要根据该返回值,设定游戏背景音乐是否开启
	 *
	 *  @param context      上下文Activity
	 */
	[DllImport("gangaOnlineUnityHelper")]
	private static extern Boolean isMusicEnabled (IntPtr context);
	
	GUI.WindowFunction windowFunction;

	void OnGUI ()
	{
		if (guiSkin) {   
			GUI.skin = guiSkin;
		} 
		//windowRect = GUI.Window (0, windowRect, DoMyWindow, str);

	}
	
	string createQueryURL (string orderId)
	{
		if (user == null) {
			return null;
		}
		StringBuilder builder = new StringBuilder ();
		builder.Append (CP_PAY_CHECK_URL);
		builder.Append ("?orderId=");
		builder.Append (orderId);
		return builder.ToString ();
	}


    //bool startcheck;  

    
    // Use this for initialization
    void Start ()
	{    
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "onstart" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                onCreate_listener( curActivity.GetRawObject(), "U8SDKhelper", "InitResult" );
                Debug.Log( "onstart完毕" );
            }
        }     
        Debug.Log( "22222222222222222222" );

    }

    // Update is called once per frame
    void Update ()
	{
	    if(Input.GetKey(KeyCode.Backspace) || Input.GetKey(KeyCode.Escape)){
            // Application.Quit();
			using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
				using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
				   exit(curActivity.GetRawObject (), "U8SDKhelper", "ExitResult");
				}
			}
        }
	}
	void extendCallback(string result){
		Debug.Log ("扩展回调:"+result);
		}
	void callback1(string result){
		Debug.Log ("------------callback1=" + result);
		SFJSONObject sfjson = new SFJSONObject (result);
		string tag = (string)sfjson.get ("tag");
		string value = (string)sfjson.get ("value");
		Debug.Log ("tag:" + tag + " value:" + value);
		if (tag.Equals ("success")) {
			Debug.Log ("成功");
				} else {
			Debug.Log ("失败");
				}
	}
	//初始化回调
	void InitResult(string result){
		if(result.Equals ("success"))
		{
			Debug.Log ("成功");
		}else
		{
			Debug.Log ("失败");
		}
		
	}
	void callback2(string result){
		Debug.Log ("------------callback2=" + result);
		SFJSONObject sfjson = new SFJSONObject (result);
		string tag = (string)sfjson.get ("tag");
		string value = (string)sfjson.get ("value");
		Debug.Log ("tag:" + tag + " value:" + value);
		if (tag.Equals ("success")) {
			Debug.Log ("成功");
		} else {
			Debug.Log ("失败");
		}
	}


    //易接登录接口
    public void YijieLogin()
    {
      
        Debug.Log( "login 0" );
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "login 1" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                setLoginListener( curActivity.GetRawObject(), "U8SDKhelper", "LoginResult" );
                Debug.Log( "setloginlistener" );
                login( curActivity.GetRawObject(), "Login" );
                Debug.Log( "login 2" );           
               
            }
        }
        Debug.Log( "易接登录接口调用完毕" );
    }
    // 登陆监听函数
    public string userinfos;
    void LoginResult (string result)
	{
        userinfos = result;
        Debug.Log ("------------loginResult=" + result);
		SFJSONObject sfjson = new SFJSONObject (result);
		string type = (string)sfjson.get ("result");	
		string customParams = (string)sfjson.get ("customParams");
		if (APaymentHelper.LoginResult.LOGOUT == type) {
			str = "login result = logout" + customParams;
            onLogout();
            user = null;
			if (!isDebug) {
				bLogined = false;
			}
			//Invoke("doLogin", 0.2f); 
			
		} else if (APaymentHelper.LoginResult.LOGIN_SUCCESS == type) {	
			SFJSONObject userinfo = (SFJSONObject)sfjson.get ("userinfo");
			if (userinfo != null) {
				long id = long.Parse ((string)userinfo.get ("id"));
				string channelId = (string)userinfo.get ("channelid");
				string ChannelUserId = (string)userinfo.get ("channeluserid");
				string UserName = (string)userinfo.get ("username");
				string Token = (string)userinfo.get ("token");
				string ProductCode = (string)userinfo.get ("productcode");
                DisplayMap.mPid = channelId;															
				Debug.Log ("## id:" + id + " channelId:" + channelId + " ChannelUserId:" + ChannelUserId
					+ " UserName:" + UserName + " Token:" + Token + " ProductCode:" + ProductCode);
			}
            StartCoroutine( _DoSdkLogin( DisplayMap.mPid, userinfos ) );

            str = "login result = login success" + customParams; 
			//LoginCheck ();
		} else if (APaymentHelper.LoginResult.LOGIN_FAILED == type) {
			str = "login result = login failed" + customParams; 
		} 
	}
    //登录验证接口
    public string YijiesdkUserID;
    public string userName;
    public IEnumerator _DoSdkLogin( string mPid, string msg )
    {
        var formdata = new WWWForm();
        formdata.AddField( "pid", mPid );
        formdata.AddField( "yijiedata", msg );
        var www = new WWW( LoginUrl, formdata );
        Debug.Log( "访问地址:" + www.url );
        while( !www.isDone )
        {
            yield return null;
        }

        if( www.error == null )
        {

            JsonData jsonData = JsonMapper.ToObject( www.text );
            Debug.Log( "data" + jsonData );
            string reuslt = jsonData["r"].ToString();
            string username = jsonData["prefix"].ToString();
            YijiesdkUserID = jsonData["sdkUserID"].ToString();         
            Debug.Log( "sdkuserid" + YijiesdkUserID );
            if( reuslt == "fail" )
            {
                Global.Alert( "验证失败,请确认您的账号!" );
            }
            else if( reuslt == "success" )
            {
                Debug.Log( "返回成功!" );
                userName = username + "_" + YijiesdkUserID;
                UIMsgReceive.instance.XiguSDKAndroidLoginButton();  //游戏登录接口
            }
        }
    }
    //易接Androidsdk EnterGame接口
    /*IntPtr context, string roleId,string roleName, string roleLevel, string zoneId, string zoneName*/
    public void onEnterGame()
    {
        SFJSONObject gameinfo = new SFJSONObject();
        gameinfo.put( "roleId", YijiesdkUserID ); //当前登录的玩家角色ID,必须为数字
        gameinfo.put( "roleName", DisplayMap.playername );  //当前登录的玩家角色名,不能为空,不能为null
        gameinfo.put( "roleLevel", "1" );    //当前登录的玩家角色等级,必须为数字,且不能为0,若无,传入1
        gameinfo.put( "zoneId", DisplayMap.mServerId );   //当前登录的游戏区服ID,必须为数字,且不能为0,若无,传入1
        gameinfo.put( "zoneName", DisplayMap.mServerName );  //当前登录的游戏区服名称,不能为空,不能为null
        gameinfo.put( "balance", Global.mInterClient.mMainCharacter.mVCoin.ToString() );   //用户游戏币余额,必须为数字,若无,传入0
        gameinfo.put( "vip", "1" );            //当前用户VIP等级,必须为数字,若无,传入1
        gameinfo.put( "partyName", "无帮派" );//当前角色所属帮派,不能为空,不能为null,若无,传入“无帮派”
        gameinfo.put( "roleCTime", DisplayMap.createtime );    //单位为秒,创建角色的时间
        Debug.Log( "创建角色时间" + DisplayMap.createtime );
        gameinfo.put( "roleLevelMTime", DisplayMap.servertime );  //单位为秒,角色等级变化时间 
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "onterGame" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                setRoleData( curActivity.GetRawObject(), YijiesdkUserID, DisplayMap.playername, "1", DisplayMap.mServerId, DisplayMap.mServerName );
                Debug.Log( "setRoleData接口调用完毕" );
                setData( curActivity.GetRawObject(), "enterServer", gameinfo.toString() );
                Debug.Log( "enterGame接口调用完毕" );

            }
        }       
    }
    //易接角色升级接口
    public void onUpdateGame(string updadelevel)
    {
        SFJSONObject gameinfo = new SFJSONObject();
        gameinfo.put( "roleId", YijiesdkUserID ); //当前登录的玩家角色ID,必须为数字
        gameinfo.put( "roleName", DisplayMap.playername );  //当前登录的玩家角色名,不能为空,不能为null
        gameinfo.put( "roleLevel", updadelevel );    //当前登录的玩家角色等级,必须为数字,且不能为0,若无,传入1
        gameinfo.put( "zoneId", DisplayMap.mServerId );   //当前登录的游戏区服ID,必须为数字,且不能为0,若无,传入1
        gameinfo.put( "zoneName", DisplayMap.mServerName );  //当前登录的游戏区服名称,不能为空,不能为null
        gameinfo.put( "balance", Global.mInterClient.mMainCharacter.mVCoin.ToString() );   //用户游戏币余额,必须为数字,若无,传入0
        gameinfo.put( "vip", "1" );            //当前用户VIP等级,必须为数字,若无,传入1
        gameinfo.put( "partyName", "无帮派" );//当前角色所属帮派,不能为空,不能为null,若无,传入“无帮派”
        gameinfo.put( "roleCTime", DisplayMap.createtime );    //单位为秒,创建角色的时间
        Debug.Log( "创建角色时间" + DisplayMap.createtime );
        gameinfo.put( "roleLevelMTime", DisplayMap.servertime );  //单位为秒,角色等级变化时间     
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "onupdateGame" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                setData( curActivity.GetRawObject(), "levelup", gameinfo.toString() );
                Debug.Log( "update接口调用完毕" );
            }
        }       
    }
    //易接支付接口
    public void Pay(int unitPrice,int count,string callBackInfo )
    {
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "paypppppppppppppp" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                pay( curActivity.GetRawObject(), "U8SDKhelper", unitPrice, "元宝", count, callBackInfo, CP_PAY_SYNC_URL, "PayResult" );
                Debug.Log( "易接支付接口调用完毕" );

            }
        }       
    }

    // 支付监听函数
    void PayResult( string result )
    {
        Debug.Log( "------------PayResult=" + result );
        SFJSONObject sfjson = new SFJSONObject( result );
        string type = (string)sfjson.get( "result" );
        string data = (string)sfjson.get( "data" );

        if( APaymentHelper.PayResult.PAY_SUCCESS == type )
        {
            str = "pay result = pay success " + data;
            Global.Alert( "支付成功!" );
        }
        else if( APaymentHelper.PayResult.PAY_FAILURE == type )
        {
            str = "pay result = pay failure" + data;
            Global.Alert( "支付失败!" );
        }
        else if( APaymentHelper.PayResult.PAY_ORDER_NO == type )
        {
            str = "pay result = pay order No" + data;
        }
    }
    //ExitResult 退出监听函数
    void ExitResult (string result)
	{
		Debug.Log ("------------ExitResult=" + result);
		SFJSONObject sfjson = new SFJSONObject (result);
		string type = (string)sfjson.get ("result");
		string data = (string)sfjson.get ("data");
	
		if (APaymentHelper.ExitResult.SDKEXIT == type) {
			//SDK退出
			if(data.Equals("true"))
			{
				Application.Quit();
			} 
			else if(data.Equals("false"))
			{
				
			}
		} else if (APaymentHelper.ExitResult.SDKEXIT_NO_PROVIDE == type) {
			//游戏自带退出界面
			Application.Quit();
		}
	}

    /*切换账号回调*/
    void onLogout()
    {
        Debug.Log( "调用onLogout" );
        Msgparam msg = new Msgparam();
        msg.msgType = UIMsgType.BACKLOGIN;
        Global.uiMsgReceive.uiToui( msg );
        DisplayMap.mIsAutoLogin = false;
        YijieLogin();
    }
    /*onLoginSuccess*/
    void onLoginSuccess( string result )
    {
        Debug.Log( "调用onLoginSuccess" );
        Msgparam msg = new Msgparam();
        msg.msgType = UIMsgType.BACKLOGIN;
        Global.uiMsgReceive.uiToui( msg );
        DisplayMap.mIsAutoLogin = false;
        LoginResult( result );
        Debug.Log( "onLoginSuccess切换账号调用完毕" );

    }

    void Awake ()
	{
        Debug.Log( "1111111111111111111111111" );
        Instance = this;
        Debug.Log( "login 00000" );
        using( AndroidJavaClass unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
        {
            Debug.Log( "login 111111" );
            using( AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" ) )
            {
                setLoginListener( curActivity.GetRawObject(), "U8SDKhelper", "LoginResult" );
                Debug.Log( "setloginlistener" );               

            }
        }
#region 111111111
        goodName = PlayerPrefs.GetString ("goodName", "");   
        money = PlayerPrefs.GetInt ("money", 0);   
        string itemCode =  PlayerPrefs.GetString ("goodid", "");   
        string remain =  PlayerPrefs.GetString ("remain", "");   
        Debug.Log ("itemCode=" +itemCode+" remain="+remain);
        bLogined = PlayerPrefs.GetInt ("login", 0) == 0 ? false : true;
        keyName = PlayerPrefs.GetString ("keyName",""); 
        if(goodName.Equals("") || money == 0)
        {
           desc = ""; 
           paytype = ""; 
        } 
        else 
        {
           desc = PlayerPrefs.GetString ("desc", ""); 
           paytype = PlayerPrefs.GetString ("paytype", ""); 

        }
        if(keyName.Equals("")){
            setDataType = ""; 
        }else{
            setDataType= PlayerPrefs.GetString ("setDataType", ""); 
        }


        PlayerPrefs.DeleteAll();

        if (Application.platform == RuntimePlatform.Android) {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
                   //setLoginListener(curActivity.GetRawObject (), "U8SDKhelper", "LoginResult");
                    Debug.Log ("Awake setDataType:" + setDataType);
                    if (bLogined == true) 
                    {
                        Debug.Log ("Alogin result = login success" );
                        str = "login result = login success";
                        if (paytype.Equals (PAYTYPE_PAY)) {
                            str = "PAYTYPE_PAY";
                            pay (curActivity.GetRawObject (), "U8SDKhelper", money, goodName, 1, desc, CP_PAY_SYNC_URL, "PayResult");
                        } else if (paytype.Equals (PAYTYPE_CHARGE)) {
                            str = "PAYTYPE_CHARGE";
                            charge (curActivity.GetRawObject (), "U8SDKhelper", goodName, money, 1, desc, CP_PAY_SYNC_URL, "PayResult");
                        } else if(paytype.Equals (PAYTYPE_PAY_EXTEND)){
                            str = "PAYTYPE_PAY_EXTEND";
                            payExtend (curActivity.GetRawObject (), "U8SDKhelper", money, goodName, itemCode, remain, 
                                1, desc, CP_PAY_SYNC_URL, "PayResult"); 
                        }else if(setDataType.Equals(PAYTYPE_ROLE)){
                            str = "PAYTYPE_ROLE";
                            SFJSONObject gameinfo = new SFJSONObject();
                            gameinfo.put("roleId", "1");//当前登录的玩家角色ID,必须为数字
                            gameinfo.put("roleName", "猎人");//当前登录的玩家角色名,不能为空,不能为null
                            gameinfo.put("roleLevel", "100");//当前登录的玩家角色等级,必须为数字,且不能为0,若无,传入1
                            gameinfo.put("zoneId", "1");//当前登录的游戏区服ID,必须为数字,且不能为0,若无,传入1
                            gameinfo.put("zoneName", "阿狸一区");//当前登录的游戏区服名称,不能为空,不能为null
                            gameinfo.put("balance", "0");   //用户游戏币余额,必须为数字,若无,传入0
                            gameinfo.put("vip", "1");            //当前用户VIP等级,必须为数字,若无,传入1
                            gameinfo.put("partyName", "无帮派");//当前角色所属帮派,不能为空,不能为null,若无,传入“无帮派”
                            gameinfo.put("roleCTime", "21322222");    //单位为秒,创建角色的时间
                            gameinfo.put("roleLevelMTime", "54456556");  //单位为秒,角色等级变化时间,如果没有就传-1
                            setData(curActivity.GetRawObject (),keyName,gameinfo.toString());//创建新角色时调用,必接接口
                        }
                    }	
                    Debug.Log ("user had payed str:" + str);

                }
            }
        }
        isPause = false;

        isFocus = false;
        Debug.Log( "pay11111111111111111111111111111" );
#endregion
    }

    void LoginCheck ()
	{
		if (isDebug == true) {
			bLogined = true;
			return;
		}

		string url = createLoginURL ();
		Debug.Log ("LoginCheck url:" + url);
		if (url == null)
			return;
		string result = executeHttpGet (url);
		Debug.Log ("LoginCheck result:" + result);
		if (result == null || !(result == "SUCCESS")) {
			bLogined = false;

		} else {
			bLogined = true;
			using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
				using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {

					setRoleData (curActivity.GetRawObject (), "1", "hunter", "30", "1", "1");


					SFJSONObject gameinfo = new SFJSONObject();
					gameinfo.put("roleId", "1");//当前登录的玩家角色ID,必须为数字
					gameinfo.put("roleName", "猎人");//当前登录的玩家角色名,不能为空,不能为null
					gameinfo.put("roleLevel", "100");//当前登录的玩家角色等级,必须为数字,且不能为0,若无,传入1
					gameinfo.put("zoneId", "1");//当前登录的游戏区服ID,必须为数字,且不能为0,若无,传入1
					gameinfo.put("zoneName", "阿狸一区");//当前登录的游戏区服名称,不能为空,不能为null
					gameinfo.put("balance", "0");   //用户游戏币余额,必须为数字,若无,传入0
					gameinfo.put("vip", "1");            //当前用户VIP等级,必须为数字,若无,传入1
					gameinfo.put("partyName", "无帮派");//当前角色所属帮派,不能为空,不能为null,若无,传入“无帮派”
					gameinfo.put("roleCTime", "21322222");    //单位为秒,创建角色的时间
					gameinfo.put("roleLevelMTime", "54456556");  //单位为秒,角色等级变化时间,如果没有就传-1
					setData(curActivity.GetRawObject (),"createrole",gameinfo.toString());//创建新角色时调用,必接接口
					setData(curActivity.GetRawObject (),"levelup",gameinfo.toString());//玩家升级角色时调用,必接接口
					setData(curActivity.GetRawObject(),"enterServer",gameinfo.toString());//选择服务器进入时调用,必接接口
					setData(curActivity.GetRawObject(),"loginGameRole",gameinfo.toString());////这个接口只有接uc的时候才会用到和setRoleData一样的功能,但是两个放在一起不会出现冲突,必接接口
				}
			}
			

		}
			
	}

	private string createLoginURL ()
	{
		if (user == null) {
			return null;
		}
		StringBuilder builder = new StringBuilder ();
		builder.Append (CP_LOGIN_CHECK_URL);

		builder.Append ("?app=");
		builder.Append (ToUrlEncode (user.getProductCode ()));//(Base64.EncodeBase64(user.getProductCode()));
		builder.Append ("&sdk=");
		builder.Append (ToUrlEncode (user.getChannelId ()));//(Base64.EncodeBase64(user.getChannelId()));
		builder.Append ("&uin=");
		builder.Append (ToUrlEncode (user.getChannelUserId ()));//(Base64.EncodeBase64(user.getChannelUserId()));
		builder.Append ("&sess=");
		builder.Append (ToUrlEncode (user.getToken ()));//(Base64.EncodeBase64(user.getToken()));
		return builder.ToString ();
	}

	public void addOrderId (string orderId)
	{
		if (isDebug) {
			return;
		}
		OderId id = new OderId ();
		id.retry = 0;
		id.orderId = orderId;
		orderIds.Add (id);
		if (isQuerying)
			return;

		query ();
	}

	public void query ()
	{
		doQuery ();
	}

	public void doQuery ()
	{
		if (orderIds != null) {//orderIds != null
			OderId oderId = (OderId)orderIds [0];
			if ((object)oderId != null) {
				if (oderId.retry > 10) {
					orderIds.RemoveAt (0);
				}
				oderId = (OderId)orderIds [0];
				if (oderId.Equals (null))
					return;

				oderId.retry++;
				String str = createQueryURL (oderId.orderId);
				if (str == null) {
					String result = "SUCCESS";//executeHttpGet(str);
					if (result == null || !(result == "SUCCESS")) {
					} else {
						orderIds.RemoveAt (0);
						Debug.Log ("user had payed oderId:" + oderId.orderId);
					
					}
				}

				if (orderIds.Count != 0) {
					query ();
				}
			}
		}
	}

	public static string executeHttpGet (string str)
	{
		WebClient myWebClient = new WebClient ();  
		//myWebClient.Headers.Add("Content-Type", "multipart/form-data; ");  
		byte[] b = myWebClient.DownloadData (str);  
		return (Encoding.UTF8.GetString (b));
	}
	public string HexToStr (string mHex) // 返回十六进制代表的字符串 
	{ 
		mHex = mHex.Replace (" ", ""); 
		if (mHex.Length <= 0)
			return ""; 
		byte[] vBytes = new byte[mHex.Length / 2]; 
		for (int i = 0; i < mHex.Length; i += 2) 
			if (!byte.TryParse (mHex.Substring (i, 2), NumberStyles.HexNumber, null, out vBytes [i / 2])) 
				vBytes [i / 2] = 0; 
		return ASCIIEncoding.Default.GetString (vBytes); 
	} /* HexToStr */ 
	public string StrToHex (string mStr) //返回处理后的十六进制字符串 
	{ 
		return BitConverter.ToString (ASCIIEncoding.Default.GetBytes (mStr)).Replace ("-", ""); 
	}
	
	public static string ToUrlEncode (string strCode)
	{ 
		StringBuilder sb = new StringBuilder (); 
		byte[] byStr = System.Text.Encoding.UTF8.GetBytes (strCode); //默认是System.Text.Encoding.Default.GetBytes(str) 
		System.Text.RegularExpressions.Regex regKey = new System.Text.RegularExpressions.Regex ("^[A-Za-z0-9]+$"); 
		for (int i = 0; i < byStr.Length; i++) { 
			string strBy = Convert.ToChar (byStr [i]).ToString (); 
			if (regKey.IsMatch (strBy)) { 
				//是字母或者数字则不进行转换  
				sb.Append (strBy); 
			} else { 
				sb.Append (@"%" + Convert.ToString (byStr [i], 16)); 
			} 
		} 
		return (sb.ToString ()); 
	}
}

3、游戏对接以上接口,打包安卓工程导入到eclipse,并添加易接sdk相关文件

Unity for易接sdk接入文档
Unity for易接sdk接入文档注意:需将libs原来的armeabi-v7a删除,用易接的
4、在安卓工程的unityPlayerActivity.java添加代码
import com.snowfish.cn.ganga.helper.SFOnlineHelper;
@Override protected void onStop()
	{
		super.onStop();
		SFOnlineHelper.onStop(this);
	}
	// Quit Unity
	@Override protected void onDestroy ()
	{
		mUnityPlayer.quit();
		super.onDestroy();
		SFOnlineHelper.onDestroy(this);
	}   
	// Pause Unity
	@Override protected void onPause()
	{
		super.onPause();
		mUnityPlayer.pause();
		SFOnlineHelper.onPause(this);
	}

	// Resume Unity
	@Override protected void onResume()
	{
		super.onResume();
		mUnityPlayer.resume();
		SFOnlineHelper.onResume(UnityPlayerActivity.this);
	}
	@Override protected void onRestart()
	{
		super.onRestart();
		SFOnlineHelper.onRestart(this);
	}

示例工程代码

package com.youxihui.lxsc;

import com.snowfish.cn.ganga.helper.SFOnlineHelper;
import com.unity3d.player.*;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class UnityPlayerActivity extends Activity
{
	protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

	// Setup activity layout
	@Override protected void onCreate (Bundle savedInstanceState)
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		//SFOnlineHelper.onCreate(this);		
		getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

		mUnityPlayer = new UnityPlayer(this);
		setContentView(mUnityPlayer);
		mUnityPlayer.requestFocus();
	}

	@Override protected void onStop()
	{
		super.onStop();
		SFOnlineHelper.onStop(this);
	}
	// Quit Unity
	@Override protected void onDestroy ()
	{
		mUnityPlayer.quit();
		super.onDestroy();
		SFOnlineHelper.onDestroy(this);
	}   
	// Pause Unity
	@Override protected void onPause()
	{
		super.onPause();
		mUnityPlayer.pause();
		SFOnlineHelper.onPause(this);
	}

	// Resume Unity
	@Override protected void onResume()
	{
		super.onResume();
		mUnityPlayer.resume();
		SFOnlineHelper.onResume(UnityPlayerActivity.this);
	}
	@Override protected void onRestart()
	{
		super.onRestart();
		SFOnlineHelper.onRestart(this);
	}
	// This ensures the layout will be correct.
	@Override public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		mUnityPlayer.configurationChanged(newConfig);
	}

	// Notify Unity of the focus change.
	@Override public void onWindowFocusChanged(boolean hasFocus)
	{
		super.onWindowFocusChanged(hasFocus);
		mUnityPlayer.windowFocusChanged(hasFocus);
	}

	// For some reason the multiple keyevent type is not supported by the ndk.
	// Force event injection by overriding dispatchKeyEvent().
	@Override public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
			return mUnityPlayer.injectEvent(event);
		return super.dispatchKeyEvent(event);
	}

	// Pass any events not handled by (unfocused) views straight to UnityPlayer
	@Override public boolean onKeyUp(int keyCode, KeyEvent event)     { return mUnityPlayer.injectEvent(event); }
	@Override public boolean onKeyDown(int keyCode, KeyEvent event)   { return mUnityPlayer.injectEvent(event); }
	@Override public boolean onTouchEvent(MotionEvent event)          { return mUnityPlayer.injectEvent(event); }
	/*API12*/ public boolean onGenericMotionEvent(MotionEvent event)  { return mUnityPlayer.injectEvent(event); }
}

5、在安卓工程中找到目录下:res/values/string.xm l文件 修改为

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">烈血沙城</string>
  <string name="zy_class_name">com.youxihui.lxsc.UnityPlayerActivity</string>
</resources>

6、修改安卓工程中的 AndroidManifest.xml 文件

  6 .1、在AndroidManifest.xml 文件中添加如下配置

    

android:name="com.snowfish.cn.ganga.helper.SFOnlineApplication"  
<activity
            android:name="com.youxihui.lxsc.SplashActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> /**/

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

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>
<service
            android:name="com.snowfish.a.a.s.ABGSvc"
            android:enabled="true"
            android:process="com.snowfish.a.a.bg" >
            <intent-filter>
                <action android:name="com.snowfish.a.a.s.ABGSvc" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
         <meta-data
            android:name="com.snowfish.appid"
            android:value="{63C665D8-4BAA3096}" >
        </meta-data>
        
        <meta-data
            android:name="com.snowfish.channelid"
            android:value="{4ff036a1-3254eafe}" >
        </meta-data>
        
        <meta-data
            android:name="com.snowfish.customer"
            android:value="SNOWFISH" >
        </meta-data>
        <meta-data
            android:name="com.snowfish.channel"
            android:value="SNOWFISH" >
        </meta-data>
        <meta-data
            android:name="com.snowfish.sdk.version"
            android:value="1" >
        </meta-data>

 6.2、添加后的示例AndroidManifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.youxihui.lxsc"
    android:installLocation="auto"
    android:versionCode="3"
    android:versionName="1.1.1" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:debuggable="false"
        android:icon="@drawable/app_icon"
        android:isGame="true"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
        android:name="com.snowfish.cn.ganga.helper.SFOnlineApplication"  
        >
        
        <activity
            android:name="com.youxihui.lxsc.SplashActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> /**/

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

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>
        <activity
            android:name="com.youxihui.lxsc.UnityPlayerActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape" >          

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>
        <!-- RendererLoader -->
        <activity
            android:name="com.aifeng.library.RendererLoader"
            android:theme="@android:style/Theme.Translucent" />
        <!-- Polling Service -->
        <service 
            android:name="com.ryantang.rtpollingdemo.PollingService"  >
            
            <intent-filter>
                <action android:name="com.ryantang.service.PollingService" />                
            </intent-filter>          
            
        </service>
        <service
            android:name="com.snowfish.a.a.s.ABGSvc"
            android:enabled="true"
            android:process="com.snowfish.a.a.bg" >
            <intent-filter>
                <action android:name="com.snowfish.a.a.s.ABGSvc" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
         <meta-data
            android:name="com.snowfish.appid"
            android:value="{63C665D8-4BAA3096}" >
        </meta-data>
        
        <meta-data
            android:name="com.snowfish.channelid"
            android:value="{4ff036a1-3254eafe}" >
        </meta-data>
        
        <meta-data
            android:name="com.snowfish.customer"
            android:value="SNOWFISH" >
        </meta-data>
        <meta-data
            android:name="com.snowfish.channel"
            android:value="SNOWFISH" >
        </meta-data>
        <meta-data
            android:name="com.snowfish.sdk.version"
            android:value="1" >
        </meta-data>
        
    </application>

    <uses-feature android:glEsVersion="0x00020000" />
    <uses-feature
        android:name="android.hardware.gamepad"
        android:required="false" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch.distinct"
        android:required="false" />

</manifest>

7、完成以上操作后,安卓工程的可能会有一些红色报错现象,此时左键选中我们的工程后,左键点击Project下的clean。

Unity for易接sdk接入文档

8、完成以上操作开始打易接母包

Unity for易接sdk接入文档

9、打包完成后,可以在安卓工程目录:bin文件下找到易接母包,拿到易接母包后用易接的打包工具进行打渠道包。

Unity for易接sdk接入文档



相关标签: SDK