Skip to content

Commit

Permalink
chore: refactor mesh materials into a manager class
Browse files Browse the repository at this point in the history
  • Loading branch information
dbirman committed Feb 29, 2024
1 parent 9e8746e commit a7ced97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,15 @@ namespace Urchin.Managers

public class PrimitiveMeshManager : MonoBehaviour
{
[SerializeField] private List<Material> _materials;
[SerializeField] private List<string> _materialNames;
[SerializeField] private Transform _primitiveMeshParentT;
[SerializeField] private GameObject _primitivePrefabGO;

//Keeping a dictionary mapping names of objects to the game object in schene
private Dictionary<string, MeshRenderer> _primMeshRenderers;
private Dictionary<string, Material> _materialDictionary;

private void Awake()
{
_primMeshRenderers = new Dictionary<string, MeshRenderer>();
_materialDictionary = new Dictionary<string, Material>();
if (_materials.Count != _materialNames.Count)
throw new System.Exception("List of materials and material names must be the same length");
for (int i = 0; i < _materials.Count; i++)
{
_materialDictionary.Add(_materialNames[i], _materials[i]);
}

}

private void Start()
Expand All @@ -55,7 +44,7 @@ private void Start()
tempObject.GetComponent<MeshBehavior>().SetID(meshID);
tempObject.name = $"primMesh_{meshID}";
_primMeshRenderers.Add(meshID, tempObject.GetComponent<MeshRenderer>());
tempObject.GetComponent<MeshRenderer>().material = _materialDictionary["default"];
tempObject.GetComponent<MeshRenderer>().material = MaterialManager.MeshMaterials["default"];
}
}

Expand Down Expand Up @@ -129,7 +118,7 @@ public void SetMaterial(Dictionary<string, string> meshMaterials)
{
foreach (var kvp in meshMaterials)
{
Material newMaterial = _materialDictionary[kvp.Value];
Material newMaterial = MaterialManager.MeshMaterials[kvp.Value];
MeshRenderer tempMesh = _primMeshRenderers[kvp.Key];
Color color = tempMesh.material.color;
tempMesh.material = newMaterial;
Expand Down
29 changes: 29 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MaterialManager : MonoBehaviour
{
[SerializeField] private List<Material> _meshMaterials;
[SerializeField] private List<string> _materialNames;

private static MaterialManager Instance;
private Dictionary<string, Material> _materialsDict;

private void Awake()
{
if (Instance != null)
throw new Exception("Only a single instance of MaterialManager should exist in the scene.");

if (_meshMaterials.Count != _materialNames.Count)
Debug.LogWarning("Mesh material and name count should be equal");

_materialsDict = new();
foreach (var zip in _meshMaterials.Zip(_materialNames, (material, name) => (material, name)))
_materialsDict.Add(zip.name, zip.material);
}

public static Dictionary<string, Material> MeshMaterials { get { return Instance._materialsDict; } }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7ced97

Please sign in to comment.