1

I am creating an Arcade GUI in Unity 5. The main feature I wish to implement is a system that automatically creates buttons for each game (in the file structure) and loads 1 sprite and some text from files not included in the build. I want the Arcade to read files in the same folder as the build without having to include those files in the build. Here's some code I experimented with:

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);

This is what my Build file looks like.

This is what the File structure looks like

If anyone can let me know a specific method I need to use, I would appreciate it. Thanks!

1 Answers1

0

I believe the issue lies in the following line:

    Image = Resources.Load ("Games" + "/" + Folder.Name + "/" + filename + ".png") as Texture2D;

When using Resources.Load, you don't give a filetype, just the path name. Also, Image is a Unity type, which may be giving you an error. It should work if you try:

    Texture2D btnTexture = Resources.Load ("Games/" + Folder.Name + "/" + filename) as Texture2D;

Edit: This will however look for the directory in your build under Assets/Resources/Games/FolderName/FileName. For more information on loading textures from an external directory, this may help: http://answers.unity3d.com/questions/813495/can-i-load-textures-outside-the-resources-folder.html

Just some further info for clarity to other potential readers: Anything you put in the Resources folder will be included in the build. It's advisable to only put things in the build if you're going to use them, otherwise it just takes up extra space.

Hope this helps!

Aspekt
  • 78
  • 1
  • 8