首先,我只是创建了这个问题,因为我无法在几乎所有需要的信息的问题上发表评论。这是一个问题,所以我不必复制更多内容:
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 :(得分:0)
在第二个文件中,您必须为要使用的命名空间添加using
语句:
[[ other-file.cs ]]
using GeneralStuff;
namespace SecondFile {
public class SecondClass {
public SecondClass() {
var something = GeneralStuff.setColor(0f,0f,0f);
}
}
}
编辑:您还需要将其包含在当前项目中。来自this SO answer:
- 右键单击项目,然后选择“添加 - >现有项目”
- 导航至 要添加为链接的文件,然后选择它。
- 看看“添加” “添加现有项”对话框右下角的按钮。它有一个 小滴箭头。
- 点击该下拉箭头。选择“添加为链接”。
醇>