MaterialHelper.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI
  5. {
  6. public class MaterialHelper {
  7. /// <summary>
  8. /// Sets the material values.
  9. /// </summary>
  10. /// <returns>The material values.</returns>
  11. /// <param name="info">Values to set.</param>
  12. /// <param name="baseMaterial">Base material.</param>
  13. public static Material SetMaterialValues(ProceduralImageMaterialInfo info,Material baseMaterial){
  14. if (baseMaterial == null) {
  15. throw new System.ArgumentNullException ("baseMaterial");
  16. }
  17. if (baseMaterial.shader.name != "UI/Procedural UI Image"&&baseMaterial.shader.name != "UI/Procedural UI Image Clip") {
  18. Debug.LogWarning("Parameter 'baseMaterial' does not use shader 'UI/Procedural UI Image'. Method returns baseMaterial.");
  19. return baseMaterial;
  20. }
  21. Material m;
  22. m = baseMaterial;
  23. m.SetFloat("_Width",info.width);
  24. m.SetFloat("_Height",info.height);
  25. m.SetFloat ("_PixelWorldScale",info.pixelWorldScale);
  26. m.SetVector("_Radius",info.radius);
  27. m.SetFloat("_LineWeight",info.borderWidth);
  28. return m;
  29. }
  30. }
  31. /// <summary>
  32. /// Material info. Contains values of "UI/Procedural UI Image" shader
  33. /// </summary>
  34. public struct ProceduralImageMaterialInfo{
  35. public float width;
  36. public float height;
  37. public float pixelWorldScale;
  38. public Vector4 radius;
  39. public float borderWidth;
  40. public ProceduralImageMaterialInfo(float width,
  41. float height,
  42. float pixelWorldScale,
  43. Vector4 radius,
  44. float borderWidth){
  45. this.width = width;
  46. this.height = height;
  47. this.pixelWorldScale = pixelWorldScale;
  48. this.radius = radius;
  49. this.borderWidth = borderWidth;
  50. }
  51. public override string ToString ()
  52. {
  53. return string.Format ("width:{0},height:{1},pws:{2},radius:{3},bw:{4}",width,height,pixelWorldScale,radius,borderWidth);
  54. }
  55. }
  56. }