unity 3d与ios代码相互调用

来源:互联网 发布:查询数据库所有表 编辑:程序博客网 时间:2024/06/06 02:57

转自 http://blog.csdn.net/lanergaming/article/details/7566928

看unity3d两天了,一直想知道如何能让unity中添加ios的ui,苦于不知道两者直接如何相互调用,

今天查文档时发现其实不是很难。unity给出了相关的例子:Bonjour Browser Sample

1,Building an Application with a Native Plugin for iOS

  1. Define your extern method in the C# file as follows: 
    [DllImport ("__Internal")]private static extern float FooPluginFunction ();
  2. Set the editor to the iOS build target
  3. Add your native code source files to the generated XCode project's "Classes" folder (this folder is not overwritten when the project is updated, but don't forget to backup your native code).

If you are using C++ (.cpp) or Objective-C (.mm) to implement the plugin you must ensure the functions are declared with C linkage to avoid name mangling issues.

extern "C" {  float FooPluginFunction ();} 

Using Your Plugin from C#

iOS native plugins can be called only when deployed on the actual device, so it is recommended to wrap all native code methods with an additional C# code layer. This code should check Application.platform and call native methods only when the app is running on the device; dummy values can be returned when the app runs in the Editor. See the Bonjour browser sample application for an example.


2,Calling C# / JavaScript back from native code
Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:

UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");

This function has three parameters : the name of the target GameObject, the script method to call on that object and the message string to pass to the called method.

Known limitations:

  1. Only script methods that correspond to the following signature can be called from native code: function MethodName(message:string)
  2. Calls to UnitySendMessage are asynchronous and have a delay of one frame.
原创粉丝点击