//用处,根据分辨率的改变,自适应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);
}
}