Unity3D Native Plugin

Unity3D Native Plugin

The native plugin should provide a simple C interface which the C# script then exposes to other user scripts.

iOS

Call iOS code from Unity

// .cs
using System.Runtime.InteropServices;

[DllImport ("__Internal")]
private static extern void _Foo();

public static void Foo()
{
  if (Application.platform != RuntimePlatform.OSXEditor) {
    _Foo();
  }
}
// .mm / .cpp

extern "C"
{
  void _Foo()
  {
    return;
  }
}


  • [Tag]

  • The iOS framework must be put in the Xcode generated project, not in Unity.
    And the framework’s functions must be wrapped in an extern "C" block.
    Then you will be able to use it in C# with DllImport.


  • All files with extensions .a, .m, .mm, .cpp under in the Assets/Plugins/iOS folder will be merged into the generated Xcode project automatically. However, merging is done by symlinking files from Assets/Plugins/iOS to the final destination, which might affect some workflows. The .h files are not included in the Xcode project tree, but they appears on the destination file system, thus alllow compilation of .m/.mm/.c/.cpp files.


    (Peng) After build to iOS project, the iOS source code would be under Unity-iPhone/Libraries/Plugins/iOS/.cpp (/Peng)


  • Sub folder are currently not supported.


Call Unity code from iOS

// .cpp

UnitySendMessage("GameObjectName", "MethodName", "Message2Send");

(Peng)
“GameObjectName” shows in Project Hierarchy View is the name of game object appears on scene.
“MethodName” is a public function name of the “GameObjectName”.
“Message2Send” is the “MethodName“‘s parameter.
(/Peng)


  • [Tag]

  • Managed-to-unmanaged calls are quite processer intensive on iOS. Try to avoid call multiple native methods per frame.


  • Xcode’s “Classes” folder is a good place to store your native code because it is not overwritten when project updated. (Peng) Check any source file has configured “Target Membership”. (/Peng)


  • (Peng) It seems UnitySendMessage only check the first “GameObjectName” from render tree (?) and its first “MethodName”. (/Peng)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值