SetGray.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * 节点置灰组件
  3. * @date 20210514
  4. * @author kaka
  5. * @description 暂时不包括spine动画
  6. */
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class SetGray extends cc.Component {
  10. @property(cc.Material)
  11. private normal: cc.Material = null; //内置正常材质
  12. @property(cc.Material)
  13. private gray: cc.Material = null; //内置灰色材质
  14. @property({ type: false, displayName: "是否置灰" })
  15. public grayState: boolean = false; //默认置灰状态
  16. //上次置灰状态
  17. private lastState = false;
  18. private spArr = null;
  19. private btnArr = null;
  20. private labArr = null;
  21. private labArrC = [];
  22. private outLineArr = null;
  23. private outLineArrC = [];
  24. private labRichArr = null;
  25. private labRichArrC = [];
  26. private grayColor = new cc.Color(230, 230, 230, 255);//new cc.Color(166, 166, 166, 255);
  27. private grayOutLineColor = new cc.Color(123, 123, 123, 255);//new cc.Color(230, 230, 230, 255);
  28. private skeArr = null;
  29. onLoad() {
  30. this.spArr = this.node.getComponentsInChildren(cc.Sprite);
  31. this.btnArr = this.node.getComponentsInChildren(cc.Button);
  32. this.labArr = this.node.getComponentsInChildren(cc.Label);
  33. this.outLineArr = this.node.getComponentsInChildren(cc.LabelOutline);
  34. this.labRichArr = this.node.getComponentsInChildren(cc.RichText);
  35. this.skeArr = this.node.getComponentsInChildren(sp.Skeleton);
  36. this.setGray(this.grayState);
  37. }
  38. /**
  39. * 设置颜色
  40. * @param isGray true 置灰 false 还原正常
  41. */
  42. setGray(isGray: boolean, isInteractable = false) {
  43. if (this.lastState == isGray) {
  44. console.log('和上次设置相同')
  45. return
  46. }
  47. this.lastState = isGray;
  48. //置灰
  49. if (isGray) {
  50. //内建材质
  51. let len = this.spArr.length;
  52. for (var i = 0; i < len; i++) {
  53. this.spArr[i].setMaterial(0, this.gray);
  54. }
  55. if(!isInteractable)
  56. {
  57. let btnLen = this.btnArr.length;
  58. for (var n = 0; n < btnLen; n++) {
  59. (this.btnArr[n] as cc.Button).enableAutoGrayEffect = true;
  60. (this.btnArr[n] as cc.Button).grayMaterial = this.gray;
  61. (this.btnArr[n] as cc.Button).interactable = false;
  62. }
  63. }
  64. //设置所有文本颜色
  65. let labLen = this.labArr.length;
  66. for (var j = 0; j < labLen; j++) {
  67. this.labArrC[j] = this.labArr[j].node.color;
  68. this.labArr[j].node.color = this.grayColor;
  69. }
  70. //设置所有描边颜色
  71. let outLineLen = this.outLineArr.length;
  72. for (var k = 0; k < outLineLen; k++) {
  73. this.outLineArrC[k] = this.outLineArr[k].color;
  74. this.outLineArr[k].color = this.grayOutLineColor;
  75. }
  76. //设置所有富文本颜色
  77. let labRichLen = this.labRichArr.length;
  78. for (var m = 0; m < labRichLen; m++) {
  79. this.labRichArrC[m] = this.labRichArr[m].node.color;
  80. this.labRichArr[m].node.color = this.grayColor;
  81. }
  82. // let skeLen = this.skeArr.length;
  83. // for (var p = 0; p < skeLen; p++) {
  84. // this.skeArr[p].setMaterial(0, this.gray);
  85. // (this.skeArr[p] as any).markForRender(true);
  86. // }
  87. }
  88. else {
  89. //内建材质
  90. let len = this.spArr.length;
  91. for (var i = 0; i < len; i++) {
  92. this.spArr[i].setMaterial(0, this.normal);
  93. }
  94. if(!isInteractable)
  95. {
  96. let btnLen = this.btnArr.length;
  97. for (var n = 0; n < btnLen; n++) {
  98. (this.btnArr[n] as cc.Button).interactable = true;
  99. }
  100. }
  101. //设置所有文本颜色
  102. let labLen = this.labArr.length;
  103. for (var j = 0; j < labLen; j++) {
  104. this.labArr[j].node.color = this.labArrC[j];
  105. }
  106. //设置所有描边颜色
  107. let outLineLen = this.outLineArr.length;
  108. for (var k = 0; k < outLineLen; k++) {
  109. this.outLineArr[k].color = this.outLineArrC[k];
  110. }
  111. //设置所有富文本颜色
  112. let labRichLen = this.labRichArr.length;
  113. for (var m = 0; m < labRichLen; m++) {
  114. this.labRichArr[m].node.color = this.labRichArrC[m];
  115. }
  116. // let skeLen = this.skeArr.length;
  117. // for (var p = 0; p < skeLen; p++) {
  118. // this.skeArr[p].setMaterial(0, this.normal);
  119. // (this.skeArr[p] as any).markForRender(true);
  120. // }
  121. }
  122. }
  123. }