chicken_dy/Assets/Scripts/Core/NativeTool.cs

75 lines
1.9 KiB
C#
Raw Normal View History

2022-01-26 07:46:33 +00:00
using System.Runtime.CompilerServices;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using AOT;
public static class NativeTool
{
private static Action<bool> mShareResultDel;
public static void Shake(int pType, float pIntensity)//0 light,1 medium,2 heavy
{
#if UNITY_EDITOR
//Debug.Log($"Shake {pType} with intensity {pIntensity}");
#elif UNITY_IOS
mShake(pType, pIntensity);
#elif UNITY_ANDROID
mShake(pType * pType * 20 + (int)(pIntensity * (pType + 1) * 10));
#endif
}
public static void Share(string pTitle, string pUrl, Action<bool> pResultDel)
{
mShareResultDel = pResultDel;
#if UNITY_EDITOR
mShareResultDel?.Invoke(true);
#elif UNITY_IOS
mShare(pTitle, pUrl, onShareCallback);
#elif UNITY_ANDROID
mShare(pTitle, pUrl);
#endif
}
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void mShake(int pType, float pIntensity);
[DllImport("__Internal")]
private static extern void mShare(string pTitle, string pUrl, BoolResultDelegate pResultCallback);
internal delegate void BoolResultDelegate(bool pResult);
[MonoPInvokeCallback(typeof(BoolResultDelegate))]
private static void onShareCallback(bool pResult)
{
mShareResultDel?.Invoke(pResult);
}
#endif
#if UNITY_ANDROID
private static AndroidJavaClass javaObj = new AndroidJavaClass("com.toukagames.NativeTool");
private static AndroidJavaObject mInstance = null;
private static void mShake(long mMilliSeconds)
{
if (mInstance == null)
mInstance = javaObj.CallStatic<AndroidJavaObject>("GetInstance");
mInstance.Call("Shake", mMilliSeconds);
}
private static void mShare(string pTitle, string pUrl)
{
if (mInstance == null)
mInstance = javaObj.CallStatic<AndroidJavaObject>("GetInstance");
mInstance.Call("Share", pUrl);
mShareResultDel?.Invoke(true);
}
#endif
}