Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IosNativeObject.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if UNITY_IOS
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. namespace UnityEngine.Advertisements.Platform.iOS
  7. {
  8. internal abstract class IosNativeObject : IDisposable
  9. {
  10. protected static ConcurrentDictionary<IntPtr, IosNativeObject> s_Objects = new ConcurrentDictionary<IntPtr, IosNativeObject>();
  11. private IntPtr m_NativePtr;
  12. public IntPtr NativePtr
  13. {
  14. get => m_NativePtr;
  15. protected set
  16. {
  17. if (m_NativePtr == value) return;
  18. if (m_NativePtr != IntPtr.Zero)
  19. {
  20. s_Objects.TryRemove(m_NativePtr, out _);
  21. }
  22. m_NativePtr = value;
  23. if (m_NativePtr != IntPtr.Zero)
  24. {
  25. s_Objects.TryAdd(m_NativePtr, this);
  26. }
  27. }
  28. }
  29. protected static T Get<T>(IntPtr ptr) where T : IosNativeObject
  30. {
  31. return s_Objects.TryGetValue(ptr, out var obj) ? (T)obj : null;
  32. }
  33. public virtual void Dispose()
  34. {
  35. if (NativePtr == IntPtr.Zero) return;
  36. BridgeTransfer(NativePtr);
  37. NativePtr = IntPtr.Zero;
  38. }
  39. public bool CheckDisposedAndLogError(string message)
  40. {
  41. if (NativePtr != IntPtr.Zero) return false;
  42. Debug.LogErrorFormat("UnityAds SDK: {0}: Instance of type {1} is disposed. Please create a new instance in order to call any method.", message, GetType().FullName);
  43. return true;
  44. }
  45. [DllImport("__Internal", EntryPoint = "UnityAdsBridgeTransfer")]
  46. private static extern void BridgeTransfer(IntPtr x);
  47. }
  48. }
  49. #endif