Unity 2017.2.1在iPad上相机画面被放大,如何恢复正常?
Hey there, this over-scaling issue on iPad is super common with Unity's WebCamTexture—usually it boils down to mismatched aspect ratios between the camera's output resolution, your render plane's dimensions, and iOS's Retina display handling. Let's break down how to fix it:
Why This Happens
iPad's camera outputs a specific resolution, and if your render plane's UV mapping or material texture scaling doesn't match that aspect ratio, Unity will stretch the texture to fit—resulting in that 1.2x over-scaled look you're seeing. Also, iOS's high-density Retina screen can throw off automatic scaling if you don't explicitly handle it.
Step-by-Step Fixes
First, let's adjust your code to properly calculate and apply the correct texture scaling. Here's the modified version with explanations:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFeed : MonoBehaviour { public Renderer rend; void Start () { WebCamDevice[] devices = WebCamTexture.devices; // Debug log for available cameras for(int i = 0; i < devices.Length; i++) { print("Webcam available: " + devices[i].name); } rend = this.GetComponentInChildren<Renderer>(); WebCamTexture tex = new WebCamTexture(devices[0].name); rend.material.mainTexture = tex; tex.Play(); // Calculate correct scaling to fit the camera feed perfectly float cameraAspectRatio = (float)tex.width / tex.height; // Get your plane's aspect ratio (based on its local scale) float planeAspectRatio = rend.transform.localScale.x / rend.transform.localScale.y; // Adjust scale to match the camera's aspect ratio to the plane float scaleX = cameraAspectRatio / planeAspectRatio; // Flip front camera to avoid mirrored feed (optional but useful) if (devices[0].isFrontFacing) { scaleX = -scaleX; } // Apply the scale to the main texture (note the correct property name: "_MainTex") rend.material.SetTextureScale("_MainTex", new Vector2(scaleX, 1f)); } void Update () { } }
Key Changes Made
- Correct Texture Property Name: Your original commented-out line used
"tex"—Unity's default standard material uses_MainTexas the main texture property name, so this ensures the scaling actually takes effect. - Aspect Ratio Matching: We calculate the exact ratio between the camera's resolution and your plane's dimensions, then apply a scale that fits the feed perfectly without stretching or over-scaling.
- Front Camera Handling: Added optional flip for front-facing cameras to fix mirrored feeds, which can also indirectly affect how the feed appears scaled.
Additional Tips
- If you still see minor scaling issues, you can tweak the
scaleXvalue slightly (e.g., multiply by 0.95f) to fine-tune for iPad's specific screen properties. - Ensure your render plane's UV coordinates are set to the default 0-1 range (no custom UV stretching) to avoid conflicting with the texture scale we're applying.
- Test directly on the iPad—editor preview might not perfectly replicate iOS's rendering behavior.
内容的提问来源于stack exchange,提问作者Zenlabs

