https://github.com/toanlcgift/xamarin-cocoscreator-samples
- CocosCreator v2.x
- Visual Studio 2017+
- nuget version >= 2.2.7 supports Xamarin.Forms
- template: default
- platform: android/iOS
- buildPath: ./build
public class MainActivity : Cocos2dxActivity
protected override void OnCreate(Bundle savedInstanceState)
{
if (!IsTaskRoot)
{
return;
}
Java.Lang.JavaSystem.LoadLibrary("cocos2djs");
this.Setxxteakey("123456789");
base.OnCreate(savedInstanceState);
app = this;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
}
NOTE: Make sure the xxteakey is matched with your cocos creator's build setting
public override Cocos2dxGLSurfaceView OnCreateView()
{
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
glSurfaceView.SetEGLConfigChooser(8, 8, 8, 8, 16, 0);
return glSurfaceView;
}
Copy resources in Cocos Creator's build folder to Xamarin.Android Assets folder (except frameworks & js backups folder)
- jsb-adapter
- res
- src
- subpackages
- main.js
- project.json
- ...
<ItemGroup>
<AndroidAsset Include="Assets\**" />
</ItemGroup>
public partial class AppDelegate : AppController
public override bool DidFinishLaunchingWithOptions(UIApplication app, NSDictionary options)
{
options = new NSDictionary(new NSString("xxtea_key"), new NSString("123456789"));
return base.DidFinishLaunchingWithOptions(app, options);
}
NOTE: Make sure the xxteakey is matched with your cocos creator's build setting
Copy resources in Cocos Creator's build folder to Xamarin.iOS Resources folder (except frameworks & js backups folder)
- jsb-adapter
- res
- src
- subpackages
- main.js
- project.json
- ...
<BundleResource Include="Resources\**">
<Optimize>False</Optimize>
</BundleResource>
NOTE: This package does not support iOS emulator
CocosViewRenderer.Init(this, savedInstanceState, [js_encrypted_key]);
Copy resources in Cocos Creator's build folder to Xamarin.iOS Resources folder (except frameworks & js backups folder)
- jsb-adapter
- res
- src
- subpackages
- main.js
- project.json
- ...
<ItemGroup>
<AndroidAsset Include="Assets\**" />
</ItemGroup>
CocosViewRenderer.Init([js_encrypted_key]);
Copy resources in Cocos Creator's build folder to Xamarin.iOS Resources folder (except frameworks & js backups folder)
- jsb-adapter
- res
- src
- subpackages
- main.js
- project.json
- ...
<BundleResource Include="Resources\**">
<Optimize>False</Optimize>
</BundleResource>
In your xaml page:
xmlns:cocosforms="clr-namespace:CocosCreator.Forms;assembly=CocosCreator.Forms"
<cocosforms:CocosView NativeCallCommand="{Binding NativeCallCommand}" />
please refer articles for android & ios first
In cocos creator javascript:
if (cc.sys.os === cc.sys.OS_ANDROID) {
var result = jsb.reflection.callStaticMethod("org/cocos2dx/lib/Cocos2dxActivity", // Cocos2dxForms instead if using nuget 2.2.7+
"paramFromJSStaticString", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
"cocos2d-js",
"Native Call Test");
cc.log(result);
}
else if (cc.sys.os === cc.sys.OS_IOS) {
var ret = jsb.reflection.callStaticMethod("NativeOcClass",
"callNativeWithReturnString:andContent:",
"cocos2d-js",
"Native Call Test");
cc.log(ret);
}
To get the value from js:
- In android, override following methods in MainActivity:
public override void ParamFromJSVoid(string title, string message)
{
System.Diagnostics.Debug.WriteLine($"title: {title}, message: {message}");
}
public override int ParamFromJSInt(string title, string message)
{
return base.ParamFromJSInt(title, message);
}
public override string ParamFromJSString(string title, string message)
{
return base.ParamFromJSString(title, message);
}
- In ios, override following methods in AppDelegate:
public override bool CallNativeWithReturnBool(string title, string content)
{
System.Diagnostics.Debug.WriteLine($"CallNativeWithReturnBool: title: { title} & content: {content}");
return base.CallNativeWithReturnBool(title, content);
}
public override string CallNativeWithReturnString(string title, string content)
{
System.Diagnostics.Debug.WriteLine($"CallNativeWithReturnString: title: { title} & content: {content}");
return base.CallNativeWithReturnString(title, content);
}
- Xamarin.Forms:
Please implement MVVM and binding to NativeCallCommand in ViewModel
public ICommand NativeCallCommand { get; set; } = new Command(OnExecuteNativeCallCommand);
private void OnExecuteNativeCallCommand(object obj)
{
CocosCreator.Forms.JSBrigde.EvaluateScript("cc.TestNativeCallJS()");
}
- Xamarin.Android:
app.RunOnGLThread(new Runnable(() =>
{
Cocos2dxJavascriptJavaBridge.EvalString("cc.TestNativeCallJS()");
}));
- Xamarin.iOS:
AppController.EvalScript("cc.TestNativeCallJS()");
- Xamarin.Forms:
CocosCreator.Forms.JSBrigde.EvaluateScript("cc.TestNativeCallJS()");
Of course, it's fully compatible with Xamarin!
No, don't need anymore, we have Xamarin Community instead!