2016-08-13 16 views
0

首先,我只創建這個問題,因爲我無法在幾乎包含所有需要的信息的問題中發表評論。這裏是一個問題,所以我不必重複比我更得:C#在單獨的cs文件中保留常用函數找不到名稱空間

Keep common functions in separate cs file? (C#)

所以我也跟着Jonesopolis給他的答案信息,但它只是拋出了我一個錯誤說,它不能在使用GeneralStuff上找到命名空間;線。

這是CS文件的內容,我想使用的功能來自:

using UnityEngine; 
using System.Collections; 

namespace GeneralStuff 
{ 
    public static class GeneralStuff 
    { 
     /// <summary> 
     /// Creates a Unity color object using values 0-255 
     /// so getting the color right is easier 
     /// </summary> 
     /// <param name="r"></param> 
     /// <param name="g"></param> 
     /// <param name="b"></param> 
     /// <returns></returns> 
     public static Color setColor(float r, float g, float b) 
     { 

      if (r != 0) 
      { 
       r = r/255f; 
      } 

      if (g != 0) 
      { 
       g = g/255f; 
      } 

      if (b != 0) 
      { 
       b = b/255f; 
      } 

      Color color = new Color(r, g, b); 

      return color; 
     } 

    } 
} 

這個CS文件是在同一文件夾中,我想用這個功能在CS文件是在相關的情況下,它是Unity項目中的腳本。我錯過了一些東西,但我無法弄清楚什麼。有人可以幫忙嗎?

+0

在你想用這個靜態類的方法添加_using GeneralStuff文件; _在文件 – Steve

+0

的beginnng我這是在錯誤被拋出。對不起,在我發佈後我總是注意到我錯過的東西。 – Eegxeta

+0

它是否包含在項目中? –

回答

0

在你的第二個文件,你必須有對命名空間的using聲明你想使用:

  • [[ other-file.cs ]]

    using GeneralStuff; 
    
    namespace SecondFile { 
        public class SecondClass { 
         public SecondClass() { 
          var something = GeneralStuff.setColor(0f,0f,0f); 
         } 
        } 
    } 
    

編輯:你也需要將其包含在當前項目中。從this SO answer

  1. 右鍵單擊項目並選擇「添加 - >現有項」
  2. 導航到 要添加爲紐帶,並選擇它的文件。
  3. 查看「Add Existing Item」對話框右下角的「Add」 按鈕。它有一個 小箭頭。
  4. 單擊該下拉箭頭。選擇「添加爲鏈接」。
+0

@Eegxeta看到編輯 - 希望能解決你的問題。 –

+0

謝謝你做到了。 – Eegxeta

相關問題