diff --git a/csharp/Facebook.Yoga/Native.cs b/csharp/Facebook.Yoga/Native.cs index ba225669..6e20fba0 100644 --- a/csharp/Facebook.Yoga/Native.cs +++ b/csharp/Facebook.Yoga/Native.cs @@ -28,7 +28,7 @@ namespace Facebook.Yoga internal class YGNodeHandle : SafeHandle { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ private GCHandle _managed; #endif @@ -46,27 +46,37 @@ namespace Facebook.Yoga protected override bool ReleaseHandle() { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ - if (_managed.IsAllocated) - { - _managed.Free(); - } +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ + ReleaseManaged(); #endif Native.YGNodeFree(this.handle); GC.KeepAlive(this); return true; } -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ public void SetContext(YogaNode node) { if (!_managed.IsAllocated) { +#if ENABLE_IL2CPP + // Weak causes 'GCHandle value belongs to a different domain' error + _managed = GCHandle.Alloc(node); +#else _managed = GCHandle.Alloc(node, GCHandleType.Weak); +#endif Native.YGNodeSetContext(this.handle, GCHandle.ToIntPtr(_managed)); } } + public void ReleaseManaged() + { + if (_managed.IsAllocated) + { + _managed.Free(); + } + } + public static YogaNode GetManaged(IntPtr ygNodePtr) { var node = @@ -83,10 +93,6 @@ namespace Facebook.Yoga internal class YGConfigHandle : SafeHandle { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ - private GCHandle _managed; -#endif - private YGConfigHandle() : base(IntPtr.Zero, true) { } @@ -101,12 +107,6 @@ namespace Facebook.Yoga protected override bool ReleaseHandle() { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ - if (_managed.IsAllocated) - { - _managed.Free(); - } -#endif Native.YGConfigFree(this.handle); GC.KeepAlive(this); return true; @@ -429,9 +429,9 @@ namespace Facebook.Yoga #endregion -#region IOS +#region AOT -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr YGNodeGetContext(IntPtr node); diff --git a/csharp/Facebook.Yoga/YogaLogger.cs b/csharp/Facebook.Yoga/YogaLogger.cs index 1d7f8e0a..d427abc0 100644 --- a/csharp/Facebook.Yoga/YogaLogger.cs +++ b/csharp/Facebook.Yoga/YogaLogger.cs @@ -13,6 +13,9 @@ using System.Runtime.InteropServices; #if __IOS__ using ObjCRuntime; #endif +#if ENABLE_IL2CPP +using AOT; +#endif namespace Facebook.Yoga { @@ -26,7 +29,7 @@ namespace Facebook.Yoga public static Func Logger = null; -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ [MonoPInvokeCallback(typeof(Func))] #endif public static void LoggerInternal(YogaLogLevel level, string message) diff --git a/csharp/Facebook.Yoga/YogaNode.cs b/csharp/Facebook.Yoga/YogaNode.cs index ef7f3bf9..cf4d7c78 100644 --- a/csharp/Facebook.Yoga/YogaNode.cs +++ b/csharp/Facebook.Yoga/YogaNode.cs @@ -12,12 +12,15 @@ using System.Collections; using System.Collections.Generic; using System.Text; -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ using System.Runtime.InteropServices; #endif #if __IOS__ using ObjCRuntime; #endif +#if ENABLE_IL2CPP +using AOT; +#endif namespace Facebook.Yoga { @@ -63,7 +66,7 @@ namespace Facebook.Yoga private MeasureFunction _measureFunction; private BaselineFunction _baselineFunction; private object _data; -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ private static YogaMeasureFunc _managedMeasure; private static YogaBaselineFunc _managedBaseline; #else @@ -106,6 +109,9 @@ namespace Facebook.Yoga _data = null; Native.YGNodeReset(_ygNode); +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ + _ygNode.ReleaseManaged(); +#endif } public bool IsDirty @@ -616,8 +622,8 @@ namespace Facebook.Yoga _measureFunction = measureFunction; if (measureFunction != null) { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ - _managedMeasure = MeasureInternalIOS; +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ + _managedMeasure = MeasureInternalAOT; _ygNode.SetContext(this); #else _managedMeasure = MeasureInternal; @@ -635,8 +641,8 @@ namespace Facebook.Yoga _baselineFunction = baselineFunction; if (baselineFunction != null) { -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ - _managedBaseline = BaselineInternalIOS; +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ + _managedBaseline = BaselineInternalAOT; _ygNode.SetContext(this); #else _managedBaseline = BaselineInternal; @@ -658,9 +664,9 @@ namespace Facebook.Yoga Native.YGNodeStyleGetDirection(_ygNode)); } -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ [MonoPInvokeCallback(typeof(YogaMeasureFunc))] - private static YogaSize MeasureInternalIOS( + private static YogaSize MeasureInternalAOT( IntPtr ygNodePtr, float width, YogaMeasureMode widthMode, @@ -687,9 +693,9 @@ namespace Facebook.Yoga return _measureFunction(this, width, widthMode, height, heightMode); } -#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ +#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__ [MonoPInvokeCallback(typeof(YogaBaselineFunc))] - private static float BaselineInternalIOS( + private static float BaselineInternalAOT( IntPtr ygNodePtr, float width, float height)