From a7ced979f026168be566832a45b4691e529cac29 Mon Sep 17 00:00:00 2001 From: Dan Birman Date: Thu, 29 Feb 2024 14:42:51 -0800 Subject: [PATCH] chore: refactor mesh materials into a manager class --- .../Scripts/Managers/PrimitiveMeshManager.cs | 15 ++-------- .../Scripts/Utils/MaterialManager.cs | 29 +++++++++++++++++++ .../Scripts/Utils/MaterialManager.cs.meta | 11 +++++++ 3 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs create mode 100644 UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs.meta diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs b/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs index 14e6f6a9..bb6e254b 100644 --- a/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs +++ b/UnityClient/Packages/vbl.urchin/Scripts/Managers/PrimitiveMeshManager.cs @@ -9,26 +9,15 @@ namespace Urchin.Managers public class PrimitiveMeshManager : MonoBehaviour { - [SerializeField] private List _materials; - [SerializeField] private List _materialNames; [SerializeField] private Transform _primitiveMeshParentT; [SerializeField] private GameObject _primitivePrefabGO; //Keeping a dictionary mapping names of objects to the game object in schene private Dictionary _primMeshRenderers; - private Dictionary _materialDictionary; private void Awake() { _primMeshRenderers = new Dictionary(); - _materialDictionary = new Dictionary(); - 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() @@ -55,7 +44,7 @@ private void Start() tempObject.GetComponent().SetID(meshID); tempObject.name = $"primMesh_{meshID}"; _primMeshRenderers.Add(meshID, tempObject.GetComponent()); - tempObject.GetComponent().material = _materialDictionary["default"]; + tempObject.GetComponent().material = MaterialManager.MeshMaterials["default"]; } } @@ -129,7 +118,7 @@ public void SetMaterial(Dictionary 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; diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs b/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs new file mode 100644 index 00000000..80540693 --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs @@ -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 _meshMaterials; + [SerializeField] private List _materialNames; + + private static MaterialManager Instance; + private Dictionary _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 MeshMaterials { get { return Instance._materialsDict; } } +} diff --git a/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs.meta b/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs.meta new file mode 100644 index 00000000..93cbf1ab --- /dev/null +++ b/UnityClient/Packages/vbl.urchin/Scripts/Utils/MaterialManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 28c1e24c412fe1e4fbd97e2e9fcaae6c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: