【转载】cocs2dx中c++与c#互调
文章有参考http://www.cnblogs.com/zhxilin/archive/2013/03/20/2971331.html
下面以接入九幽数据统计插件为例
Step 1:如果是cocos2dx的话,每个版本不一样,比如3.6的话,已不支持wp8的,原因如下:
WP8: remove WP8 support because Angle don't support WP8 any more
以3.3的版本为例,直接打开proj.wp8-xaml目录下的工程文件;
如果不是cocos2dx,那就创建一个directx and xaml 工程。
Step 2:
新建 文件ICallback.h,例如内容可参考如下:
/*** Code begin ***/ #pragma once namespace SilverlightDirect3DInterop1Comp { [Windows::Foundation::Metadata::WebHostHidden] public interface class ICallback { public: virtual void Exec(Platform::String ^Command, Platform::String ^Param); int getOfferPoints(); void startLevel(int nLevel); void failLevel(int nLevel); void filishLevel(int nLevel); }; } /*** Code end ***/
Step 3:在Direct3DInterop.h文件中加入成员变量和成员函数
public: void SetXmalCallback(ICallback^ callback){ m_globalCallback = callback; }; private: ICallback^ m_globalCallback;
Step 4:
新建文件JiuYouCallback.cs,内容可参考如下:
using System; using System.ComponentModel; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Threading; using JYAnalyticsSdk; namespace SilverlightDirect3DInterop1Comp { public sealed class JiuYouCallback : ICallback { //public MainPage m_mainPage; static public int s_nOffserPoints; public void Exec(String Command, String Param) { //Execute some C# code, if you call UI stuff you will need to call this too //Deployment.Current.Dispatcher.BeginInvoke(() => { // //Lambda code //} } public int getOfferPoints() { MessageBox.Show("jiuyou", "jiuyou", MessageBoxButton.OK); return 10; } public void startLevel(int nLevel) { JYAnalytics.TrackEvent("startlevel", Convert.ToString(nLevel)); } public void failLevel(int nLevel) { JYAnalytics.TrackEvent("faillevel", Convert.ToString(nLevel)); } public void filishLevel(int nLevel) { JYAnalytics.TrackEvent("filishlevel", Convert.ToString(nLevel)); } } }
Step 5
在 MainPage.xaml.cs中的找到
private void DrawingSurface_Loaded(object sender, RoutedEventArgs e)
在里面加入
JiuYouCallback sCallBack = new JiuYouCallback(); m_d3dInterop.SetXmalCallback(sCallBack);//这句是c#调用c++的方法
Step 6
前面5步已经把c++ c#互调的框架搭好了。下面测下是否成功
在Direct3DInterop.cpp文件里添加如下
void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args) { int nPoints = m_globalCallback->getOfferPoints(); }
Step 7,运行程序,记得把弄成如下图这样测试
Step 8
接入九幽统计插件,参考文档《九幽数据统计插件(WP8)使用说明文档》
Ps:
注意命名空间要一致,否则可运行不起来哦
比如cocos2d的,c#跟c++这边都有cocos2d
原文链接:http://blog.csdn.net/kuovane/article/details/45723645