2020-09-05 14:22:53 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UIBgAdaptive : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public GameObject[] bgList;
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < bgList.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (bgList[i] != null)
|
|
|
|
|
{
|
|
|
|
|
bgList[i].GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
|
|
|
|
|
bgList[i].GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);
|
|
|
|
|
bgList[i].GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
|
|
|
|
|
bgList[i].transform.localScale = new Vector3(1, 1, 1);
|
|
|
|
|
bgList[i].transform.localPosition = new Vector3(0, 0, 0);
|
2020-09-05 20:05:50 +08:00
|
|
|
|
|
|
|
|
|
//float curHeight = 1920 * (Screen.height / Screen.width) / (1920 / 1080);
|
|
|
|
|
//float curWidth = (1080f / 1920f) * curHeight;
|
|
|
|
|
//bgList[i].GetComponent<RectTransform>().sizeDelta = new Vector2(curWidth, curHeight);
|
2020-09-24 17:26:27 +08:00
|
|
|
|
if (Screen.height >= 1920)
|
2020-09-05 20:05:50 +08:00
|
|
|
|
{
|
2021-06-04 18:06:44 +08:00
|
|
|
|
//--计算比例因子 比例因子 = 10 ^ ((lg(屏幕宽 / 开发宽) + lg(屏幕高 / 开发高) / 2))
|
|
|
|
|
//local log = math.log(Screen.width / 1080, 10) + math.log(Screen.height / 1920, 10)
|
|
|
|
|
//local avg = log / 2
|
|
|
|
|
//local ft = math.pow(10, avg)
|
|
|
|
|
//-- 实际分辨率 = 屏幕分辨率 / 比例因子
|
|
|
|
|
//UIManager.UIHeight = Screen.height / ft
|
|
|
|
|
//UIManager.UIWidth = Screen.width / ft
|
|
|
|
|
float log = Mathf.Log10(Screen.width / 1080f) + Mathf.Log10(Screen.height / 1920f);
|
|
|
|
|
float avg = log / 2f;
|
|
|
|
|
float ft = Mathf.Pow(10, avg);
|
|
|
|
|
float curHeight = Screen.height / ft;
|
|
|
|
|
float curWidth = curHeight /1920f * 1080f;
|
2020-09-05 20:05:50 +08:00
|
|
|
|
//Debug.Log("Screen.height " + Screen.height+ " curWidth "+ curWidth);
|
2021-06-04 18:06:44 +08:00
|
|
|
|
bgList[i].GetComponent<RectTransform>().sizeDelta = new Vector2(curWidth, curHeight);
|
2020-09-05 20:05:50 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bgList[i].GetComponent<RectTransform>().sizeDelta = new Vector2(1080, 1920) * 1.2f;
|
|
|
|
|
}
|
2020-09-05 14:22:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|