最近公司需要做分享功能,截图时,要显示一些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;//注册回调函数
}
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();//分享前,创建对象
content.SetText(str);//分享文字
content.SetImagePath(Application.persistentDataPath+"/ScreenShot.png");//分享图片
content.SetShareType(ContentType.Image);//设定分享内容的主要类型
#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";
//若没路径 创建
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)
{
//并非所有平台都会报告正确的状态
if (state == ResponseState.Success)
{
Debug.Log("分享成功");
}
else if (state == ResponseState.Fail)
{
Debug.Log("分享失败");
}
else if (state == ResponseState.Cancel)
{
Debug.Log("分享操作被取消");
}
}
}
两个Action是在lua里面写的,隐藏图片的方法。 你也可以在协程里面写,我这样写只是为了以后热更方便点
--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
转载请注明出处,谢谢!
网友评论