最近公司需要做分享功能,截圖時(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)載請注明出處,謝謝!