Cross-platform aspect Unity3d although already good enough, but for different platform still have some native interface need to use a platform own language to write,
This happens when you connect to the SDK.
Today simply write the interactive method of iOS and Android platform, Note: Need to debug the real machine, simulator what must be error.
First, create a new class two U3dPlugin.h u3dplugin.m in Xcode
The U3dPlugin.h code is as follows
// // U3dPlugin.h// unity-iphone//// Created by SHI In 14-11-14. // // #import <Foundation/Foundation.h>@interface u3dplugin:nsobject@end
The U3DPLUGIN.M code is as follows:
////u3dplugin.m//Unity-iphone////Created by Shi on 14-11-13.////#import "U3dPlugin.h"//=================== Importing the Unity SendMessage method to send a message to U3d must ===================#ifDefined (__cplusplus)extern "C"{#endif extern voidUnitysendmessage (Const Char*,Const Char*,Const Char*); externnsstring* _creatensstring (Const Char*string);#ifDefined (__cplusplus)}#endif@implementationU3dplugin//the method to invoke in unity,voidHellou3d (Const Char*ID,Const Char*name) {NSLog (@"xcode________%c", name); //U3d Method Three parameters are the object in the scene, the method name is returned, the method parameterUnitysendmessage ("Cube","Hellocallback","i ' m Xcode");}@end
Next, create a new project in unity, add a cube to the scene, or something else, and change the name of the cube.
Note: Cube is the name of the object that iOS sends a message to U3d to accept the message. The first parameter in the code above
Unitysendmessage ("Cube", "Hellocallback", "I-M Xcode");
After that, create a class HellowXcode.cs in your unity project to mount to the cube.
Write the code:
usingUnityengine;usingSystem.Collections;//the reference required to import the DLL library must beusingSystem.Runtime.InteropServices; Public classTestxcode:monobehaviour {#if unity_iphone&&! Unity_editor//DLL Import, the method that will be called in Hellou3doc[DllImport ("__internal")] Private Static extern voidHellou3d (stringIdstringname); //Use this for initialization voidStart () {helloxcode (); } /// <summary> ///hellos the Xcode. /// </summary> Public Static voidHelloxcode () {if(application.platform==runtimeplatform.iphoneplayer) {print ("u3d++++++++++ Helloxcode"); Hellou3d ("111","i ' m u3d"); } } /// <summary> ///Helloxcode callback function/// </summary> /// <param name= "name" >Name.</param> Public voidHellocallback (stringname) {Debug.Log ("Hellocallback"+name); }
#endif}
Import DLL Add Reference.
Using System.Runtime.InteropServices;
Under the assets directory, create a new directory Plugins/ios
Put the U3dPlugin.h u3dplugin.m you just created in the directory
Why put it here, the contents of this folder will be compiled into the libraries directory after the Xcode project is exported.
All right, open the build Settings interface.
Bundle identifier* Enter your ID to correspond to the name of the Provisioning profile file you requested.
SDK version Select Device SDK
Tick development build and script debugging to see more log information in the Xcode console for bug finding
Okay, click Build.
Normal operation can look at the log output in the Xcode console:
1.u3d++++++++++ Helloxcode
2.xcode________i ' m u3d
3.helloCallBacki ' m Xcode
Android or the above project Add File: Helloeclipse.cs
usingUnityengine;usingSystem.Collections;usingSystem; Public classhelloweclipse:monobehaviour{ #ifunity_android&&! Unity_editorPrivate StaticAndroidjavaclass Jc_u3dplugin;//class name Const stringSdk_java_class ="Com.hello.U3dPlugin";//Package Name
Public Static void Androidplugin (string funcName, paramsobject[] obj)
{Debug.Log (funcName); if(jc_u3dplugin==NULL) {Jc_u3dplugin=NewAndroidjavaclass (Sdk_java_class); } jc_u3dplugin.callstatic (FuncName, obj); } } Public Static voidhelloeclipses () {string[] Strarry=Newstring[2]; strarry[0] ="ID"; strarry[1] ="name";
Androidplugin("helloeclipses", Strarry);
public void Helloeclipsescallback (string name) {
Debug.Log ("helloeclipses" +name);
}
#endif
}
Export Eclipse Engineering
Then import Android project into Eclipse (project can be opened directly run)
Create Class Com.hello.U3dPlugin.java
Package Com.hello.U3dPlugin; Import // Importing a package that u3d send messages Import Android.util.Log; Public class U3dplugin { publicstatichelloeclipses
LOG.E ("U3dplugin", "Hello:" Name
Unityplayer.unitysendmessage ("Cube", Helloeclipsescallback, "I-M eclipse");
}
}
Run the project to see the log output in the LogCat.
Software version: U3d version 4.5.5f, xcode5.1, eclipse:android Developer Tools
This concludes.
Unity3d---ios--android communication