Unity UGUI AutoLayout

Unity UGUI AutoLayout_第1张图片
48d8d5f6508f73fb39cdf8ca21581902.jpg

既UGUI的UI控件(如Button,Image)自动布局,PC,安卓都可以,解决屏幕缩小的时候UI无法缩小的问题,控件缩放全部按窗口比例缩放,可以设置极值

原创 by superowner ,禁止转载文章
PC导出配置:


Unity UGUI AutoLayout_第2张图片
image.png

//AutoLayout.cs,添加需要伸缩的UI,加上两个组建即可,组建ContentSizeFitter,HorizontalLayoutGroup,配置可以参考我下面的参考链接,具体可以根据自己的项目调节部分代码
//插入代码框参考:http://blog.csdn.net/m_agician/article/details/77420899

# define _Log
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;



namespace Imouto
{
    public class AutoLayout : MonoBehaviour
    {

        //public Slider slider1, slider2;
        public RectTransform rt; //受控物件
#if _Log
        public Text text;
#endif
        //public Image image;

        float aspect;
        float width_;
        float oldwidth_;
        float height_;
        float oldheight_;

        const int MinScreenWidth = 550;
        const int MinScreenHeight = 423;

        bool isFullScreen = false;

        //参考:http://www.manew.com/thread-108077-1-1.html


        void Awake()
        {
            Screen.fullScreen = false;
        }

        // Use this for initialization
        void Start()
        {

            //slider1.value = 1;
            //slider2.value = 1;
            //print("显示器分辨率:" + Screen.currentResolution.width +"x"+ Screen.currentResolution.height);//显示器分辨率
            //print("工程设置的分辨率:" + Screen.height + "-" + Screen.width);//

            width_ = rt.GetComponent().sizeDelta.x;
            //GetComponent().rect.size
            oldwidth_ = Screen.width;
            height_ = rt.GetComponent().sizeDelta.y;
            oldheight_ = Screen.height;
        }

        void OnApplicationQuit()
        {
            PlayerPrefs.DeleteKey("LastRuntimeScreenHeight");
            PlayerPrefs.DeleteKey("LastRuntimeScreenWidth");
        }


        public void OnFullScreen()
        {
            if (false == Screen.fullScreen)
                StartCoroutine(WaitToSet());
        }

        IEnumerator WaitToSet()
        {
            PlayerPrefs.SetInt("LastRuntimeScreenHeight", Screen.height);
            PlayerPrefs.SetInt("LastRuntimeScreenWidth", Screen.width);

            yield return 0;

            isFullScreen = true;
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width_);
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height_);
            Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
        }

        // Update is called once per frame
        void Update()
        {

            //Camera.main.fieldOfView//张角

            //通过检测Camera.main.aspect更新,待定。
            if (false == isFullScreen)
            {
                rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Clamp(width_ * Screen.width / oldwidth_, 16, width_));
                rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Mathf.Clamp(height_ * Screen.height / oldheight_, 3, height_));

                //if (Screen.height < 423 || Screen.width < 550)
                //    Screen.SetResolution(550, 423, isFullScreen);//OK

                //拉伸允许的最小分辨率,Editor下無效
                if (Screen.height < MinScreenHeight)
                    Screen.SetResolution(Screen.width, MinScreenHeight, isFullScreen);
                if (Screen.width < MinScreenWidth)
                    Screen.SetResolution(MinScreenWidth, Screen.height, isFullScreen);
#if _Log
                text.text = "h:" + Screen.height + " w:" + Screen.width;
#endif
            }

            if (true == Screen.fullScreen)
            {
                if (Input.GetKeyDown(KeyCode.Escape))
                {
                    //Screen.fullScreen = false;
                    isFullScreen = false;
                    Screen.SetResolution(PlayerPrefs.GetInt("LastRuntimeScreenWidth"), PlayerPrefs.GetInt("LastRuntimeScreenHeight"), isFullScreen);
                }
            }


        }
    }
}

你可能感兴趣的:(Unity UGUI AutoLayout)