[Unity] Road to Picnic (Craft_Week) - Mobile ScreenCapture

박호준·2022년 5월 27일
0

Craft_Week

목록 보기
17/20
post-thumbnail

Mobile ScreenCapture

  • 처음 기획단계에서 요구하였던 엔딩씬의 화면을 사용자의 모바일기기의 사진첩에 저장되도록 구현을 하였다.

UnityNativeGallery를 이용하면 iphone, Andorid구분없이 구현 할 수 있다.

https://github.com/yasirkula/UnityNativeGallery

Code

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" );
	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();

	// Share on WhatsApp only, if installed (Android only)
	//if( NativeShare.TargetExists( "com.whatsapp" ) )
	//	new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
}
profile
hopark

0개의 댓글