• 调用摄像头并将其显示在UGUI image上自适应屏幕大小


    参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html

    不过该博主是竖屏,我的是横屏

    代码修改:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    /// <summary>
    /// 调用摄像机并自适应大小的脚本
    /// </summary>
    public class STCamDeviceController : MonoBehaviour
    {
    
        WebCamTexture camTexture;
        CanvasScaler CanScaler;
        Camera ca;
        Image img;
    
        void Start()
        {
    
            img = GetComponentInChildren<Image>();
    
            CanScaler = GetComponentInChildren<CanvasScaler>();
            CanScaler.referenceResolution = new Vector2(Screen.width, Screen.height);
    
            ca = GetComponentInChildren<Camera>();
            ca.orthographicSize = Screen.width / 100.0f / 2.0f;
    
            //img.transform.localScale = new Vector3(-1, -1, -1);
    
            img.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
            img.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
            img.rectTransform.pivot = new Vector2(0.5f, 0.5f);
    
            img.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width);
            img.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height);
    
            // 设备不同的坐标转换
    #if UNITY_IOS || UNITY_IPHONE
            img.transform.Rotate (new Vector3 (0, 180, 90));
    #elif UNITY_ANDROID
            img.transform.Rotate (new Vector3 (0, 0, 90));
    #endif
    
            StartCoroutine(CallCamera());
        }
    
        IEnumerator CallCamera()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                if (camTexture != null)
                    camTexture.Stop();
    
                WebCamDevice[] cameraDevices = WebCamTexture.devices;
                string deviceName = cameraDevices[0].name;
    
                camTexture = new WebCamTexture(deviceName, Screen.height, Screen.width, 60);
                img.canvasRenderer.SetTexture(camTexture);
    
                camTexture.Play();
            }
        }
    }
  • 相关阅读:
    Django REST framework+Vue 打造生鲜超市(十二)
    pymsql
    (四)Jmeter系列之---http接口请求脚本优化
    (三)Jmeter系列之---实现一个简单的http接口请求
    2021每天一个知识点(二月)
    (二)Jmeter系列之---Jmeter工具介绍
    (一)Jmeter系列之---性能测试+工具介绍
    Linux磁盘的分区操作
    Python格式化处理json数据的方式
    (17)-Python3之--文件操作
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7596990.html
Copyright © 2020-2023  润新知