Video3D.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Video;
  6. public class Video3D : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private VideoClip videoClip;
  10. private VideoPlayer vp;
  11. private RenderTexture rt;
  12. [SerializeField]
  13. private RawImage ri;
  14. [SerializeField]
  15. bool useVideoRosulution = true;
  16. [SerializeField]
  17. RectTransform rectTransform;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. if (videoClip && ri) {
  22. vp = GetComponent<VideoPlayer>();
  23. if (vp == null) {
  24. vp = gameObject.AddComponent<VideoPlayer>();
  25. }
  26. vp.clip = videoClip;
  27. vp.isLooping = true;
  28. if (useVideoRosulution) {
  29. rt = new RenderTexture((int)vp.clip.width, (int)vp.clip.height, 0);
  30. } else {
  31. rt = new RenderTexture(Screen.width / 2, Screen.height, 0);
  32. }
  33. vp.renderMode = VideoRenderMode.RenderTexture;
  34. vp.targetTexture = rt;
  35. ri.texture = rt;
  36. rectTransform = GetComponent<RectTransform>();
  37. if (rectTransform) {
  38. rectTransform.sizeDelta = new Vector2((int)vp.clip.width, (int)vp.clip.height);
  39. }
  40. }
  41. }
  42. }