2013-05-06 65 views
1

我开始我的项目,我得到这个错误无法弄清楚它有什么问题。 如果是愚蠢的问题,真的非常不满意帮助和抱歉。我仍然是新的团结。感谢所有的答案。统一3d错误如何解决它

我试过寻找关于这个vector2(宽度,高度)的帮助,但对我来说它看起来一切正常。 如果有人可以向我解释这个问题,为什么我得到它?

Unity3d错​​误:

Assets/Scenes/Game/Scripts/GUI/GameGUI.cs(22,22): error CS1061: Type `UnityEngine.Rect' does not contain a definition for `center' and no extension method `center' of type `UnityEngine.Rect' could be found (are you missing a using directive or an assembly reference?) 

代码:

using UnityEngine; 
using System.Collections; 

public class GameGUI : MonoBehaviour { 


    void OnResume() { 
     enabled = true; 
    } 

    void OnPause() { 
     enabled = false; 
    } 


    void OnGUI() { 
     int fps = (int) (1f/Time.deltaTime*Time.timeScale); 
     GUILayout.Box("FPS "+fps); 

     Vector2 size = GUI.skin.label.CalcSize(new GUIContent("+")); 
     Rect rect = new Rect(0, 0, size.x, size.y); 
     rect.center = new Vector2(Screen.width, Screen.height)/2f; 
     GUI.Label(rect, "+"); 
    } 


} 

谢谢您的时间。

+0

问题不在于Vector2。错误表示Rect类没有一个名为center的变量。但是,它的确有(http://docs.unity3d.com/Documentation/ScriptReference/Rect-center.html)。 也许,正如错误消息所说,你错过了一个参考?你也试过右键单击Rect类的名字,然后选择“转到定义”,看看该变量是否存在于类中?也许你使用的是旧版本的Unity,试着检查一下? – Renan 2013-05-06 11:59:40

+0

我在Unity 3.5.6下创建了一个测试类GameGUI,它编译没有错误。必须是一些设置。奇怪 – Kay 2013-05-06 12:00:07

+0

嗯,我的版本是3.2我认为。我使用UniScite,所以我没有“定义”这样的功能。 – karolis 2013-05-06 12:08:06

回答

2

center属性是根据Unity's Script Reference History page.在Unity 3.5中引入的。所以你必须自己计算中心。也许你的构造函数应该是这样的:

Rect rect = new Rect(Screen.width/2f, Screen.height/2f, size.x, size.y); 
+0

“该中心的属性是在Unity 3.5中根据”是的,我认为是这种情况,谢谢你的回答。我试过你的方式: 'codeRect rect = new Rect(Screen.width/2f,Screen.width/2f,size.x,size.y);'它没有奏效。 – karolis 2013-05-06 12:40:39

+0

糟糕,第二个参数必须是'Screen.height/2f'而不是'Screen.width/2f'。我更新了我的答案。 – Kay 2013-05-06 12:52:16

+0

+1漂亮。我希望Unity脚本引用有一个修订下拉。 – Jerdak 2013-05-06 12:59:47