UGUI对话框自适应

来源:互联网 发布:守望先锋先锋大神数据 编辑:程序博客网 时间:2024/05/22 02:11



using UnityEngine;using System.Collections;public class WinAdaptiveComponent : MonoBehaviour {    private float mReferenceWidth = 1334f;    private float mReferenceHeight = 750f;    /// <summary>    /// Canvas Scaler Match Width Or Height    /// </summary>    public bool mIsAdaptiveWidth = true;    private RectTransform mRect;// Use this for initializationvoid Start ()    {        mRect = GetComponent<RectTransform>();        mReferenceWidth = mRect.sizeDelta.x;        mReferenceHeight = mRect.sizeDelta.y;        Adaptive();}    void Adaptive()    {        float pex = 1.0f;        if (mIsAdaptiveWidth)        {            float tmpHeight = Screen.height * (mReferenceWidth / Screen.width);            if (tmpHeight < mReferenceHeight)            {                pex = tmpHeight / mReferenceHeight;            }        }        else        {            float tmpWidth = Screen.width * (mReferenceHeight / Screen.height);            if (tmpWidth < mReferenceWidth)            {                pex = tmpWidth / mReferenceWidth;            }        }        mRect.localScale = new Vector3(mRect.localScale.x * pex, mRect.localScale.y * pex, 1);    }}


0 0