SpatialButton_Normal.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.UI;
  8. using EZXR.Glass.Inputs;
  9. using UnityEditor;
  10. using EZXR.Glass.Core;
  11. namespace EZXR.Glass.UI
  12. {
  13. /// <summary>
  14. /// 切换显示按钮背景图或颜色
  15. /// </summary>
  16. [ExecuteInEditMode]
  17. public class SpatialButton_Normal : SpatialButton
  18. {
  19. public enum Transition
  20. {
  21. None,
  22. ColorTint,
  23. TextureSwap,
  24. }
  25. public Transition transition;
  26. //public Texture2D normalTexture;
  27. public Texture2D hoverTexture;
  28. public Texture2D pressedTexture;
  29. public Texture2D disabledTexture;
  30. //public Color normalColor = Color.white;
  31. public Color hoverColor = Color.white;
  32. public Color pressedColor = Color.white;
  33. public Color disabledColor = Color.white;
  34. protected void SetMaterialOrColor(Texture2D texture, Color color)
  35. {
  36. switch (transition)
  37. {
  38. case Transition.ColorTint:
  39. SetColor(color);
  40. break;
  41. case Transition.TextureSwap:
  42. SetMaterial(texture);
  43. break;
  44. }
  45. }
  46. void SetMaterial(Texture2D texture)
  47. {
  48. //按钮图换成hover状态
  49. if (texture != null)
  50. {
  51. materialPropertyBlock.SetTexture("_MainTex", texture);
  52. }
  53. else
  54. {
  55. //重置materialPropertyBlock,因为无法直接SetTexture为null
  56. materialPropertyBlock.Clear();
  57. }
  58. _meshRenderer.SetPropertyBlock(materialPropertyBlock);
  59. }
  60. void SetColor(Color color)
  61. {
  62. materialPropertyBlock.SetColor("_Color", color);
  63. _meshRenderer.SetPropertyBlock(materialPropertyBlock);
  64. }
  65. /// <inheritdoc />
  66. public override void OnPointerHoverEnter(Spatial.InteractiveMode interactiveMode = Spatial.InteractiveMode.None)
  67. {
  68. base.OnPointerHoverEnter(interactiveMode);
  69. SetMaterialOrColor(hoverTexture, hoverColor);
  70. }
  71. public override void OnPointerHoverExit()
  72. {
  73. base.OnPointerHoverExit();
  74. SetMaterialOrColor(Texture, MainColor);
  75. }
  76. /// <inheritdoc />
  77. public override void OnPointerStartPress()
  78. {
  79. base.OnPointerStartPress();
  80. SetMaterialOrColor(pressedTexture, pressedColor);
  81. }
  82. /// <inheritdoc />
  83. public override void OnPointerUp()
  84. {
  85. base.OnPointerUp();
  86. SetMaterialOrColor(hoverTexture, hoverColor);
  87. }
  88. }
  89. }