에셋 다운 : [URL]
샘플 : [URL]
1. 에셋을 다운로드한다.
- 유니티 에셋 스토어에서 "Native Share for Android & iOS" 에셋을 검색해 현재 프로젝트에 Import 한다.
2. 환경을 설정한다.
- Assets/Plugins/Android 경로에 AndroidManifest.xml 파일이 있는지 확인한다.
- [설치된 유니티 경로]\Editor\Data\PlaybackEngines\AndroidPlayer\Apk 폴더에서 AndroidManifest.xml 파일을 복사한다.
- adroid:authorities 설정은 내 프로젝트의 이름으로 설정한다.
<provider android:name="com.yasirkula.unity.UnitySSContentProvider" android:authorities="MY_UNIQUE_AUTHORITY" android:exported="false" android:grantUriPermissions="true" />
3.코드로 기능을 호출 한다.
- GitHub의 샘플 코드를 확인해 내게 필요한 항목을 설정한다.
- SetText 에 마켓 URL를 넣으면 간단하게 공유 기능을 구현할 수 있다.
using System.IO;
void Update()
{
if( Input.GetMouseButtonDown( 0 ) )
StartCoroutine( TakeSSAndShare() );
}
private IEnumerator TakeSSAndShare()
{
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!" ).Share();
// Share on WhatsApp only, if installed (Android only)
//if( NativeShare.TargetExists( "com.whatsapp" ) )
// new NativeShare().AddFile( filePath ).SetText( "Hello world!" ).SetTarget( "com.whatsapp" ).Share();
}