문제
내가 만든 화면을 다른 사람에게 공유하고 싶을 때 어떻게하지?
하면서 찾아봤던 기능
현재 에셋스토어에서는 구매해서 사용할 수 있는걸로 보인다. 깃허브에서는 쓸 수 있지만 에셋스토어에서 주기적으로 업데이트 해주는거겠지?
하지만 나는 현재 씬의 화면을 캡쳐해서 이미지로 공유하는 기능만 사용하면 됐기에 그냥 사용해봤는데 Unity6(6000.0.37f1)버전으로도 정상적으로 작동했다.
링크
>> https://github.com/yasirkula/UnityNativeShare
GitHub - yasirkula/UnityNativeShare: A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text
A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS - yasirkula/UnityNativeShare
github.com
사용 방법
일단 패키지를 다운받고 에셋 폴더에 넣은 후, 코드를 작성하면 된다.
깃허브 리드미를 살펴보면 사용 방법이 자세하게 나와있어서 간단하게 적용할 수 있었다.
나는 버튼 클릭을 했을 때 전체화면이 캡쳐가 되면서 공유하기가 되는 기능을 구현했다.
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public static class SHARE_INFO
{
public static string SUBJECT = $"이미지";
}
public class ClickSharePage : MonoBehaviour
{
[SerializeField] private Button shareButton;
private void Start()
{
shareButton.onClick.AddListener(OnShareButtonClick);
}
public void OnShareButtonClick()
{
StartCoroutine(TakeScreenshotAndShare());
}
private string MakeSampleScreenShotImage()
{
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());
Destroy(ss);
return filePath;
}
private IEnumerator TakeScreenshotAndShare()
{
yield return new WaitForEndOfFrame();
string filePath = MakeSampleScreenShotImage();
new NativeShare().AddFile(filePath)
.SetSubject(SHARE_INFO.SUBJECT).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();
}
}
주석 처리된 부분은 아마 특정 앱을 지정해서 공유하기가 가능하다는 것 같음!
각 어플의 API를 안써도 된다는 점이 유용합니다~
'Unity > Project' 카테고리의 다른 글
[Unity] step5-1. 좀비런: 찌끄만 버그들 픽스와 맵 디자인^^ - 계획을 지키지 못한 바보의 일기 (3) | 2024.04.14 |
---|---|
[Unity] step5. 좀비런: 점수 기능, 맵 제작(배경, 바닥) (2) | 2024.04.13 |
[Unity] step4. 좀비런: 좀비맛 스프라이트 이용하여 애니메이션 설정, 쿠키 슬라이드 기능(Feat. 내배캠 - TIL) (0) | 2024.04.12 |
[Unity] step3. 좀비런: UI와 로딩화면 제작 (2) | 2024.04.11 |
[Unity] step2. 좀비런: 더블 점프 기능, 장애물 설치 (Feat. 내배캠 - TIL) (2) | 2024.04.10 |
문제
내가 만든 화면을 다른 사람에게 공유하고 싶을 때 어떻게하지?
하면서 찾아봤던 기능
현재 에셋스토어에서는 구매해서 사용할 수 있는걸로 보인다. 깃허브에서는 쓸 수 있지만 에셋스토어에서 주기적으로 업데이트 해주는거겠지?
하지만 나는 현재 씬의 화면을 캡쳐해서 이미지로 공유하는 기능만 사용하면 됐기에 그냥 사용해봤는데 Unity6(6000.0.37f1)버전으로도 정상적으로 작동했다.
링크
>> https://github.com/yasirkula/UnityNativeShare
GitHub - yasirkula/UnityNativeShare: A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text
A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS - yasirkula/UnityNativeShare
github.com
사용 방법
일단 패키지를 다운받고 에셋 폴더에 넣은 후, 코드를 작성하면 된다.
깃허브 리드미를 살펴보면 사용 방법이 자세하게 나와있어서 간단하게 적용할 수 있었다.
나는 버튼 클릭을 했을 때 전체화면이 캡쳐가 되면서 공유하기가 되는 기능을 구현했다.
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public static class SHARE_INFO
{
public static string SUBJECT = $"이미지";
}
public class ClickSharePage : MonoBehaviour
{
[SerializeField] private Button shareButton;
private void Start()
{
shareButton.onClick.AddListener(OnShareButtonClick);
}
public void OnShareButtonClick()
{
StartCoroutine(TakeScreenshotAndShare());
}
private string MakeSampleScreenShotImage()
{
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());
Destroy(ss);
return filePath;
}
private IEnumerator TakeScreenshotAndShare()
{
yield return new WaitForEndOfFrame();
string filePath = MakeSampleScreenShotImage();
new NativeShare().AddFile(filePath)
.SetSubject(SHARE_INFO.SUBJECT).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();
}
}
주석 처리된 부분은 아마 특정 앱을 지정해서 공유하기가 가능하다는 것 같음!
각 어플의 API를 안써도 된다는 점이 유용합니다~
'Unity > Project' 카테고리의 다른 글
[Unity] step5-1. 좀비런: 찌끄만 버그들 픽스와 맵 디자인^^ - 계획을 지키지 못한 바보의 일기 (3) | 2024.04.14 |
---|---|
[Unity] step5. 좀비런: 점수 기능, 맵 제작(배경, 바닥) (2) | 2024.04.13 |
[Unity] step4. 좀비런: 좀비맛 스프라이트 이용하여 애니메이션 설정, 쿠키 슬라이드 기능(Feat. 내배캠 - TIL) (0) | 2024.04.12 |
[Unity] step3. 좀비런: UI와 로딩화면 제작 (2) | 2024.04.11 |
[Unity] step2. 좀비런: 더블 점프 기능, 장애물 설치 (Feat. 내배캠 - TIL) (2) | 2024.04.10 |