PhotoHouse.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SoftKitty.MasterCharacterCreator
  5. {
  6. public class PhotoHouse : MonoBehaviour
  7. {
  8. public Transform mCameraRoot;
  9. public Camera mCamera;
  10. public GameObject mLight;
  11. public Renderer mBackground;
  12. public static Texture2D TakePhoto(Vector2 _size, Transform _parent, Color _bgColor, float _angle=0F,bool _lightOn=true)
  13. {
  14. GameObject instance = Instantiate(Resources.Load<GameObject>("MasterCharacterCreator/Core/PhotoHouse"));
  15. instance.GetComponent<PhotoHouse>().DestroyLater();
  16. return instance.GetComponent<PhotoHouse>().GetPhoto(_size, _parent, _bgColor, _angle, _lightOn);
  17. }
  18. IEnumerator DestroyLaterCo()
  19. {
  20. yield return 1;
  21. Destroy(gameObject);
  22. }
  23. public void DestroyLater()
  24. {
  25. StartCoroutine(DestroyLaterCo());
  26. }
  27. public Texture2D GetPhoto(Vector2 _size, Transform _parent, Color _bgColor, float _angle, bool _lightOn)
  28. {
  29. if (_parent != null)
  30. {
  31. transform.SetParent(_parent);
  32. transform.localPosition = Vector3.zero;
  33. transform.localEulerAngles = new Vector3(0F, 0F, 90F);
  34. }
  35. mCameraRoot.localEulerAngles = new Vector3(0F,_angle,0F);
  36. mLight.SetActive(_lightOn);
  37. mBackground.material.SetColor("_EmissionColor", _bgColor);
  38. mBackground.material.SetColor("_EmissiveColor", _bgColor);
  39. mBackground.material.SetColor("_EmissiveColorLDR", _bgColor);
  40. RenderTexture rt = new RenderTexture(Mathf.FloorToInt(_size.x), Mathf.FloorToInt(_size.y), 24, UnityEngine.Experimental.Rendering.DefaultFormat.LDR);
  41. mCamera.targetTexture = rt;
  42. Texture2D screenShot = new Texture2D(Mathf.FloorToInt(_size.x), Mathf.FloorToInt(_size.y), TextureFormat.RGB24, false);
  43. mCamera.Render();
  44. RenderTexture.active = rt;
  45. screenShot.ReadPixels(new Rect(0, 0, Mathf.FloorToInt(_size.x), Mathf.FloorToInt(_size.y)), 0, 0);
  46. screenShot.Apply();
  47. mCamera.targetTexture = null;
  48. RenderTexture.active = null;
  49. Destroy(rt);
  50. return screenShot;
  51. }
  52. }
  53. }