Version: Unity 7.0 Alpha (7000.0)
Language : English
Create a native plug-in for iOS
Create callbacks from native code

Call native plug-ins for iOS

Understand how to run code when your application is running on a device.

You can call iOS native plug-ins only when your application runs on a device or in the Simulator. Wrap native method calls in a C# layer that checks the current platform and returns placeholder values in the Unity Editor, so your scripts don’t fail. Store this C# file in your project’s Assets folder.

Use platform conditional compilation or Application.platform to branch your code.

Use conditional compilation

Platform-dependent compilation is faster than checking Application.platform because it’s evaluated at compile time, rather than runtime.

Use the following pattern:

void MyMethod()
{
#if UNITY_IOS && !UNITY_EDITOR
    CallNativeMethodImplementation();
#else
    CallEditorMethodImplementation();
#endif
}

Check the platform at runtime

Alternatively, check Application.platform at runtime and return placeholder values in the Editor:

void MyMethod()
{
    if (Application.platform != RuntimePlatform.OSXEditor)
    {
        return _GetLookupStatus();
    }
    else
    {
        return "Done";
    }
}

Additional resources

Create a native plug-in for iOS
Create callbacks from native code
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961