C# 运行时加载映像时出现问题(Unity)(生成后)

C# 运行时加载映像时出现问题(Unity)(生成后),c#,image,file,unity3d,runtime,C#,Image,File,Unity3d,Runtime,我正在Unity 5中创建一个Arcade GUI。我希望实现的主要功能是一个系统,它可以自动为每个游戏(在文件结构中)创建按钮,并从未包含在构建中的文件中加载1个精灵和一些文本。我希望Arcade能够读取与构建相同文件夹中的文件,而不必在构建中包含这些文件。以下是我试用过的一些代码: DirectoryInfo DirectoryToFiles = new DirectoryInfo ("Games"); DirectoryInfo[] Folders = DirectoryToFil

我正在Unity 5中创建一个Arcade GUI。我希望实现的主要功能是一个系统,它可以自动为每个游戏(在文件结构中)创建按钮,并从未包含在构建中的文件中加载1个精灵和一些文本。我希望Arcade能够读取与构建相同文件夹中的文件,而不必在构建中包含这些文件。以下是我试用过的一些代码:

DirectoryInfo DirectoryToFiles = new DirectoryInfo ("Games");
    DirectoryInfo[] Folders = DirectoryToFiles.GetDirectories ();
    foreach (DirectoryInfo Folder in Folders) {
        FileInfo[] Files = Folder.GetFiles ();
        string filename = Files [0].Name.Substring(0,Files [0].Name.IndexOf("."));

        //Loads Image
        Image = Resources.Load ("Games" + "/" + Folder.Name + "/" + filename + ".png") as Texture2D;
        ButtonSprite = Sprite.Create (Image, new Rect (0, 0, Image.width, Image.height), new Vector2 (0f, 0f));
        //The Declaration of "ButtonSprite" returns a NullReferenceException

        //Reads Description File
        GameDescription = ReadFile (Folder,filename);

        //Gives Path to Play Button
        GameObject.Find ("Play").GetComponent<PlayButton> ().UpdatePath ("Games" + "/" + Folder.Name + "/" + filename + ".exe");


        //Transfers All Variables to the new Button
        CurrentButton = Instantiate (GameButton, GameObject.Find ("Content").GetComponent<Transform> ());
        CurrentButton.GetComponent<Transform> ().localPosition = new Vector3 (x,0f,1f);
        x += 230;
        CurrentButton.GetComponent<ButtonInstantiate> ().UpdateVariables (ButtonSprite, GameName.Substring(0,GameName.IndexOf(".")), GameDescription, Hours);
DirectoryInfo DirectoryToFiles=新的DirectoryInfo(“游戏”);
DirectoryInfo[]Folders=DirectoryToFiles.GetDirectories();
foreach(文件夹中的DirectoryInfo文件夹){
FileInfo[]Files=Folder.GetFiles();
字符串文件名=文件[0].Name.Substring(0,文件[0].Name.IndexOf(“.”);
//加载图像
Image=Resources.Load(“Games”+“/”+Folder.Name+“/”+filename+“.png”)作为纹理2D;
ButtonSprite=Sprite.Create(图像,新矩形(0,0,Image.width,Image.height),新向量2(0f,0f));
//“ButtonSprite”的声明返回NullReferenceException
//读取描述文件
GameDescription=ReadFile(文件夹,文件名);
//提供播放按钮的路径
GameObject.Find(“Play”).GetComponent().UpdatePath(“Games”+“/”+Folder.Name+“/”+filename+”.exe”);
//将所有变量转移到“新建”按钮
CurrentButton=实例化(GameButton,GameObject.Find(“内容”).GetComponent());
CurrentButton.GetComponent().localPosition=新向量3(x,0f,1f);
x+=230;
CurrentButton.GetComponent().UpdateVariables(ButtonsWrite,GameName.Substring(0,GameName.IndexOf(“.”),GameDescription,小时);


如果有人能告诉我需要使用的具体方法,我将不胜感激。谢谢!

我相信问题在于以下几行:

    Image = Resources.Load ("Games" + "/" + Folder.Name + "/" + filename + ".png") as Texture2D;
使用Resources.Load时,您不提供文件类型,只提供路径名。此外,Image是Unity类型,这可能会给您带来错误。如果您尝试以下操作,它应该可以工作:

    Texture2D btnTexture = Resources.Load ("Games/" + Folder.Name + "/" + filename) as Texture2D;
编辑:但是,这将在构建中的Assets/Resources/Games/FolderName/FileName下查找目录。有关从外部目录加载纹理的详细信息,这可能会有所帮助:

为了让其他潜在读者更清楚,请提供一些进一步的信息:您在Resources文件夹中放置的任何内容都将包含在构建中。建议您仅在要使用这些内容时将其放置在构建中,否则只会占用额外的空间


希望这有帮助!

我认为问题在于以下几点:

    Image = Resources.Load ("Games" + "/" + Folder.Name + "/" + filename + ".png") as Texture2D;
使用Resources.Load时,您不提供文件类型,只提供路径名。此外,Image是Unity类型,这可能会给您带来错误。如果您尝试以下操作,它应该可以工作:

    Texture2D btnTexture = Resources.Load ("Games/" + Folder.Name + "/" + filename) as Texture2D;
编辑:但是,这将在构建中的Assets/Resources/Games/FolderName/FileName下查找目录。有关从外部目录加载纹理的详细信息,这可能会有所帮助:

为了让其他潜在读者更清楚,请提供一些进一步的信息:您在Resources文件夹中放置的任何内容都将包含在构建中。建议您仅在要使用这些内容时将其放置在构建中,否则只会占用额外的空间

希望这有帮助