MaterialEffect.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using DG.Tweening;
  6. using System;
  7. namespace SC
  8. {
  9. public class MaterialEffect : PointEffectBase {
  10. /// <summary>
  11. /// 初始是否显示Mesh
  12. /// </summary>
  13. private bool InitMeshRenderState;
  14. private MeshRenderer _meshRenderer;
  15. protected MeshRenderer meshRenderer {
  16. get {
  17. if(_meshRenderer == null) {
  18. try {
  19. _meshRenderer = GetComponent<MeshRenderer>();
  20. } catch(Exception e) {
  21. Debug.Log(e);
  22. }
  23. }
  24. return _meshRenderer;
  25. }
  26. }
  27. public Material normalMaterial;
  28. public Material enterMaterial;
  29. public Material clickMaterial;
  30. protected override void Awake() {
  31. base.Awake();
  32. if(meshRenderer) {
  33. InitMeshRenderState = meshRenderer.enabled;
  34. }
  35. }
  36. protected override void OnEnable() {
  37. base.OnEnable();
  38. if(meshRenderer) {
  39. meshRenderer.enabled = InitMeshRenderState;
  40. if(normalMaterial) {
  41. meshRenderer.material = normalMaterial;
  42. }
  43. }
  44. }
  45. public override void OnPointerEnter(PointerEventData eventData) {
  46. base.OnPointerEnter(eventData);
  47. if(!InitMeshRenderState && meshRenderer) {
  48. meshRenderer.enabled = true;
  49. }
  50. if (meshRenderer && enterMaterial) {
  51. meshRenderer.material = enterMaterial;
  52. }
  53. }
  54. public override void OnPointerClick(PointerEventData eventData) {
  55. base.OnPointerClick(eventData);
  56. if (meshRenderer && clickMaterial) {
  57. meshRenderer.material = clickMaterial;
  58. }
  59. }
  60. public override void OnPointerExit(PointerEventData eventData) {
  61. base.OnPointerExit(eventData);
  62. if(!InitMeshRenderState && meshRenderer) {
  63. meshRenderer.enabled = false;
  64. }
  65. if (meshRenderer && normalMaterial) {
  66. meshRenderer.material = normalMaterial;
  67. }
  68. }
  69. public override void ClickFinish() {
  70. base.ClickFinish();
  71. if(meshRenderer && enterMaterial) {
  72. meshRenderer.material = enterMaterial;
  73. }
  74. }
  75. }
  76. }