TextureBlenderMaterialPropertyCacheHelper.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. public class TextureBlenderMaterialPropertyCacheHelper
  8. {
  9. private struct MaterialPropertyPair
  10. {
  11. public Material material;
  12. public string property;
  13. public MaterialPropertyPair(Material m, string prop)
  14. {
  15. material = m;
  16. property = prop;
  17. }
  18. public override bool Equals(object obj)
  19. {
  20. if (!(obj is MaterialPropertyPair)) return false;
  21. MaterialPropertyPair b = (MaterialPropertyPair)obj;
  22. if (!material.Equals(b.material)) return false;
  23. if (property != b.property) return false;
  24. return true;
  25. }
  26. public override int GetHashCode()
  27. {
  28. return base.GetHashCode();
  29. }
  30. }
  31. private Dictionary<MaterialPropertyPair, object> nonTexturePropertyValuesForSourceMaterials = new Dictionary<MaterialPropertyPair, object>();
  32. private bool AllNonTexturePropertyValuesAreEqual(string prop)
  33. {
  34. bool foundFirst = false;
  35. object firstVal = null;
  36. foreach (MaterialPropertyPair k in nonTexturePropertyValuesForSourceMaterials.Keys)
  37. {
  38. if (k.property.Equals(prop))
  39. {
  40. if (!foundFirst)
  41. {
  42. firstVal = nonTexturePropertyValuesForSourceMaterials[k];
  43. foundFirst = true;
  44. }
  45. else
  46. {
  47. if (!firstVal.Equals(nonTexturePropertyValuesForSourceMaterials[k]))
  48. {
  49. return false;
  50. }
  51. }
  52. }
  53. }
  54. return true;
  55. }
  56. public void CacheMaterialProperty(Material m, string property, object value)
  57. {
  58. nonTexturePropertyValuesForSourceMaterials[new MaterialPropertyPair(m, property)] = value;
  59. }
  60. public object GetValueIfAllSourceAreTheSameOrDefault(string property, object defaultValue)
  61. {
  62. if (AllNonTexturePropertyValuesAreEqual(property))
  63. {
  64. foreach (MaterialPropertyPair k in nonTexturePropertyValuesForSourceMaterials.Keys)
  65. {
  66. if (k.property.Equals(property))
  67. {
  68. return nonTexturePropertyValuesForSourceMaterials[k];
  69. }
  70. }
  71. }
  72. return defaultValue;
  73. }
  74. }
  75. }