Alle Dateien
Darstellung110 Zeilen

BlockMaterials.cs

Materialien zur Laufzeit: Gras mit Deck- und Seitenfläche, Erde mit der Erde-Textur, jeder Stamm mit Rinde und Jahresringen, jedes Holz mit Brettern (Slabs und Treppen eingeschlossen) — alle Bilder aus Assets/Resources/Textures. Fehlt ein PNG, wird der Block einfach farbig gezeichnet.

Assets/Scripts/BlockMaterials.cs
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Blockwerk
{
    /// <summary>
    /// Materialien zur Laufzeit. Gras bekommt zwei Texturen (Deckfläche und Seite),
    /// alle anderen Blöcke eine Farbe. Fehlt eine Textur, fällt der Block auf seine Farbe zurück.
    /// </summary>
    public static class BlockMaterials
    {
        public const string TextureFolder = "Textures/";

        private static readonly Dictionary<string, Material> Cache =
            new Dictionary<string, Material>();

        public static Material For(BlockType type)
        {
            // Erde trägt die Erde-Textur, die auch unter der Grasnarbe steckt.
            if (type == BlockType.Dirt)
                return Get("dirt", () => Create(BlockInfo.ColorOf(type), LoadTexture("dirt")));

            return Get("color_" + type, () => Create(BlockInfo.ColorOf(type), null));
        }

        public static Material GrassSide()
        {
            return Get("grass_side", () => Create(BlockInfo.ColorOf(BlockType.Grass), LoadTexture("grass_side")));
        }

        public static Material GrassTop()
        {
            return Get("grass_top", () => Create(BlockInfo.ColorOf(BlockType.Grass), LoadTexture("grass_top")));
        }

        /// <summary>Rinde an den vier Seitenflächen eines Stamms.</summary>
        public static Material LogSide(BlockType type)
        {
            return Get("log_side_" + type, () =>
                Create(BlockInfo.ColorOf(type), LoadTexture(BlockInfo.WoodKey(type) + "_log")));
        }

        /// <summary>Jahresringe an Ober- und Unterseite eines Stamms.</summary>
        public static Material LogEnd(BlockType type)
        {
            return Get("log_end_" + type, () =>
                Create(BlockInfo.ColorOf(type), LoadTexture(BlockInfo.WoodKey(type) + "_log_top")));
        }

        public static Material Planks(BlockType type)
        {
            return Get("planks_" + type, () =>
                Create(BlockInfo.ColorOf(type), LoadTexture(BlockInfo.WoodKey(type) + "_planks")));
        }

        private static Material Create(Color color, Texture2D texture)
        {
            Shader shader = Shader.Find("Universal Render Pipeline/Lit");
            if (shader == null) shader = Shader.Find("Standard");
            if (shader == null) shader = Shader.Find("Sprites/Default");

            var material = new Material(shader);

            // Mit Textur zeichnet das Bild selbst, sonst trägt die Farbe den Block.
            Color tint = texture != null ? Color.white : color;
            material.color = tint;

            if (material.HasProperty("_BaseColor")) material.SetColor("_BaseColor", tint);
            if (material.HasProperty("_Glossiness")) material.SetFloat("_Glossiness", 0f);
            if (material.HasProperty("_Smoothness")) material.SetFloat("_Smoothness", 0f);

            if (texture != null) material.mainTexture = texture;
            return material;
        }

        private static Texture2D LoadTexture(string name)
        {
            var texture = Resources.Load<Texture2D>(TextureFolder + name);

            if (texture == null)
            {
                Debug.LogWarning("Textur fehlt: Assets/Resources/" + TextureFolder + name + ".png — Block bleibt farbig.");
                return null;
            }

            texture.filterMode = FilterMode.Point;
            texture.wrapMode = TextureWrapMode.Repeat;
            return texture;
        }

        private static Material Get(string key, Func<Material> factory)
        {
            if (Cache.TryGetValue(key, out Material cached) && cached != null) return cached;

            Material material = factory();
            material.name = "Block_" + key;
            Cache[key] = material;
            return material;
        }

        public static void Clear()
        {
            foreach (Material material in Cache.Values)
                if (material != null) UnityEngine.Object.Destroy(material);

            Cache.Clear();
        }
    }
}