Convert Texture to Texture2D using RenderTexture

+ Save Texture2D File (EncodeToPNG())


++) in case using casting::

Texture2D test = (Texture2D)targetTexture;

-> creates UnityException: Texture 'textureName' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.


However, it cannot be saved and an error popped up.

-> Unsupported texture format - Texture2D::EncodeTo functions do not support compressed texture formats.



PROBLEM with source underneath:: Graphics.Blit has transparency problem in Android. -> Maybe it's of Hardware.


-> SOLVED: Use Camera with material and RenderTexture, and use the method Texture2D.ReadPixels. (180912)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Texture targetTexture;
string saveImagePath;
 
private void Save()
{
    int width = targetTexture.width;
    int height = targetTexture.height;
    //save the active render texture
    RenderTexture curRenderTexture = RenderTexture.active;
    
    //create new render texture and copy from the target texture
    RenderTexture copiedRenderTexture = new RenderTexture(width, height, 0);
    Graphics.Blit(targetTexture, copiedRenderTexture);
 
    //change active render texture
    RenderTexture.active = copiedRenderTexture;
 
    //copy to texture 2d
    Texture2D convertedImage = new Texture2D(width, height, TextureFormat.RGBA32, false);
    convertedImage.ReadPixels(new Rect(00, width, height), 00);
    convertedImage.Apply();
 
    //change back to prev active render texture
    RenderTexture.active = curRenderTexture;
 
    //additional
    //save to persistent data path
    Color[] pixels = convertedImage.GetPixels();
    byte[] imgBytes = convertedImage.EncodeToPNG();
    File.WriteAllBytes(saveImagePath, imgBytes);
}
cs


'유니티' 카테고리의 다른 글

유니티 Screen Orientation  (0) 2018.09.04
World Position to the specific Rect screen position  (0) 2018.09.03
Unity Attitude  (0) 2018.09.03
유니티 FrameRate 확인  (0) 2018.06.26
유니티 화면 꺼짐 방지  (0) 2018.06.26
Posted by ssume
,

로딩 바를 prefab으로 뽑아서 landscape / portrait에 따라서 다른 프리팹을 로딩하여 사용하고 있다. 해당 프리팹을 인스턴스화 해주는 부분은 Start에서 실행.


Orientation: Portrait Upside Down 에서 portrait로 시작한 뒤 180도 도는 현상을 보였다.


확인 과정:

1. UGUI Image를 canvas에 붙여두면 orientation에 따라서 같이 돌아간 뒤로 시작하여 화면 상에서는 회전이 보이지 않는다.

2. Prefab을 로딩해서 사용할 경우 화면 상에서 회전이 보인다.

3. Prefab을 canvas에 붙인 뒤 사용할 경우 화면 상에서 회전이 보이지 않는다.


Debug로 찍어 본 결과 Awake와 Update에서는 이미 Orientation 처리가 끝난 뒤다.

제일 처음 앱에 진입할 때 orientation 처리는 끝나는 것 같은데 왜 그 이후에 이미지를 로딩하면 돌아간 상태 (Portrait 기준으로 awake)로 초기화되는지 모르겠다...


-> 해결법: canvas에 이미지를 붙여 사용한다....



'유니티' 카테고리의 다른 글

Unity Texture to Texture 2d (convert through RenderTexture)  (0) 2018.09.11
World Position to the specific Rect screen position  (0) 2018.09.03
Unity Attitude  (0) 2018.09.03
유니티 FrameRate 확인  (0) 2018.06.26
유니티 화면 꺼짐 방지  (0) 2018.06.26
Posted by ssume
,

//Variables

GameObject targetObject;

Camera renderCamera;

RectTransform canvasRect;



//Update or somewhere

Vector3 viewportPos = renderCamera.WorldToScreenPoint(targetObject.transform.position);

//World Position to Viewport Position


Vector2 localScreenPos = new Vector2();

RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, viewportPos, renderCamera, out localScreenPos);

//Viewport Position to local Rect Position





'유니티' 카테고리의 다른 글

Unity Texture to Texture 2d (convert through RenderTexture)  (0) 2018.09.11
유니티 Screen Orientation  (0) 2018.09.04
Unity Attitude  (0) 2018.09.03
유니티 FrameRate 확인  (0) 2018.06.26
유니티 화면 꺼짐 방지  (0) 2018.06.26
Posted by ssume
,

Unity Attitude

유니티 2018. 9. 3. 11:19

Unity에서 Attitude를 사용해 Object의 forward 자북방향 가리키게 하기




Posted by ssume
,






Posted by ssume
,

Android

Screen.sleepTimeout = SleepTimeOut.NeverSleep;

getWindow().addFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


Iphone

iPhoneSettings.screenCanDarken = false;

'유니티' 카테고리의 다른 글

Unity Texture to Texture 2d (convert through RenderTexture)  (0) 2018.09.11
유니티 Screen Orientation  (0) 2018.09.04
World Position to the specific Rect screen position  (0) 2018.09.03
Unity Attitude  (0) 2018.09.03
유니티 FrameRate 확인  (0) 2018.06.26
Posted by ssume
,