96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections;
|
|
|
|
public class ScreenShotManager : SingletonMonoBehaviour<ScreenShotManager> {
|
|
|
|
[SerializeField]
|
|
private Texture2D defaultScreenShotTexture = default;
|
|
|
|
public string shareImageFileName = "ScreenShot.png";
|
|
public string ImagePath {
|
|
get{ return string.Format("{0}/{1}", Application.persistentDataPath, shareImageFileName); }
|
|
}
|
|
|
|
#if !UNITY_EDITOR
|
|
public bool ExistsScreenShot{
|
|
get{ return System.IO.File.Exists(ImagePath); }
|
|
}
|
|
#else
|
|
private Texture2D texture;
|
|
public bool ExistsScreenShot{
|
|
get{ return texture != null; }
|
|
}
|
|
#endif
|
|
|
|
void Awake(){
|
|
if(CheckInstance()) return ;
|
|
}
|
|
|
|
public void CaptureScreenShot(Action justBeforeAction, Action justAfterAction, Action callback){
|
|
StartCoroutine(_CaptureScreenShot(justBeforeAction, justAfterAction, callback));
|
|
}
|
|
private IEnumerator _CaptureScreenShot(Action justBeforeAction, Action justAfterAction, Action callback){
|
|
#if !UNITY_EDITOR
|
|
string imagePath = ImagePath;
|
|
if(ExistsScreenShot){
|
|
System.IO.File.Delete(imagePath);
|
|
}
|
|
yield return null;
|
|
justBeforeAction();
|
|
yield return new WaitForEndOfFrame();
|
|
ScreenCapture.CaptureScreenshot(shareImageFileName);
|
|
yield return null;
|
|
justAfterAction();
|
|
|
|
yield return new WaitUntil(() => { return System.IO.File.Exists(imagePath); });
|
|
#else
|
|
yield return null;
|
|
justBeforeAction();
|
|
yield return new WaitForEndOfFrame();
|
|
texture = new Texture2D(Screen.width, Screen.height);
|
|
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
|
texture.Apply();
|
|
yield return null;
|
|
justAfterAction();
|
|
#endif
|
|
callback();
|
|
}
|
|
|
|
public Texture2D GetTexture(){
|
|
try{
|
|
#if !UNITY_EDITOR
|
|
byte[] image = System.IO.File.ReadAllBytes(ImagePath);
|
|
Texture2D texture = new Texture2D(0, 0);
|
|
texture.LoadImage(image);
|
|
#endif
|
|
if(texture.isBogus()){
|
|
return defaultScreenShotTexture;
|
|
}else{
|
|
return texture;
|
|
}
|
|
}catch(Exception){
|
|
return defaultScreenShotTexture;
|
|
}
|
|
}
|
|
|
|
public Sprite GetSprite(){
|
|
return GetTexture().ToSprite();
|
|
}
|
|
|
|
public void ShareTwitter(string message, Action<bool> callback){
|
|
#if UNITY_IOS
|
|
ShareManager.Instance.ShareTwitter(message, GetTexture(), callback);
|
|
#else
|
|
ShareManager.Instance.ShareTwitter(message, ImagePath, callback);
|
|
#endif
|
|
}
|
|
public void ShareFacebook(string message, Action<bool> callback){
|
|
#if UNITY_IOS
|
|
ShareManager.Instance.ShareFacebook(message, GetTexture(), callback);
|
|
#else
|
|
ShareManager.Instance.ShareFacebook(message, ImagePath, callback);
|
|
#endif
|
|
}
|
|
}
|