As MovieTexture has been deprecated after Unity 5.6.0b1, I am using VideoPlayer Api to play video over RawImage for Android, by taking reference from here. I am trying to add a toggle to switch from initial size of video played over RawImage Texture to full screen and back to original after video stops.
I have a perfectly playing video, and so far I am able to change the transform of the video to stretch to full screen with this code.
void Update () {
if (Input.GetButtonDown("Jump"))
{
image.rectTransform.offsetMax = Vector2.Lerp(Vector2.up, Vector2.down, 100);
image.rectTransform.offsetMin = Vector2.Lerp(Vector2.left, Vector2.right, 100);
image.rectTransform.rotation = Quaternion.AngleAxis(Mathf.Lerp(0f, 90f, 50), Vector3.forward);
}
}
First two line inside the if
block is able to full screen the RawImage on which the video is played effect shown in section 2 of image. Here's the docs for Vector2.
For the third line code of rotation, I took reference from this discussion on Unity Forum, but still I am not getting the effect as I wanted, result in section 3. I wanted to rotate the content of the RawImage, but I am rotating the RawImage itself, it might be because the reference is not describing to rotate the content.
Can any one help me find out how can I solve it. If it helps version of Unity I am using is Unity 5.6.0b11 Beta, and download the sample project if you want to test in on your device. Unity Video Player.zip 18.33MB You can also follow this Video Tutorial on new VideoPlayer from YouTube.
Update 1
So far, the only thing I got is, I tried only with the rotation code and by removing first two lines inside if
block, I was able to rotate the video, but when I try to stretch the RawImage, it is streching outside of the screen. See here for the image
Update 2
After lots of research, hit and trials. I finally did the fullscreen and rotation of the RawImage to play full screen videos on Unity with this code.
image.rectTransform.offsetMin = new Vector2(-560, 560);
image.rectTransform.offsetMax = new Vector2(560, -560);
But, as you see, values I provide to the vector are static numbers and has no guarantee to work on multiple screen sizes. Well, rotating the RawImage was confusing as the axis rotates along with the RawImage, but some components were not changed or rotated, and when changing offset values, the RawImage changed it value in reference to the older Rect (I really don't know what is happening here).
Rotating RawImage rotates the axis attached to it. On the image above, you can see the RawImage is rotated, and that in turn rotates the Axis. But even when the image is rotated you can see there's some reference line.
How I tried to fix the problem? [Failed]
I tried to solve the problem in new way, I created a new RawImage that fits to the canvas and is already rotated and attached to the script StreamVideo.cs
as the previous RawImage. Initially it is in inactive mode, and is switched to active when user commands to fullscreen. When it goes to full screen the video is played in new RawImage.
public void playFullScreen() {
isFullScreen = true;
imageFullScreen.gameObject.SetActive(true);
imageFullScreen.texture = videoPlayer.texture;
image.gameObject.SetActive(false);
}
But, with this I had problem of starting the video from the same frame on the new RawImage when it is switched to full screen, and same problem when toggling back to small screen.
Summary:
Looking at update 2, I am able to play, pause, full screen and rotate the video but, with the plain numeric values on offsetMax
and offsetMin
. I am trying to learn if there is a way to curate those value through script to fit to multiple screen sizes. I know it might sound simple but the tricky part is, for the screen of my need, the reference of the value I provided works like this. Notice the dashed rectangle in picture.