Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [System.Serializable]
- public class ColorToPrefab
- {
- public Color color;
- public Transform prefab;
- }
- public class LabirynthGenerator : MonoBehaviour
- {
- [SerializeField] private Texture2D map;
- [SerializeField] private ColorToPrefab[] colorMappings;
- [SerializeField] private float offset = 5f;
- private void GenerateTile(int x, int z)
- {
- Color pixelColor = map.GetPixel(x, z);
- if (pixelColor.a == 0) return;
- foreach (ColorToPrefab mapping in colorMappings)
- {
- // jeżeli kolory się nie zgadzają - idziemy do kolejnego
- if (!mapping.color.Equals(pixelColor)) continue;
- Vector3 position = new Vector3(x, 0, z) * offset;
- Instantiate(mapping.prefab, position, Quaternion.identity, transform);
- }
- }
- private void ClearLabirynth()
- {
- while (transform.childCount > 0)
- {
- DestroyImmediate(transform.GetChild(0).gameObject);
- }
- }
- public void GenerateLabirynth()
- {
- ClearLabirynth();
- for (int x = 0; x < map.width; x++)
- {
- for (int z = 0; z < map.height; z++)
- {
- GenerateTile(x, z);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement