detect when running on Unity iOS

pull/14605/head
Jan Tattermusch 7 years ago
parent b988e396d2
commit d35471afd5
  1. 8
      src/csharp/Grpc.Core/Internal/NativeExtension.cs
  2. 13
      src/csharp/Grpc.Core/Internal/PlatformApis.cs

@ -114,9 +114,15 @@ namespace Grpc.Core.Internal
/// </summary>
private static NativeMethods LoadNativeMethodsUnity()
{
// TODO(jtattermusch): use NativeMethods.DllImportsFromStaticLib for iOS.
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()
{

@ -23,6 +23,7 @@ using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
@ -99,6 +100,18 @@ namespace Grpc.Core.Internal
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);

Loading…
Cancel
Save