大大的蛋项目 第二篇 第三关

                                                             大大的蛋项目 第二篇

     这个项目的UI要自动适应分辨率,我们采用了Unity3d自带的UI,对于GUI的自适应原则是这样的!

      GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(m_fScaleWidth, m_fScaleHeight, 1));

      其中m_fScaleWidth,m_fScaleHeight是屏幕运行是的分辨率与当前分辨率的比例系数!

       float m_fScreenWidth = 1440;
       float m_fScreenHeight= 900;
       float m_fScaleWidth;
       float m_fScaleHeight;

      void Awake ()
    {
     m_fScaleWidth = (float)((Screen.width)/m_fScreenWidth);
     m_fScaleHeight = (float)((Screen.height)/m_fScreenHeight);     
    }

    //UI控制界面.
    void OnGUI()
    {
    GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(m_fScaleWidth, m_fScaleHeight, 1));
    GUI.DrawTexture(new Rect(200,600,bgtex.width,bgtex.height),bgtex);
    }

    这样就可以自适应了!

原文地址:https://www.cnblogs.com/alongu3d/p/3070289.html