微信分享(图片,链接,文字),向剪贴板粘贴内容,



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
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值