直接本上代码。
IEnumerator GetScreenshot()
{
// 截屏1帧后再呼起微信
yield return new WaitForEndOfFrame();
string imgPath = System.IO.Path.Combine(Application.persistentDataPath, "Screenshot.jpg");
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
tex.Apply();
System.IO.File.WriteAllBytes(imgPath, tex.EncodeToJPG());
PaiShareUrl = imgPath;
//0 session
//1 timeline
string shareType = "0";
Debug.Log("分享截图:" + imgPath);
#if UNITY_IOS || UNITY_IPHONE
_WXSharePicture("", "", "", shareType, imgPath);
#elif UNITY_ANDROID
AndroidJavaClass jc = new AndroidJavaClass(Defines.APP_UNITY_CLASS);
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("_WXSharePicture", new string[] { "", "", "", shareType, imgPath });
#endif
}