GrayEffectNode.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { _decorator, Component, RenderComponent, builtinResMgr, RichText, Node, Sprite } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 灰度控制脚本
  5. * @author 郑聂华
  6. */
  7. @ccclass('GrayEffectNode')
  8. export class GrayEffectNode extends Component {
  9. setGray() {
  10. this.node.children.forEach(childNode => {
  11. //childNode.getComponentsInChildren(RenderComponent).forEach(renderComponent => {
  12. // renderComponent.setMaterial(builtinResMgr.get('ui-sprite-gray-material'), 0);
  13. //});
  14. this.checkSpriteGray(childNode, true);
  15. this.checkRichTextGray(childNode);
  16. });
  17. }
  18. revert() {
  19. this.node.children.forEach(childNode => {
  20. this.checkSpriteGray(childNode, false);
  21. });
  22. // this.node.children.forEach(childNode => {
  23. // childNode.getComponentsInChildren(RenderComponent).forEach(renderComponent => {
  24. // renderComponent.setMaterial(builtinResMgr.get('ui-sprite-material'), 0);
  25. // });
  26. // });
  27. }
  28. checkRichTextGray(richTextNode: Node) {
  29. if (richTextNode.getComponent(RichText)) {
  30. let strProp = richTextNode.getComponent(RichText);
  31. let strs = strProp.string.split("#");
  32. let target = "";
  33. for (let i = 0; i < strs.length; i++) {
  34. if (i != 0) {
  35. strs[i] = "#585858" + strs[i].slice(6);
  36. }
  37. target += strs[i];
  38. }
  39. strProp.string = target;
  40. }
  41. }
  42. checkSpriteGray(tempNode: Node, isGray: boolean) {
  43. tempNode.getComponentsInChildren(Sprite).forEach(img => {
  44. img.grayscale = isGray;
  45. });
  46. }
  47. }