Unity 3d多按钮点击播放视频求助:求单按钮交互示例
Hey there, let's break this down step by step since you're new to Unity—no need to overcomplicate things! I'll walk you through a complete single-button example that ties a UI button to a Video Player, including setting up the click listener and getting your video to play in landscape mode.
Step 1: Set Up Your Core UI & Video Elements
First, let's get the basic objects in place:
- Right-click in the Hierarchy window → UI → Button - TextMeshPro (or just Button if you don't have TextMeshPro installed). Rename it
PlayMyVideoButtonso it's easy to find. - Next, add a Video Player component: Right-click in the Hierarchy → Video → Video Player, and name this object
VideoPlayer. - To display the video, add a RawImage as the output target: Right-click under the
VideoPlayerobject → UI → RawImage, name itVideoDisplay. Drag this RawImage into the Target Texture field of the Video Player component—Unity will automatically create a Render Texture for you here.
Step 2: Configure the Video Display for Landscape
To make the video play in a landscape view:
- Select the
VideoDisplayRawImage, adjust its RectTransform to stretch across the screen horizontally. Use the anchor presets (click the square icon in the RectTransform section) to select "Stretch" for both width and height, then tweak the offsets if needed to fit your desired landscape ratio. - We'll add code later to force the game to switch to landscape orientation when the button is clicked.
Step 3: Create the Click Listener Script
Create a new C# script named VideoButtonHandler and paste this code:
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class VideoButtonHandler : MonoBehaviour { // Assign these references in the Inspector panel public Button playVideoButton; public VideoPlayer videoPlayer; public string targetVideoURL; // Example: "https://your-video-host.com/your-video.mp4" void Start() { // Attach the click event listener to the button playVideoButton.onClick.AddListener(PlaySelectedVideo); } void PlaySelectedVideo() { // Force screen to landscape mode (optional but recommended) Screen.orientation = ScreenOrientation.LandscapeLeft; // Stop any currently playing video to avoid conflicts videoPlayer.Stop(); // Set the new video URL videoPlayer.url = targetVideoURL; // Prepare the video (loads it in the background) and start playing videoPlayer.Prepare(); videoPlayer.Play(); } }
Step 4: Hook Up Everything in the Inspector
- Create an empty GameObject in the Hierarchy named
VideoController, then attach theVideoButtonHandlerscript to it. - Drag your
PlayMyVideoButtonfrom the Hierarchy into the Play Video Button field of the script. - Drag your
VideoPlayerobject into the Video Player field. - Enter your video's URL in the Target Video URL field (for local files, use a path like
file:///C:/YourProject/StreamingAssets/MyVideo.mp4on Windows, or adjust for Mac/Linux).
Step 5: Test It Out
Enter Play Mode, click your button—your video should start playing in the RawImage, and the screen should switch to landscape automatically.
Quick Troubleshooting Tips
- If the video won't play, double-check the URL works in a browser first.
- Ensure your Video Player's Source setting is set to URL (not Video Clip).
- For local videos, place them in the
StreamingAssetsfolder of your project (create it if it doesn't exist) and useApplication.streamingAssetsPath + "/MyVideo.mp4"as the URL for better compatibility.
内容的提问来源于stack exchange,提问作者andrew
相关产品推荐
相关产品推荐

