private void generatePrefab(int x, int y, ColorToPrefab colorMapping) { float xPos = x * xOffset; // Are we on an odd row? if (y % 2 == 1) { xPos += xOffset / 2f; } GameObject hex_go = (GameObject)Instantiate(colorMapping.prefab, new Vector3(xPos, 0, y * zOffset), Quaternion.identity, this.transform); // Name the gameobject something sensible. hex_go.name = "Hex_" + x + "_" + y; // Make sure the hex is aware of its place on the map Hex hexClass = hex_go.GetComponent <Hex>(); hexClass.x = x; hexClass.y = y; //assign properties to hex hexClass.isWalkable = colorMapping.isWalkable; hexClass.costToEnter = colorMapping.costToEnterTile; temp.Add(hexClass); //tilesToGameObjects.Add(hexClass, hex_go); if (generateHeigthMap) { float heigthScale = heigthMapTexture.GetPixel(x, y).grayscale *colorMapping.heigthScaleMultiplier + 0.5f; hex_go.transform.Find("model").localScale = new Vector3(1, 1, heigthScale); hex_go.transform.position = new Vector3(hex_go.transform.position.x, hex_go.transform.position.y + heigthScale * 0.057f, hex_go.transform.position.z); } }
//Generates tile data array and fills initial data. void GenerateTile(int x, int y) { Color pixelColor = map.GetPixel(x, y); ColorToPrefab colorMapping = new ColorToPrefab(); colorMappings[x + y * map.width] = colorMapping; colorMapping.password = CreateRandomString(passwordLength); colorMapping.pos = new Vector2(x, y); colorMapping.color = pixelColor; colorMapping.gameobject = Instantiate(prefab, colorMapping.pos, Quaternion.identity, transform); }
void CreateScriptableObjectFromTexture(Texture2D texture) { //Get and set values for the new object string name = Path.GetFileNameWithoutExtension(assetPath); LevelObject newLevel = ScriptableObject.CreateInstance <LevelObject>(); newLevel.levelName = name; //Actually create the new object AssetDatabase.CreateAsset(newLevel, LevelPath); AssetDatabase.SaveAssets(); //For some reason the texture actually isnt done being imported yet so use delay call UnityEditor.EditorApplication.delayCall += () => { //Set the map Texture2D t = AssetDatabase.LoadAssetAtPath <Texture2D>(assetPath); newLevel.map = t; //Set the values for the color array HashSet <Color> colors = new HashSet <Color>(); //List<Color> colors = new List<Color>(); //Get all the colors for (int x = 0; x < t.width; x++) { for (int y = 0; y < t.height; y++) { Color c = t.GetPixel(x, y); //Ignore transparent colors if (c.a < 1) { continue; } colors.Add(c); } } //Set the array to have a value for each color ColorToPrefab[] matchings = new ColorToPrefab[colors.Count]; int i = 0; foreach (Color color in colors) { matchings[i] = new ColorToPrefab() { color = color }; i++; } //Finish assigning the colors newLevel.prefabs = matchings; }; }