unity⼆次开发接第三⽅sdk
我看到过很多unity接sdk的⽅式,但是主要分为两种
1.以unity⼯程为主,导⼊sdk的jar包和修改配置l,这种⽅式优点就是不脱离unity,但是这样接sdk的资料太少,⽽且如果sdk过多不⽅便。
2.我今天主要说第⼆种⽅式,导出Android⼯程在esplise⾥进⾏接⼊并且进⾏⼆次开发,因为第⼆种⽅式需要对esplise和java有⼀定程度的了解,对我⽽⾔第⼆种⽅式更适合长期的投资。今天我就把我这三个⽉学习经验向⼤家分享⼀下。
因为今天这篇⽂章涉及的不仅仅是unity,⼤体的东西我就都说⼀下,如果有疑问的地⽅可以留⾔或者⾃⼰资料。
⼀.要做sdk接⼊⾸先要知道,unity是如何与Android通信的,这个知识点⽹上有很多,我在这⾥就举⼀个简单的例⼦(补充已经这个脚本是要挂到场景内部的)
这⾥⼀共两句代码,先解释第⼀句代码,这句代码⽆⾮是到esplise⾥stcommunicate下的PlatformContext类
第⼆句代码留个悬念,然后再unity内部留出测试接⼝
⼆.进⾏esplise内部接⼝的实现,也就是unity与Android通信的第⼆步。说到这⾥
呢,stcommunicate.PlatformContext就能⽤上了。
这就是我esplise的⽬录结构,多余的脚本会后续解释。第⼀步我们已经确定了Android内部的通信类PlatformContext,我们来实现它内部的通信接⼝。
通常来说这个PlatformContext我们都定义为抽象类,⽅便以后开发,我先给出抽象类,再给出实现的⼦类
stcommunicate;
import org.json.JSONObject;
import android.app.Activity;
t.Context;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
public abstract class PlatformContext {
protected static PlatformContext m_instance;
private Context m_ctx;
private Activity m_activity;
protected String m_target;
protected JSONObject m_config;
protected String m_tag;
protected String m_userId;
protected String m_nickname;
protected String m_channelUserInfo;
protected String m_channelSid;
protected String m_orderUrl;
protected String m_devilerUrl;
protected String m_imei;
protected String m_pkgid;
protected String m_packageName;
protected String m_version;
protected String m_serverId;
protected String m_serverName;
protected abstract void initContext(String config);
public abstract boolean destroyContext();
public abstract void login(int nParam);
public abstract void logout();
public abstract void switchAccount();
public abstract boolean isLogin();
protected abstract String getSession();
protected abstract String getGameId();
public static PlatformContext getInstance()
{
return m_instance;
}
protected void setGameObject(String name) {
m_target = name;
}
protected PlatformContext(String tag)
{
m_instance = this;
m_activity = UnityPlayer.currentActivity;
m_ctx = ApplicationContext();
m_pkgid = Class().getPackage().getName();
m_packageName = PackageName();
try {
m_version = PackageManager().getPackageInfo(m_packageName, 0).versionName; } catch (Exception e) {
m_version = "0";
}
}
protected PlatformContext(String target,String tag)
{
m_instance = this;
m_activity = UnityPlayer.currentActivity;
m_ctx = ApplicationContext();
m_pkgid = Class().getPackage().getName();
m_packageName = PackageName();
try {
m_version = PackageManager().getPackageInfo(m_packageName, 0).versionName; } catch (Exception e) {
m_version = "0";
}
}
}
stcommunicate;
import java.util.HashMap;
import android.util.Log;
t.Intent;
t.pm.ActivityInfo;
import android.os.Handler;
import android.os.Looper;
import com.unity3d.player.UnityPlayer;
public class TSPlatformContext extends PlatformContext {
int m_cpId;
int m_gameId;
boolean m_debug = false;
boolean m_blogin;
boolean m_enableGameAccount = false;
String m_gameAccountTitle;
String m_channalID;
String m_uid;
String m_name;
String Timestamp;
String m_token;
//String m_channelID;
String rsa = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArSULL4MRYI8wxLDLCWxQRXiEx61E7Dm9cbMHHY2l3lb3Axi0VEuLI/9Ee0zYF2/jXBfnE1m String roleId = null;
String roleName = null;
String roleLevel;
String zoneId ;
String zoneName = null;
String balance ;
String vip ;
sdk
String partyName = null;
public static int tempTypeGame = 1;
public TSPlatformContext(String target) {
super(target,"KZ");
m_blogin = false;
}
public static void Init()
{
if(m_instance==null) {
TSPlatformContext ucctx = new TSPlatformContext("");
ucctx.initPlatform();
}
return;
}
protected void initPlatform() {
// TODO Auto-generated method stub
Log.d("", "");
}
public void initContext(String config) {
Log.d("", "initContext config:"+config);
UnityPlayer.UnitySendMessage("PlatformHelper", "AtoUnity", "sucess");//这个其实就是Android向unity内的反向通信,"PlatformHelper"是unity场景内挂着AndroidT
}
@Override
public boolean destroyContext() {
// TODO Auto-generated method stub Log.d("", "");
return false;
}
@Override
public void login(int nParam) {
Log.d("", "");
}
@Override
public void logout() {
Log.d("", "");
}
@Override
public void switchAccount() {
Log.d("", "");
}
@Override
public boolean isLogin() {
// TODO Auto-generated method stub return false;
}
@Override
protected String getSession() {
// TODO Auto-generated method stub return "";
}
@Override
protected String getGameId() {
// TODO Auto-generated method stub return "";
}
}
unity内部的完成demo代码
using UnityEngine;
using System.Collections;
public class AndroidTestCommunication : MonoBehaviour
{
AndroidJavaObject m_sdkobj;
// Use this for initialization
AndroidJavaObject klass;
void Awake ()
{
#if NORMAL
#else
// Debug.LogError("AndroidJavaObject Init");
klass = new AndroidJavaClass("stcommunicate.PlatformContext");
m_sdkobj = klass.CallStatic<AndroidJavaObject>("getInstance");//这句就是通过⼀个简单的单例获取Android内部的PlatformContext类的实例对象
/// m_sdkobj.Call("setGameObject", name);
#endif
//Debug.LogError(klass);
//Debug.LogError(m_sdkobj);
}
//这⾥我添加了开机动画很简单的
IEnumerator Start()
{
Debug.LogError("Start");
if (Handheld.PlayFullScreenMovie("Heroes_Kingdom_ingame1280.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput))
yield return new WaitForEndOfFrame();
yield break;
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUILayout.Button("button1",GUILayout.Height(Screen.height*0.4f),GUILayout.Width(Screen.height*0.4f)))
{
Debug.LogError("onclick button1");
m_sdkobj.Call("initContext", "success");
}
}
public void AtoUnity(string succes)
{
Debug.LogError("come back:" + succes);
}
}
这⾥也看到了,我只是实现了⼀个初始化通信接⼝ initContext,其实如果接第三⽅sdk,登录⽀付接⼝实现的都是⼀个道理。
上⾯把通信说了,可是是怎么个⼆次开发的呢,其实很简单⾸先导出unity的esplise包,3个地⽅
导出后的⼯程⽂件夹,将assert下的bin⽂件和⽂件拷到新建的Android⼯程assert⽂件夹中。还有lib⽂件夹下的.so⽂件也同样拷到Android⼯程的lib⽂件夹下
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论