Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Unity: Assigning a new clip to video player changes ALL video player clips in my current scene

I have several GameObjects instantiated, all of them have a video player component, all those objects are inside a main canvas and should be able to play the video simultaneously. When I assign one of them a new clip, ALL of them start playing over again and change their resource to the new one. Of course, it should only change the one I actually assign it it. I have made sure, the function below gets only called ONCE and on the correct object. I also printed the hash codes of the videos player to make sure they are different objects, and they are.

Does anyone have any idea what could cause this ?

How I assign the clip

        public virtual void SetVideoResource(string fileName)
        {
            videoName = fileName;
            textPanel.SetActive(false);
            videoPanel.SetActive(true);
            videoPlayer = videoPanel.GetComponentInChildren<VideoPlayer>();
            videoPlayer.clip = (VideoClip)Resources.Load("Videos/" + fileName);
            Debug.Log(videoPlayer.GetHashCode());
        }

Setup of my Video Player component

enter image description here

And the target texture

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fixed the problem by myself:

The reason for the behaviour was not, that all the clips got reassigned. (All video players still had their individual video resource) But instead, I had one single render texture assigned to the video players. Of course that wont work, as all video players will render their frames onto this one render texture.

The solution instantiates a custom render texture based on the size of the video panel for each individual video player:

        public virtual void SetVideoResource(string fileName)
        {
            videoName = fileName;
            textPanel.SetActive(false);
            videoPanel.SetActive(true);
            float parentWidth = videoPanel.GetComponent<RectTransform>().rect.width;
            float parentHeight = videoPanel.GetComponent<RectTransform>().rect.height;
            videoPlayer = videoPanel.GetComponentInChildren<VideoPlayer>();
            CustomRenderTexture targetTexture = new CustomRenderTexture((int)(parentWidth*0.90f), (int)(parentHeight * 0.90f), RenderTextureFormat.ARGB32);
            videoPanel.GetComponentInChildren<RawImage>().texture = targetTexture;
            videoPlayer.targetTexture = targetTexture;
            videoPlayer.clip = (VideoClip)Resources.Load("Videos/" + fileName);
        }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...