|
示例代码 以下代码会在您点击屏幕时捕获游戏的屏幕截图,将其保存在临时路径中,然后共享它:
void Update()
{
if( Input.GetMouseButtonDown( 0 ) )
StartCoroutine( TakeScreenshotAndShare() );
}
private IEnumerator TakeScreenshotAndShare()
{
yield return new WaitForEndOfFrame();
Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
ss.Apply();
string filePath = Path.Combine( Application.temporaryCachePath, "shared img.png" );
//不止可以分享png类型
//string filePath = Path.Combine( Application.temporaryCachePath, "test.txt" );
File.WriteAllBytes( filePath, ss.EncodeToPNG() );
// To avoid memory leaks
Destroy( ss );
new NativeShare().AddFile( filePath )
.SetSubject( "Subject goes here" ).SetText( "Hello world!" ).SetUrl( "https://github.com/yasirkula/UnityNativeShare" )
.SetCallback( ( result, shareTarget ) => Debug.Log( "Share result: " + result + ", selected app: " + shareTarget ) )
.Share();
/*
new NativeShare().AddFile( filePath )
.SetSubject( "Subject goes here" ).SetText( "Hello world!" ).SetUrl( "https://www.baidu.com" )
.SetCallback( ( result, shareTarget ) => Debug.Log( "Share result: " + result + ", selected app: " + shareTarget ) )
.Share();
*/
// Share on WhatsApp only, if installed (Android only)
//if( NativeShare.TargetExists( "com.whatsapp" ) )
// new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
}
|