我们的产品需要做截屏发微博功能,尝试了和多次用ios的原生代码实现发现出来都是白屏,无奈之下只有使用unity提供的代码截屏保存,然后再到ios, android上读取图片发布。
代码如下:
public const string STATISTIC_SCREENSHOT = "statistic.png";
IEnumerator ShareScreenShotToWeibo(int platform){
//注意,在移动设备上不需要添加默认路径,但是在pc上调试的时候需要,否则就会截屏保存到工程目录下
//Application.CaptureScreenshot(UnityFileSave.GetDefaultFilePath() + STATISTIC_SCREENSHOT);
Application.CaptureScreenshot(STATISTIC_SCREENSHOT);
//因为截屏保存需要时间,所以加等待判断文件已存在再执行后续操作
while(!UnityFileSave.IsFileExist(STATISTIC_SCREENSHOT)){
yield return new WaitForSeconds(0.05f);
}
//MultiplateUtiles.ShareScreenShotToWeibo(platform, ResString.STR_SHARE_WEIBO, STATISTIC_SCREENSHOT);
}
//file save function:
//get default save/read file path on device
public static string GetDefaultFilePath(){
return Application.persistentDataPath + "/";
}
//Is file exist in default path public static bool IsFileExist(string name){ return IsFileExistByPath(GetDefaultFilePath() + name);; } //is file exist in custom path and name public static bool IsFileExistByPath(string path){ FileInfo info = new FileInfo(path); if (info == null || info.Exists == false) { return false; }; return true; }