using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameAgencyView : MonoBehaviour
{
public Text text;
public Button button;
#region 判斷平臺
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
#elif UNITY_ANDROID
private AndroidJavaObject jo;
#elif UNITY_IPHONE
private static extern void copyTextToClipboard(string text);
#endif
#endregion
// [Inject]
// public CLWeChatInvite clWeChatInviteSignal { get; set; }
// Use this for initialization
void Awake()
{
button.onClick.AddListener(OnCopyBtnClick);
}
void Start()
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("onWechatInit", AppReleseKey.appId, AppReleseKey.appSecret);
#elif UNITY_IPHONE
onWechatInit(AppReleseKey.appId, AppReleseKey.appSecret);
#endif
}
public void OnCopyBtnClick()
{
//string WeChatId = text.text;
//CrossPlatformInterface.instance.CopyTextToClipboard(WeChatId);
//clWeChatInviteSignal.Dispatch();
#if UNITY_EDITOR
TextEditor te = new TextEditor();
te.text = text.text;
te.SelectAll();
te.Copy();
Debug.Log(te.text);
#elif UNITY_ANDROID
jo.Call("OnCopyBtnClick", text);
#elif UNITY_IPHONE
copyTextToClipboard(text);
#endif
}
}
之後,獲得了一個更爲全面的腳本,進行微信將要進行的分享和向剪貼板中添加文字
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
public enum ShareType
{
SceneSession = 0,
SceneTimeline = 1
}
public class CrossPlatformInterface : MonoBehaviour
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
#elif UNITY_ANDROID
private AndroidJavaObject jo;
#elif UNITY_IPHONE
[DllImport("__Internal")]
private static extern void onWechatInit(string appId, string appSecret);
[DllImport("__Internal")]
private static extern void onWechatLogin();
[DllImport("__Internal")]
private static extern void reqShareText(string text, int type);
[DllImport("__Internal")]
private static extern void reqShareURL(string url, string title, string desc, int type);
[DllImport("__Internal")]
private static extern void reqShareImg(string imageName, int type);
[DllImport("__Internal")]
private static extern void reqPay(string productId);
[DllImport("__Internal")]
private static extern void copyTextToClipboard(string text);
[DllImport("__Internal")]
private static extern bool isWXAppInstalled();
#endif
public static CrossPlatformInterface instance;
private void Awake()
{
instance = this;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
// Use this for initialization
void Start()
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("onWechatInit", AppReleseKey.appId, AppReleseKey.appSecret);
#elif UNITY_IPHONE
onWechatInit(AppReleseKey.appId, AppReleseKey.appSecret);
#endif
}
private void Update()
{
}
public void OnWechatLogin()
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
jo.Call("onWechatLogin");
#elif UNITY_IPHONE
onWechatLogin();
#endif
}
public void WechatShareURL(string url, string title, string desc, ShareType type)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
jo.Call("reqShareURL", url, title, desc, (int)type);
#elif UNITY_IPHONE
reqShareURL(url, title, desc, (int)type);
#endif
}
public void WechatShareText(string text, ShareType type)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
jo.Call("reqShareText", text, (int)type);
#elif UNITY_IPHONE
reqShareText(text, (int)type);
#endif
}
public void WechatShareImg(string filePath, ShareType type)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
jo.Call("reqShareImg", filePath, (int)type);
#elif UNITY_IPHONE
reqShareImg(Path.GetFileName(filePath), (int)type);
#endif
}
public void Purchase(string param)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
jo.Call("onIAppPay", AppReleseKey.appPayId, param);
#elif UNITY_IPHONE
reqPay(param);
#else
#endif
}
//向剪貼板中添加文本
public void CopyTextToClipboard(string text)
{
#if UNITY_EDITOR
TextEditor te = new TextEditor();
te.text = text;
te.SelectAll();
te.Copy();
#elif UNITY_ANDROID
jo.Call("copyTextToClipboard", text);
#elif UNITY_IPHONE
copyTextToClipboard(text);
#endif
// UILogin.instance.ShowTips("複製成功");
}
public bool IsWXAppInstalled()
{
#if UNITY_EDITOR
return false;
#elif UNITY_IPHONE
return isWXAppInstalled();
#else
return true;
#endif
}
}