detect when running on Unity iOS
diff --git a/src/csharp/Grpc.Core/Internal/NativeExtension.cs b/src/csharp/Grpc.Core/Internal/NativeExtension.cs
index 1b99c97..333f677 100644
--- a/src/csharp/Grpc.Core/Internal/NativeExtension.cs
+++ b/src/csharp/Grpc.Core/Internal/NativeExtension.cs
@@ -114,8 +114,14 @@
/// </summary>
private static NativeMethods LoadNativeMethodsUnity()
{
- // TODO(jtattermusch): use NativeMethods.DllImportsFromStaticLib for iOS.
- return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
+ switch (PlatformApis.GetUnityRuntimePlatform())
+ {
+ case "IPhonePlayer":
+ return new NativeMethods(new NativeMethods.DllImportsFromStaticLib());
+ default:
+ // most other platforms load unity plugins as a shared library
+ return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
+ }
}
private static string GetAssemblyPath()
diff --git a/src/csharp/Grpc.Core/Internal/PlatformApis.cs b/src/csharp/Grpc.Core/Internal/PlatformApis.cs
index 9456b30..ce99ba4 100644
--- a/src/csharp/Grpc.Core/Internal/PlatformApis.cs
+++ b/src/csharp/Grpc.Core/Internal/PlatformApis.cs
@@ -23,6 +23,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
+using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
@@ -99,6 +100,18 @@
get { return IntPtr.Size == 8; }
}
+ /// <summary>
+ /// Returns <c>UnityEngine.Application.platform</c> as a string.
+ /// See https://docs.unity3d.com/ScriptReference/Application-platform.html for possible values.
+ /// Value is obtained via reflection to avoid compile-time dependency on Unity.
+ /// This method should only be called if <c>IsUnity</c> is <c>true</c>.
+ /// </summary>
+ public static string GetUnityRuntimePlatform()
+ {
+ GrpcPreconditions.CheckState(IsUnity, "Not running on Unity.");
+ return Type.GetType("UnityEngine.Application, UnityEngine").GetProperty("platform").GetValue(null).ToString();
+ }
+
[DllImport("libc")]
static extern int uname(IntPtr buf);