自用相机管理



//用处,根据分辨率的改变,自适应canvas大小,不去改变长度,自适应高度和相机的值

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
public struct Point
{
    public double x;
    public double y;
    public double z;
}
public static class Angle
{
    public static double[] getAllAngle(Point a, Point b, Point c)
    {
        double[] result = new double[3];
        result[0] = getIntersectionAngle(b, c, a);
        result[1] = getIntersectionAngle(a, c, b);
        result[2] = getIntersectionAngle(a, b, c);
        return result;
    }

    public static double getIntersectionAngle(Point a, Point b, Point Intersection)
    {
        Point tempPoint1 = new Point { x = Intersection.x - a.x, y = Intersection.y - a.y, z = Intersection.z - a.z };
        Point tempPoint2 = new Point { x = Intersection.x - b.x, y = Intersection.y - b.y, z = Intersection.z - b.z };

        double temp = tempPoint1.x * tempPoint2.x + tempPoint1.y * tempPoint2.y + tempPoint1.z * tempPoint2.z;
        double temp2 = Math.Sqrt(Math.Pow(tempPoint1.x, 2) + Math.Pow(tempPoint1.y, 2) + Math.Pow(tempPoint1.z, 2));
        double temp3 = Math.Sqrt(Math.Pow(tempPoint2.x, 2) + Math.Pow(tempPoint2.y, 2) + Math.Pow(tempPoint2.z, 2));
        double tempCos = temp / (temp2 * temp3);
        double result = Math.Acos(tempCos);

        return Math.Round(((180 * result) / Math.PI), 1);
    }
}

public class UICameraScale : MonoBehaviour {

    public RectTransform canvas;
    private float lastwidth = 0f;
    private float lastheight = 0f;
    private Camera m_Camera;
    void Awake()
    {
        m_Camera = this.transform.GetComponent<Camera>();
    }
    void Start()
    {
       
    }
    void Update()
    {
        if (lastwidth != Screen.width || lastheight != Screen.height)
        {
            lastwidth = Screen.width;
            lastheight = Screen.height;
            ResetSize();
        }
    }

    private void ResetSize()
    {
        //Debug.Log(Screen.width + " / " + Screen.height + " = " + ((float)Screen.width / (float)Screen.height));
        canvas.sizeDelta = new Vector2(1920, 1920 * Screen.height / Screen.width);
        //调用类库:
        Point a = new Point { x = 0, y = 0, z = 0 };
        Point b = new Point { x = 0, y = canvas.sizeDelta.y / 2, z = 2000 };
        Point c = new Point { x = 0, y = -canvas.sizeDelta.y / 2, z = 2000 };
        double[] result = Angle.getAllAngle(a, b, c);
        m_Camera.fieldOfView = (float)result[0];
        EventManager.getInstance().Broadcast<Vector2>(EventEnum.UI_ChangeCameraScreen, canvas.sizeDelta);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值