SetGray.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(166, 166, 166, 255);
  27. private grayOutLineColor = 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) {
  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. let btnLen = this.btnArr.length;
  56. for (var n = 0; n < btnLen; n++) {
  57. (this.btnArr[n] as cc.Button).enableAutoGrayEffect = true;
  58. (this.btnArr[n] as cc.Button).grayMaterial = this.gray;
  59. (this.btnArr[n] as cc.Button).interactable = false;
  60. }
  61. //设置所有文本颜色
  62. let labLen = this.labArr.length;
  63. for (var j = 0; j < labLen; j++) {
  64. this.labArrC[j] = this.labArr[j].node.color;
  65. this.labArr[j].node.color = this.grayColor;
  66. }
  67. //设置所有描边颜色
  68. let outLineLen = this.outLineArr.length;
  69. for (var k = 0; k < outLineLen; k++) {
  70. this.outLineArrC[k] = this.outLineArr[k].color;
  71. this.outLineArr[k].color = this.grayOutLineColor;
  72. }
  73. //设置所有富文本颜色
  74. let labRichLen = this.labRichArr.length;
  75. for (var m = 0; m < labRichLen; m++) {
  76. this.labRichArrC[m] = this.labRichArr[m].node.color;
  77. this.labRichArr[m].node.color = this.grayColor;
  78. }
  79. // let skeLen = this.skeArr.length;
  80. // for (var p = 0; p < skeLen; p++) {
  81. // this.skeArr[p].setMaterial(0, this.gray);
  82. // (this.skeArr[p] as any).markForRender(true);
  83. // }
  84. }
  85. else {
  86. //内建材质
  87. let len = this.spArr.length;
  88. for (var i = 0; i < len; i++) {
  89. this.spArr[i].setMaterial(0, this.normal);
  90. }
  91. let btnLen = this.btnArr.length;
  92. for (var n = 0; n < btnLen; n++) {
  93. (this.btnArr[n] as cc.Button).interactable = true;
  94. }
  95. //设置所有文本颜色
  96. let labLen = this.labArr.length;
  97. for (var j = 0; j < labLen; j++) {
  98. this.labArr[j].node.color = this.labArrC[j];
  99. }
  100. //设置所有描边颜色
  101. let outLineLen = this.outLineArr.length;
  102. for (var k = 0; k < outLineLen; k++) {
  103. this.outLineArr[k].color = this.outLineArrC[k];
  104. }
  105. //设置所有富文本颜色
  106. let labRichLen = this.labRichArr.length;
  107. for (var m = 0; m < labRichLen; m++) {
  108. this.labRichArr[m].node.color = this.labRichArrC[m];
  109. }
  110. // let skeLen = this.skeArr.length;
  111. // for (var p = 0; p < skeLen; p++) {
  112. // this.skeArr[p].setMaterial(0, this.normal);
  113. // (this.skeArr[p] as any).markForRender(true);
  114. // }
  115. }
  116. }
  117. }