After a couple tries, I still haven't found any solution to this problem of mine.
Currently, I have a script inheriting from MonoBehavior which creates a map with Perlin Noise. Let's call it mapGenerator
.
It instantiates a HexTile
object from another script, which does not inherit from MonoBehaviour, that contains a refere to my terrain prefab and an enum value which says its type (so water
, grass
, wood
, etc.). There is an array declared at the beginning of the script that I manually change as I'm adding terrains.
Everything works fine except when tweaking the map generation, If I want to change some values for perlin intervals, I might have to add another value to my HexTile[]
and then drag my terrain prefab and set the enum value.
What I want is to know if there is a way to make the script select a prefab depending on the value the enum was set to.
Here is the code (simplified):
public class ProceduralTerrainGenerator : MonoBehaviour
{
public bool regenerateMap = true;
public Hextile[] hexTiles;
void Update()
{
CreateMap();
}
private void CreateMap()
{
// Some code for Perlin Noise generation inside perlinMap[,]
for (int i = 0; i < mapLength; i++)
{
for (int j = 0; j < mapHeight; j++)
{
// Setting up position from i and j index
foreach (HexTile hexTile in hexTiles)
{
if (ValueInRange(perlinMap[i,j], hexTile.LowLimit, hexTile.HighLimit)
{
Instantiate<HexTile>(hexTile.tile, position, Quaternion.Euler(0f, 0f, 0f), this.transform);
}
}
}
}
}
}
and my other script
[System.Serializable]
public class HexTile
{
public GameObject tile;
public HexMetrics.HexType hexType;
public float perlinNoiseLowLimit;
public float perlinNoiseHighLimit;
}
=========== EDIT 1 ===========
I tried simplifing my problem, because I'm more interested in the concept, but here are more details.
I have a class named HexSettings, which contains the hex prefab (HexTile
) named tile
, its HexType, and the perlin noise bundaries.
[System.Serializable]
public class HexSettings
{
public HexTile tile;
public HexMetrics.HexType hexType;
public float perlinNoiseLowLimit;
public float perlinNoiseHighLimit;
}
Here is the HexType enum contained inside HexMetrics
public class HexMetrics
{
// Hex metrics data for placement in grid data
public enum HexType
{
GRASS,
FOREST,
WATER,
SNOW,
DESERT,
LAKE
}
}
What I would like is to have some soprt of dictionnary that contains the correct prefab according to the selected HexType.
As you can see in the image below, I can set my HexType to my terrain types, but I also have to set the HexTile object manually, where it could be set automatically according to the HexType value set. That is what I'm looking for.
I tried adding it in a dictionnary (something like Dictionnary<Hextype, HexTile>
), looking for prefabs in my folder prefab with Resources.LoadAll<HexTile>("prefab");
, no success, prefabs couldn't be loaded in HexSettings.