zoukankan      html  css  js  c++  java
  • 调用摄像头并将其显示在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();
            }
        }
    }
  • 相关阅读:
    php 多进程
    关于TP的RBAC的使用
    谈谈自己对于Auth2.0的见解
    php 写队列
    关于thinkphp中Hook钩子的解析
    JS的闭包
    单链表的查找和取值-1
    shell输入输出重定向
    转-Visual Studio控制台程序输出窗口一闪而过的解决方法
    linux下如何调用不同目录下的.h 库文件
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7596990.html
Copyright © 2011-2022 走看看