Jump to content

I am making a level editor for my game in unity and I need help...

forecaster1310

My level editor works by scanning a Texture2D and searching for specific colors. If it finds the color, it instantiates an object. I want to make a folder that you can drop your maps into and the game will take the png and make a map. How can I do that?

 

Here's my code so far...

It works but the folder feature doesn't.

<

using System.IO;
using UnityEngine;

public class LevelGenarator : MonoBehaviour
{
    public Texture2D map;
    public string LevelFileName;

    public ColorToTile[] colorMappings;
    // Start is called before the first frame update
    void Start()
    {
        GenearteLevel();
    }

    void GenearteLevel()
    {
        
        for (int x = 0; x < map.width; x++)
        {
            for (int y = 0; y < map.height; y++)
            {
                GenerateTile(x, y);
            }
        }
    }

    void GenerateTile(int x, int y)
    {
        Color pixelColor = map.GetPixel(x, y);
        if (pixelColor.a == 0)
        {
            // that means that the pixel is transparent so we don't need to make any tiles... 
            return;
        }

        foreach (ColorToTile colorMapping in colorMappings)
        {
            if (colorMapping.color.Equals(pixelColor))
            {
                Vector2 pos = new Vector2(x, y);
                Instantiate(colorMapping.prefab, pos, Quaternion.identity, transform);
            }
        }
    }
}
>

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't code in Unity so let me know if I am understanding correctly.

You are instantiating the LevelGenerator class object with an existing Texture2D?

If yes, then it isn't it matter of iterating over the png's in a folder then passing it to the function you are using to create the Texture2D object you are testing with?

 

 foreach (file in files) 
 { new LevelGenerator(new Texture2D(file))}

 

Link to comment
Share on other sites

Link to post
Share on other sites

What you want to do is load your image in to a byte array then use the Texture2D's Loadfile function on the byte array.  There is more details in this blog post which I've included the link for 

 

https://gyanendushekhar.com/2017/07/08/load-image-runtime-unity/

 

By using the byte array method you can then specific a folder location anywhere on the system The only thing that you would have to hook up some place else through your UI is how the user would select the specific map file that they want to use.  Hopefully this helps and is what you're looking for

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×