유니티 NGUI 사용 프로젝트에서 런타임에서 스크린샷 캡쳐 생성후 PNG 저장 및 Texture2D로 불러오기 (Unity After creating screenshot capture at runtime in NGUI-using project, save PNG and load as Texture2D)
이모티콘・01・고양이 마멋 친구들 - Google Play 앱
이모티콘・01・고양이 마멋 친구들: 무료 이모티콘, 회원가입 없이! 카톡, SNS로 감정 표현이 쉬워져요. 귀여움 가득, 대화창을 더 풍성하게!
play.google.com



// ## 이 내용을 포스팅 하는 이유: 삽질했던 내용들을 재삽질 하지 않기 위해. | |
// ## 이런 방식으로 작업한 이유: GUI를 사용했다면 그냥 PNG 저장 후 Sprite로 다시 불러오는 방식으로 했을텐데 | |
// 예전에 NGUI로 만들어진 프로젝트를 가지고 작업을 해서. | |
// ------- 캐릭터 선택 스크롤 뷰의 각 캐릭터 버튼의 부모(Table)의 부모(Scrollview)에 붙어있는 스크립트 ----------------------- | |
/// <summary> | |
/// 캐릭터 선택버튼에 포함된 아이콘에 사용 할 Texture를 캡쳐하기 위해 | |
/// 각 3D캐릭터에 붙어 있는 증명사진용? 카메라를 이용해 캡쳐한 후 | |
/// Application.persistentDataPath의 해당 경로에 PNG파일로 저장 | |
/// </summary> | |
public void CaptureAllUiCamera() | |
{ | |
StartCoroutine(CoCaptureAllUiCamera()); | |
} | |
/// <summary> | |
/// 코루틴 | |
/// 캐릭터 선택버튼에 포함된 아이콘에 사용 할 Texture를 캡쳐하기 위해 | |
/// 각 3D캐릭터에 붙어 있는 증명사진용? 카메라를 이용해 캡쳐한 후 | |
/// Application.persistentDataPath의 해당 경로에 PNG파일로 저장 | |
/// </summary> | |
IEnumerator CoCaptureAllUiCamera() | |
{ | |
// 왠지 모르겠지만 한 프레임을 기다려 주라고 한다. | |
yield return new WaitForEndOfFrame(); | |
// Texture2D 변수를 하나 생성 후 | |
Texture2D txt2d; | |
// 내가 가지고 있는 캐릭터 수 만큼 반복문을 돌린다. | |
for (int i = 0; i < CntMyChar; i++) | |
{ | |
// 각 3D캐릭터에 붙어있는 카메라의 TargetTexture(미리 설정)의 크기로 초기화하고 | |
txt2d = new Texture2D(arrUserCamera[i].targetTexture.width, arrUserCamera[i].targetTexture.height); | |
// Camera.Render 관련 정보: https://docs.unity3d.com/kr/530/ScriptReference/Camera.Render.html | |
arrUserCamera[i].Render(); | |
RenderTexture.active = arrUserCamera[i].targetTexture; | |
txt2d.ReadPixels(new Rect(0, 0, arrUserCamera[i].targetTexture.width, arrUserCamera[i].targetTexture.height), 0, 0); | |
txt2d.Apply(); | |
byte[] bytes = txt2d.EncodeToPNG(); | |
Destroy(txt2d); | |
string sPath = Application.persistentDataPath + "/ScreenShot/"; | |
string sFullpath = $"{sPath}capture{(i + 1).ToString("D2")}.png"; | |
DirectoryInfo dir = new DirectoryInfo(sPath); | |
// 해당 경로에 디렉토리가 없으면 생성 | |
if (!dir.Exists) | |
{ | |
Directory.CreateDirectory(sPath); | |
} | |
// 설정한 경로에 PNG 파일 생성. | |
File.WriteAllBytes(sFullpath, bytes); | |
} | |
// 캐릭터 버튼에 포함된 아이콘의 Texture를 새로고침 하기 위한 static 함수 호출 | |
RefreshUiButtonCharIcon(); | |
} | |
/// <summary> | |
/// 캐릭터 버튼에 포함된 아이콘의 Texture를 새로고침 하기 위한 static 함수 | |
/// </summary> | |
public static void RefreshUiButtonCharIcon() | |
{ | |
// 스크롤뷰를 찾아 해당 Child를 일단 찾아 | |
Transform trsScrollviewSetCharUI = GameObject.Find("Scroll View SetCharUI").transform.GetChild(0); | |
// 새로고침 할 캐릭터 아이콘을 포함한 버튼들의 부모: trsScrollviewSetCharUI | |
foreach (Transform child in trsScrollviewSetCharUI) | |
{ | |
// 캐릭터 아이콘을 포함한 버튼에 붙어 있는 SetCharUiButton 스크립트 안에 있는 RefreshScrollviewCharIcon 함수를 호출 | |
// 성능에 별로 좋지 않으니 특별히 엄청 귀찮을 때에만 SendMessage를 사용하길 권장. | |
child.GetComponent<SetCharUiButton>().SendMessage("RefreshScrollviewCharIcon"); | |
} | |
} | |
// ------- 캐릭터 선택 스크롤 뷰의 각 캐릭터 버튼에 붙어있는 스크립트 ---------------------------------------------- | |
/// <summary> | |
/// 캐릭터 선택페이지 스크롤뷰 아이콘용 이미지 PNG로 저장한 거 다시 Texture2D로 불러오기 | |
/// </summary> | |
/// <param name="path"></param> | |
void LoadRenderTextureFromPersistentDataPath(string path) | |
{ | |
StartCoroutine(CoLoadRenderTextureFromPersistentDataPath(path)); | |
} | |
IEnumerator CoLoadRenderTextureFromPersistentDataPath(string path) | |
{ | |
// 로딩중에 로딩 아이콘 활성 | |
gameObject.transform.GetChild(7).gameObject.SetActive(true); | |
// Deprecated 되었지만 일단 이걸로 씀: UnityWebRequest 권장 | |
WWW www = new WWW(path); | |
yield return www; | |
Texture2D uiTexture2d = www.texture; | |
gameObject.transform.GetChild(1).GetComponent<UITexture>().mainTexture = uiTexture2d; | |
// 로딩이 끝나고 로딩 아이콘 비활성 | |
gameObject.transform.GetChild(7).gameObject.SetActive(false); | |
} | |
/// <summary> | |
/// 캐릭터 선택 스크롤 뷰의 각 캐릭터 버튼의 부모(Table)의 부모(Scrollview)에 붙어있는 | |
/// 스크립트에서 이 함수를 SendMessage를 이용해 호출. | |
/// 성능에 별로 좋지 않으니 특별히 엄청 귀찮을 때에만 SendMessage를 사용하길 권장. | |
/// </summary> | |
public void RefreshScrollviewCharIcon() | |
{ | |
// 첫번째 index에 다른 object가 있음. | |
int num = gameObject.transform.GetSiblingIndex() + 1; | |
string sPath = Application.persistentDataPath + "/ScreenShot/"; | |
string sFullpath = $"{sPath}capture{num.ToString("D2")}.png"; | |
LoadRenderTextureFromPersistentDataPath(sFullpath); | |
} |
WWW 관련 이걸로 수정
유니티 WWW 대신 UnityWebRequest 사용하기 class UnityEngine.WWW deprecated
유니티 WWW 대신 UnityWebRequest 사용하기 class UnityEngine.WWW deprecated CS0618: 'WWW'은(는) 사용되지 않습니다. 'Use UnityWebRequest, a fully featured replacement which is more efficient and has ad..
ssscool.tistory.com
저장경로 관련 이걸로 수정
유니티 에디터에서는 되는데 안드로이드 디바이스에서 파일 관련 동작 안 될 때 unity curl error 7: f
유니티 에디터에서는 되는데 안드로이드 디바이스에서 파일 관련 동작 안 될 때 It works in the Unity editor, but the file-related operation does not work on Android devices. unity curl error 7: failed t..
ssscool.tistory.com
두더지게임: 리그 오브 두더지, 게임쿠폰
구글플레이스토어 쿠폰사용 O, 앱스토어 쿠폰사용 X (앱스토어 정책상) 10만골드 가족 20만골드 건강 30만골드 자유 40만골드 행복하자 50만골드 아프지말고 젤리툰 웹툰 소재 전문 사이트 젤리툰
ssscool.tistory.com
[무료게임쿠폰] 뽑기키우기게임이라 쓰고 이세계 가서 몬스터 때려잡아서 레벨업 후 사차원가서
[무료게임쿠폰] 뽑기키우기게임이라 쓰고 이세계 가서 몬스터 때려잡아서 레벨업 후 사차원가서 짱먹기 라고 읽는다. 다이아 100개 쿠폰 테슬라 다이아 200개 쿠폰 건강 다이아 300개 쿠폰 부자 다
ssscool.tistory.com
