C# 在Unity中使用资源文件夹

C# 在Unity中使用资源文件夹,c#,unity3d,resources,assets,hololens,C#,Unity3d,Resources,Assets,Hololens,我正在开发一个全息透镜项目,需要参考.txt文件。我将这些文件存储在Unity的“Resources”文件夹中,并使它们工作正常(通过Unity运行时): 然而,当为HoloLens部署构建程序时,我能够运行代码,但它不起作用。没有显示任何资源,在检查HoloLens Visual Studio解决方案(通过选择build in Unity创建)时,我甚至没有看到资源或资产文件夹。我想知道我是否做错了什么,或者是否有一种特殊的方式来处理这些资源 还有图像和声音文件 foreach (string

我正在开发一个全息透镜项目,需要参考.txt文件。我将这些文件存储在Unity的“Resources”文件夹中,并使它们工作正常(通过Unity运行时):

然而,当为HoloLens部署构建程序时,我能够运行代码,但它不起作用。没有显示任何资源,在检查HoloLens Visual Studio解决方案(通过选择build in Unity创建)时,我甚至没有看到资源或资产文件夹。我想知道我是否做错了什么,或者是否有一种特殊的方式来处理这些资源

还有图像和声音文件

foreach (string str in im)
{
      spriteList.Add(Resources.Load<Sprite>(str));
}
foreach(im中的字符串str)
{
spriteList.Add(Resources.Load(str));
}

字符串'str'有效;它在团结一致的情况下工作得非常好。但是,同样,在运行HoloLens时,它不会加载任何内容。

您无法使用
StreamReader
文件
类读取资源目录。你必须使用

1。路径与项目的资产文件夹中的任何资源文件夹相对

2。不要在路径参数中包含文件扩展名,如.txt、.png、.mp3

3。如果资源文件夹中有另一个文件夹,请使用前斜杠而不是后斜杠。反斜杠不行

文本文件

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
支持的TextAsset格式:

txt.html.htm.xml.bytes.json.csv.yaml.fnt

声音文件

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
图像文件

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
精灵-单个

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
纹理类型设置为“精灵”(2D和UI)的图像,以及

将“精灵”模式设置为“单一”的图像

精灵-多个

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
纹理类型设置为“精灵”(2D和UI)的图像,以及

将“精灵”模式设置为“多个”的图像

游戏对象预设

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}
3D网格(如FBX文件)

3D网格(来自游戏对象预制)

3D模型(作为游戏对象)

异步加载

TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
IEnumerator loadFromResourcesFolder()
{
    //Request data to be loaded
    ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));

    //Wait till we are done loading
    while (!loadAsync.isDone)
    {
        Debug.Log("Load Progress: " + loadAsync.progress);
        yield return null;
    }

    //Get the loaded data
    GameObject prefab = loadAsync.asset as GameObject;
}

要使用:
start例程(loadFromResourcesFolder())

如果要从资源加载文本文件,请不要指定文件扩展名 只需按照下面给出的代码进行操作

private void Start()
{
    TextAsset t = Resources.Load<TextAsset>("textFileName");
    List<string> lines = new List<string>(t.text.Split('\n'));
    foreach (string line in lines)
    {
        Debug.Log(line);
    }
}
private void Start()
{
TextAsset t=Resources.Load(“textFileName”);
列表行=新列表(t.text.Split('\n'));
foreach(行中的字符串行)
{
Debug.Log(行);
}
}

debug.Log
可以打印文本文件中可用的所有数据

我明白了,这很有意义。然后,我的下一部分将是,我也有图像和声音文件(在参考资料中),我正在使用resources.load检索这些文件。这些都是统一的,但当我部署到HoloLens时也不行。我在几个小时前回答了许多关于Resources文件夹和一个文件夹的问题,其中大多数问题的答案相同,但标题或描述不同。我已经决定制定一个解决方案,它将覆盖大多数问题和文件类型,以防止将来提供重复的答案。请检查我的编辑,如果您有问题,请告诉我。这是我一直在寻找的资源的实用且完整的参考:-)