zoukankan      html  css  js  c++  java
  • unity3d UI自动适合屏幕分辨率

    用unity3d开发移动平台的游戏  不可避免的会遇到屏幕分辨率的问题   

    不同的分辨率上会使得原本正常的UI变得乱七八糟   

    我们知道  在unity3d中可以拿一个plane作为背景 UI则是绘制在离摄像机最近的位置  可以认为是绘制在摄像机上的 

    因此分辨率的不同会导致UI的位置和大小出现错误 

    我们完全可以用一个plane去模拟button  并将它放在世界空间中  这样虽然可以解决位置和大小的问题  但是所带来的问题也一大堆并难于维护 

    因此我们需要根据屏幕的大小去按比例缩放UI 

    假如原本有个按钮是这样,并且当前的480x854分辨率下没问题,如果改成600x1024或者其他的分辨率,便会发现位置和大小都不正确了 

    function 
    OnGUI () 
    { 
       
      if (GUI.Button(Rect(Screen.width - 200, Screen.height - 100, 64, 64), 
    "Start")) 
       
      { 
       
        // dosomething 
       
      } 
    }



      
    于是我们按比例去移动和缩放UI   

    // 
    original screen size 
    var 
    m_fScreenWidth  : float = 480; 
    var 
    m_fScreenHeight : float = 854; 
    
    
    // 
    scale factor 
    var 
    m_fScaleWidth  : float; 
    var 
    m_fScaleHeight : float; 
    
    
    function 
    Awake () 
    { 
       
      m_fScaleWidth = parseFloat(Screen.width)/m_fScreenWidth; 
       
      m_fScaleHeight = parseFloat(Screen.height)/m_fScreenHeight;     
      
    } 
    
    
    function 
    OnGUI () 
    { 
       
      if (GUI.Button(Rect(Screen.width - 200 * m_fScaleWidth , Screen.height - 
    100 * m_fScaleHeight , 64 * m_fScaleWidth , 64 * m_fScaleHeight ), 
    "Start")) 
       
      { 
       
        // dosomething 
       
      } 
    } 


      
    若UI控件较多的时候,对每一个都去控制大小显然没必要 
    则使用矩阵实现 

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

    这样就将button的位置和大小都按照比例缩放了  很简单

  • 相关阅读:
    repeater嵌套RadioButtonList赋值
    hive表导出到mysql报错
    hive创建表时报错
    linux————mysql————修改密码
    Spark入门:Spark运行架构(Python版)
    1、Spark简介(Python版)
    使用蒙特 ·卡罗方法计算圆周率近似值
    python运算符,内置函数简单使用
    numpy、pandas、scipy、matplotlib、jieba、 openpyxl、pillow的安装
    XML详解
  • 原文地址:https://www.cnblogs.com/fm168/p/3123176.html
Copyright © 2011-2022 走看看