scene_ex.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. *场景逻辑扩展
  3. *删除选中的节点以及节点所绑定的脚本
  4. */
  5. 'use strict';
  6. var path = require('path');
  7. var fs = require('fs');
  8. const config = require('../../config');
  9. const USER_NEW_FILE_RULE = require('./panel_ex').USER_NEW_FILE_RULE;
  10. let cc_require = (fileName)=>{
  11. // 阻止报错提示
  12. let func = Editor.failed;
  13. let comp;
  14. Editor.failed = () => { }
  15. try {
  16. comp = cc.require(fileName)
  17. } catch (t) { }
  18. Editor.failed = func;
  19. return comp;
  20. }
  21. let onComplete = (saveUrl,data,node,jsFileName)=>{
  22. if(require(USER_NEW_FILE_RULE).onComplete){
  23. setTimeout(()=>{
  24. try{
  25. data = data || fs.readFileSync( Editor.remote.assetdb.urlToFspath(saveUrl)).toString();
  26. }catch(err){
  27. }
  28. require(USER_NEW_FILE_RULE).onComplete(saveUrl,data,node,jsFileName);
  29. },100)
  30. }
  31. }
  32. module.exports = {
  33. /************* 事件 *************/
  34. messages:
  35. {
  36. 'get-curr-scene-url-and-node': function (event,args,parent)
  37. {
  38. let node = args.uuid && parent.findNode(args.uuid);
  39. if (!node || args.type != "node") {
  40. Editor.info("该功能需要您选中一个节点后再执行才能创建脚本与绑定节点")
  41. event.reply(null);
  42. return
  43. }
  44. let name = node.name;
  45. parent.getCurrSceneUrl((url)=>{
  46. event.reply(null,{currNodeName:name,url,name})
  47. })
  48. },
  49. 'new-js-file': function (event, args,parent) {
  50. let node = args.uuid && parent.findNode(args.uuid);
  51. if (!node || args.type != "node") {
  52. Editor.info("该功能需要您选中一个节点后再执行才能创建脚本与绑定节点")
  53. event.reply(null, {});
  54. return
  55. }
  56. let uuid = node.uuid;
  57. let jsFileName = path.basename(args.saveUrl,path.extname(args.saveUrl));
  58. // 判断文件是否存在
  59. let comp = node.getComponent(jsFileName);
  60. if (comp) {
  61. onComplete(args.saveUrl,null,node,jsFileName)
  62. event.reply(null, {});// 回调通知结果
  63. return;
  64. }else{
  65. comp = cc_require(jsFileName);
  66. if (comp) {
  67. onComplete(args.saveUrl,null,node,jsFileName)
  68. node.addComponent(jsFileName);
  69. event.reply(null, {});// 回调通知结果
  70. return;
  71. }
  72. }
  73. let data = fs.readFileSync(args.templePath).toString();
  74. if(require(USER_NEW_FILE_RULE).getSaveText){
  75. data = require(USER_NEW_FILE_RULE).getSaveText(data,args.saveUrl,node)
  76. }
  77. // 创建文件
  78. Editor.assetdb.create(args.saveUrl, data, (err, results) => {
  79. if (err) return event.reply(null, {});
  80. // 定时检测creator加载新建文件缓存没
  81. let stop_func;
  82. let chk_count = 0
  83. let old_uuid ;
  84. stop_func = parent.setTimeoutToJS(() => {
  85. //等场景加载完脚本
  86. node = parent.findNode(uuid)
  87. if (node && !node._objFlags) {
  88. let comp = node.getComponent(jsFileName)
  89. if (comp) {
  90. // 创建脚本瞬间添加的node组件会丢失,所以需要检测3次组件确定加载了
  91. // *:组件uuid改变了说明场景已经刷新了一遍, comp.uuid != old_uuid
  92. if (chk_count++ == 3 || (old_uuid && old_uuid != comp.uuid) ) {
  93. stop_func();
  94. event.reply(null, { data: "", node_uuid: uuid, scipt_uuid: comp.__scriptUuid });
  95. Editor.Ipc.sendToPanel('simple-code', 'custom-cmd', { cmd: "openFile" });
  96. parent['scene-need-save']()
  97. onComplete(args.saveUrl,data,node,jsFileName)
  98. }
  99. } else {
  100. // 阻止报错提示
  101. let func = Editor.failed;
  102. Editor.failed = () => { }
  103. try {
  104. comp = cc.require(jsFileName)
  105. } catch (t) { }
  106. Editor.failed = func;
  107. // 添加组件
  108. if (comp) {
  109. // Editor.Ipc.sendToPanel('scene', 'scene:add-component', uuid, jsFileName); //添加不了脚本
  110. comp = node.addComponent(jsFileName);
  111. if(!old_uuid) old_uuid = comp.uuid;
  112. }
  113. }
  114. }
  115. }, 0.5, { count: 30 })
  116. }, 500)
  117. }
  118. }
  119. };