1.9 unity接shareSDK實(shí)現(xiàn)截屏分享功能(shareSDK+unity+隱藏部分ui)

最近公司需要做分享功能,截圖時(shí),要顯示一些UI,也要隱藏一些UI。
話不多少,貼代碼

using System;
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using cn.sharesdk.unity3d;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using LuaFramework;
using UnityEditor;

public class MoleShareManager : Manager
{
    private ShareSDK ssdk;
    void Start()
    {
        ssdk = gameObject.AddComponent<ShareSDK>();
        ssdk.shareHandler = OnShareResultHandler;//注冊回調(diào)函數(shù)
    }
    public void OpenSomeChange(int[] set_hide_platforms, string str,System.Action screen_shot_start, System.Action screen_shot_end)
    {
        if (set_hide_platforms == null)
        {
            return;
        }
        StartCoroutine(OnScreenShot(screen_shot_start, screen_shot_end));//截屏
        ShareContent content = new ShareContent();//分享前,創(chuàng)建對象
        content.SetText(str);//分享文字
        content.SetImagePath(Application.persistentDataPath+"/ScreenShot.png");//分享圖片
        content.SetShareType(ContentType.Image);//設(shè)定分享內(nèi)容的主要類型


#if UNITY_IOS
        PlatformType[] platforms ={ PlatformType.SinaWeibo, PlatformType.WeChat };
        ssdk.ShowPlatformList(platforms, content, 100, 100);

#endif
#if UNITY_ANDROID
        int len = set_hide_platforms.Length;
        string[] p_str_array = new string[len];
        for (int i = 0; i < set_hide_platforms.Length; i++)
        {
            p_str_array[i] = Convert.ToString(set_hide_platforms[i]);
            //Debug.Log("OpenSomeChange:" + set_hide_platforms[i]);
        }
        content.SetHidePlatforms(p_str_array);
        ssdk.ShowPlatformList(null, content, 100, 100);

#endif
    }

    IEnumerator OnScreenShot(System.Action screen_shot_start, System.Action screen_shot_end)
    {

        screen_shot_start();
        
        //命名圖片
        string file_name = "ScreenShot.png";
        //截屏
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        yield return new WaitForEndOfFrame();
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        texture.Apply();
        byte[] bytes = texture.EncodeToPNG();
        //存儲路徑
        string destination = Application.persistentDataPath;//"/sdcard/DCIM/screensshots";
        //若沒路徑 創(chuàng)建
        if (!Directory.Exists((destination)))
        {
            Directory.CreateDirectory(destination);
        }
        //保存路徑
        string path_save = destination + "/" + file_name;
        //寫入圖片
        File.WriteAllBytes(path_save, bytes);
        screen_shot_end();
        //Debug.Log("11111111111");
    }

    public void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        //并非所有平臺都會報(bào)告正確的狀態(tài)
        if (state == ResponseState.Success)
        {
            Debug.Log("分享成功");
        }
        else if (state == ResponseState.Fail)
        {

            Debug.Log("分享失敗");
        }
        else if (state == ResponseState.Cancel)
        {

            Debug.Log("分享操作被取消");
        }
    }

}

兩個(gè)Action是在lua里面寫的,隱藏圖片的方法。 你也可以在協(xié)程里面寫,我這樣寫只是為了以后熱更方便點(diǎn)

--lua代碼
--截屏前隱藏/顯示ui
local function shot_start()
    --print("shot_startshot_startshot_startshot_startshot_startshot_start")
    panel.drawcardafter_animator.enabled = false
    panel.button_close:SetActive(false)
    panel.button_Share:SetActive(false)
    panel.ima_logo:SetActive(true)
end
--截屏后隱藏/顯示ui
local function shot_end()
    --print("shot_endshot_endshot_endshot_endshot_endshot_endshot_end")
    panel.drawcardafter_animator.enabled = true
    panel.button_close:SetActive(true)
    panel.button_Share:SetActive(true)
    panel.ima_logo:SetActive(false)
end

轉(zhuǎn)載請注明出處,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容