打通unity调用cpp
This commit is contained in:
parent
342af74b54
commit
45f833467d
@ -25,7 +25,7 @@
|
||||
|
||||
#include "AppDelegate.h"
|
||||
|
||||
//#include "cocos2d.h"
|
||||
#include "cocos2d.h"
|
||||
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_module_register.hpp"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
||||
@ -66,10 +66,9 @@ bool AppDelegate::applicationDidFinishLaunching()
|
||||
|
||||
se::AutoHandleScope hs;
|
||||
jsb_run_script("js/jsb-adapter/jsb-builtin.js");
|
||||
jsb_run_script("js/jcwallet.js");
|
||||
jsb_run_script("js/main.js");
|
||||
se::Value rval;
|
||||
jsb_run_code("JSON.stringify(wallet[0])", &rval);
|
||||
|
||||
|
||||
se->addAfterCleanupHook([]() {
|
||||
JSBClassType::destroy();
|
||||
});
|
||||
|
48
Classes/JcWallet.cpp
Normal file
48
Classes/JcWallet.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include "JcWallet.h"
|
||||
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
||||
#include "scripting/js-bindings/event/EventDispatcher.h"
|
||||
#include "platform/CCApplication.h"
|
||||
|
||||
cocos2d::Application *cocos_android_app_init(int width, int height);
|
||||
|
||||
using namespace cocos2d;
|
||||
NS_CC_BEGIN
|
||||
|
||||
cocos2d::Application *g_app = nullptr;
|
||||
JcWallet *JcWallet::_instance = nullptr;
|
||||
|
||||
JcWallet::JcWallet() {
|
||||
JcWallet::_instance = this;
|
||||
}
|
||||
|
||||
void JcWallet::initEnv() {
|
||||
g_app = cocos_android_app_init(1, 1);
|
||||
se::ScriptEngine *se = se::ScriptEngine::getInstance();
|
||||
EventDispatcher::init();
|
||||
g_app->start();
|
||||
}
|
||||
|
||||
JcWallet::~JcWallet() {
|
||||
EventDispatcher::destroy();
|
||||
se::ScriptEngine::destroyInstance();
|
||||
JcWallet::_instance = nullptr;
|
||||
}
|
||||
|
||||
char* JcWallet::initWallet() {
|
||||
se::Value rval;
|
||||
jsb_run_code("jc.wallet.currentAccount().address", &rval);
|
||||
char* result = const_cast<char*>(rval.toString().c_str());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
char* initWallet() {
|
||||
JcWallet::getInstance()->initEnv();
|
||||
return JcWallet::getInstance()->initWallet();
|
||||
}
|
||||
}
|
||||
|
||||
NS_CC_END
|
15
Classes/JcWallet.h
Normal file
15
Classes/JcWallet.h
Normal file
@ -0,0 +1,15 @@
|
||||
#include "cocos2d.h"
|
||||
#include "base/ccMacros.h"
|
||||
NS_CC_BEGIN
|
||||
class CC_DLL JcWallet {
|
||||
public:
|
||||
char* initWallet();
|
||||
void initEnv();
|
||||
JcWallet();
|
||||
virtual ~JcWallet();
|
||||
static JcWallet* getInstance() { return _instance; }
|
||||
private:
|
||||
static JcWallet* _instance;
|
||||
};
|
||||
|
||||
NS_CC_END
|
@ -13,6 +13,7 @@ endif
|
||||
LOCAL_SRC_FILES := hellojavascript/main.cpp \
|
||||
../../Classes/AppDelegate.cpp \
|
||||
../../Classes/jsb_module_register.cpp \
|
||||
../../Classes/JcWallet.cpp \
|
||||
|
||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
|
||||
|
||||
|
@ -40,7 +40,7 @@ void cocos_jni_env_init(JNIEnv *env)
|
||||
}
|
||||
|
||||
//called when onSurfaceCreated()
|
||||
Application *cocos_android_app_init(JNIEnv *env, int width, int height)
|
||||
Application *cocos_android_app_init(int width, int height)
|
||||
{
|
||||
LOGD("cocos_android_app_init");
|
||||
auto app = new AppDelegate(width, height);
|
||||
|
@ -17,6 +17,7 @@ import org.cocos2dx.lib.CocosJSHelper;
|
||||
|
||||
|
||||
public class MainActivity extends Activity implements Cocos2dxHelper.Cocos2dxHelperListener {
|
||||
public static MainActivity app;
|
||||
protected UnityPlayer mUnityPlayer;
|
||||
|
||||
protected String updateUnityCommandLineArguments(String cmdLine)
|
||||
@ -37,6 +38,7 @@ public class MainActivity extends Activity implements Cocos2dxHelper.Cocos2dxHel
|
||||
|
||||
// setContentView(R.layout.activity_main);
|
||||
onLoadNativeLibraries();
|
||||
app = this;
|
||||
Cocos2dxHelper.init(this);
|
||||
CocosJSHelper.initJSEnv(getApplicationContext());
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package com.jc.jcfw;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.cocos2dx.lib.CocosJSHelper;
|
||||
|
||||
public class JcSDK {
|
||||
private static final String TAG = JcSDK.class.getSimpleName();
|
||||
private static UnityCallback commonCB;
|
||||
@ -9,9 +11,10 @@ public class JcSDK {
|
||||
Log.i(TAG, "call init common callback from unity");
|
||||
commonCB = callBack;
|
||||
}
|
||||
|
||||
// 不使用该方法, 直接由unity调用cpp方法
|
||||
public static void initWallet(String password) {
|
||||
Log.i(TAG, "call init wallet from unity with password: " + password);
|
||||
CocosJSHelper.initWallet(password);
|
||||
commonCB.stringCallback("wallet init success");
|
||||
}
|
||||
}
|
||||
|
61
js/jcwallet.js
Normal file
61
js/jcwallet.js
Normal file
File diff suppressed because one or more lines are too long
42858
js/jsb-adapter/web3.min.js
vendored
42858
js/jsb-adapter/web3.min.js
vendored
File diff suppressed because one or more lines are too long
20
js/main.js
20
js/main.js
@ -1,10 +1,12 @@
|
||||
require('js/jsb-adapter/web3.min.js');
|
||||
console.log('hi tiny cocos')
|
||||
var web3 = new Web3('https://rpc-testnet.kcc.network')
|
||||
let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
||||
web3.eth.accounts.wallet.add(key);
|
||||
web3.eth.accounts.wallet.save('111111')
|
||||
window.wallet = web3.eth.accounts.wallet.load('111111')
|
||||
console.log(web3.eth.accounts.wallet[0].address);
|
||||
console.log(web3.eth.accounts.wallet[0].privateKey);
|
||||
console.log('end of main.js')
|
||||
var wallet = new jcwallet.default();
|
||||
var account = jc.wallet.currentAccount();
|
||||
console.log('[WALLET]' + account.address);
|
||||
// var web3 = new Web3('https://rpc-testnet.kcc.network')
|
||||
// let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
||||
// web3.eth.accounts.wallet.add(key);
|
||||
// web3.eth.accounts.wallet.save('111111')
|
||||
// window.wallet = web3.eth.accounts.wallet.load('111111')
|
||||
// console.log(web3.eth.accounts.wallet[0].address);
|
||||
// console.log(web3.eth.accounts.wallet[0].privateKey);
|
||||
// console.log('end of main.js')
|
||||
|
Loading…
x
Reference in New Issue
Block a user