【问题标题】:Screen positions of GUI elements in Unity3DUnity3D中GUI元素的屏幕位置
【发布时间】:2013-06-30 17:52:02
【问题描述】:

我正在使用 Unity3D 的标准 GUI。

如何获取 GUI 元素的屏幕位置?

【问题讨论】:

    标签: user-interface unity3d


    【解决方案1】:

    基本上你不能。使用更好的词,正如您已经注意到的那样,GUI.Button 只返回一个布尔值,指示按钮是否被按下。

    因为您实际上是在每一帧重新创建按钮(您的 GUI.Button 代码在回调中,例如 UpdateFixedUpdateOnGUI、...),并且当您调用 @987654326 @ 你自己传递了Rect 边界,实际上不需要查询任何对象来检索实际坐标。只需将它们存放在某个地方即可。

    Rect buttonBounds = new Rect (50,60,100,20);
    
    bool buttonPressed = GUI.Button (buttonBounds, "get postion");
    
    if (buttonPressed)
    {
      //you know the bounds, because buttonBounds button has been pressed
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      var positionGui : Vector2;
      positionGui = Vector2 (guiElement.transform.position.x * Screen.width, guiElement.transform.position.y * Screen.height);
      

      【讨论】:

        【解决方案3】:

        你可以这样做

        public static Rect screenRect
            (float tx,
             float ty,
             float tw,
             float th) 
        {
            float x1 = tx * Screen.width;
            float y1 = ty * Screen.height;     
            float sw = tw * Screen.width;
            float sh = th * Screen.height;
            return new Rect(x1,y1,sw,sh);
        }
        public void OnGUI()
        {   
        
        
          if (GUI.Button(screenRect(0.4f, 0.6f, 0.2f, 0.1f), "TRY AGAIN")) 
        
         {  
          Application.LoadLevel(0);
        
         }
        
        print ("button position + framesize"+screenRect(0.4f, 0.6f, 0.2f, 0.1f));
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-31
          • 2017-08-06
          • 1970-01-01
          • 1970-01-01
          • 2019-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多