2013-05-06 67 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