Edwarddv

CameraGrabAndRender

Apr 26th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1.     public Texture2D GetTexture2DFacePreset(FacePreset thisFacePreset, string generateIconHere, string iconName)
  2.     {
  3.         if (thisFacePreset != null)
  4.         {
  5.             if (customisablePerson != null)
  6.             {
  7.                 customisablePerson.LoadPreset(thisFacePreset);
  8.                 Texture2D generatedTexture = GrabIconTexture();
  9.  
  10.                 string assetPath = Path.GetDirectoryName(generateIconHere) + "/Icons/" + iconName + ".png";
  11.  
  12.  
  13.                 byte[] bytes = generatedTexture.EncodeToPNG();
  14.                 File.WriteAllBytes(assetPath, bytes);
  15.  
  16. #if UNITY_EDITOR
  17.                 AssetDatabase.ImportAsset(assetPath);
  18. #endif
  19.  
  20.                 return generatedTexture;
  21.             }
  22.         }
  23.         return null;
  24.     }
  25.  
  26.     public Texture2D GrabIconTexture()
  27.     {
  28.         if (cam == null)
  29.             cam = transform.Find("IconCamera").GetComponent<Camera>();
  30.  
  31.         RenderTexture rt = new RenderTexture(width, height,24, RenderTextureFormat.ARGB32);
  32.         cam.targetTexture = rt;
  33.         _screenShot = new Texture2D(width, height, textureFormat, false);
  34.        
  35.  
  36.         cam.Render();
  37.         RenderTexture.active = rt;
  38.  
  39.         _screenShot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  40.         _screenShot.Apply();
  41.         cam.targetTexture = null;
  42.         RenderTexture.active = null;
  43.  
  44.         if (Application.isPlaying)
  45.             Destroy(rt);
  46.         else
  47.             DestroyImmediate(rt);
  48.  
  49.         return _screenShot;
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment