本文基于 Cocos Creator 2.4.1 撰寫辐赞。
文件目錄:
/**
?* !#zh
?* 這里是一些用來判斷執(zhí)行環(huán)境的宏,這些宏都是全局變量铸敏,直接訪問即可闰蛔。<br>
?* 在項目構(gòu)建時,這些宏將會被預處理并根據(jù)構(gòu)建的平臺剔除不需要的代碼香到,例如
?*
?*? ? ?if (CC_DEBUG) {
?*? ? ? ? ?cc.log('debug');
?*? ? ?}
?*? ? ?else {
?*? ? ? ? ?cc.log('release');
?*? ? ?}
?*
?* 在構(gòu)建后會只剩下
?*
?*? ? ?cc.log('release');
?*
?* <br>
?* 如需判斷腳本是否運行于指定平臺鱼冀,可以用如下表達式:
?*
?*? ? ?{
?*? ? ? ? ?"編輯器":? CC_EDITOR,
?*? ? ? ? ?"編輯器 或 預覽":? CC_DEV,
?*? ? ? ? ?"編輯器 或 預覽 或 構(gòu)建調(diào)試":? CC_DEBUG,
?*? ? ? ? ?"網(wǎng)頁預覽":? CC_PREVIEW && !CC_JSB,
?*? ? ? ? ?"模擬器預覽":? CC_PREVIEW && CC_JSB,
?*? ? ? ? ?"構(gòu)建調(diào)試":? CC_BUILD && CC_DEBUG,
?*? ? ? ? ?"構(gòu)建發(fā)行":? CC_BUILD && !CC_DEBUG,
?*? ? ?}
?*
?* !#en
?* Here are some of the macro used to determine the execution environment, these macros are global variables, can be accessed directly.<br>
?* When the project is built, these macros will be preprocessed and discard unreachable code based on the built platform, for example:
?*
?*? ? ?if (CC_DEBUG) {
?*? ? ? ? ?cc.log('debug');
?*? ? ?}
?*? ? ?else {
?*? ? ? ? ?cc.log('release');
?*? ? ?}
?*
?* After build will become:
?*
?*? ? ?cc.log('release');
?*
?* <br>
?* To determine whether the script is running on the specified platform, you can use the following expression:
?*
?*? ? ?{
?*? ? ? ? ?"editor":? CC_EDITOR,
?*? ? ? ? ?"editor or preview":? CC_DEV,
?*? ? ? ? ?"editor or preview or build in debug mode":? CC_DEBUG,
?*? ? ? ? ?"web preview":? CC_PREVIEW && !CC_JSB,
?*? ? ? ? ?"simulator preview":? CC_PREVIEW && CC_JSB,
?*? ? ? ? ?"build in debug mode":? CC_BUILD && CC_DEBUG,
?*? ? ? ? ?"build in release mode":? CC_BUILD && !CC_DEBUG,
?*? ? ?}
?*
?* @module GLOBAL-MACROS
?*/
/**
?* @property {Boolean} CC_EDITOR - Running in the editor.
?*/
/**
?* @property {Boolean} CC_PREVIEW - Preview in browser or simulator.
?*/
/**
?* @property {Boolean} CC_DEV - Running in the editor or preview.
?*/
/**
?* @property {Boolean} CC_DEBUG - Running in the editor or preview, or build in debug mode.
?*/
/**
?* @property {Boolean} CC_BUILD - Running in published project.
?*/
/**
?* @property {Boolean} CC_JSB - Running in native platform (mobile app, desktop app, or simulator).
?*/
/**
?* @property {Boolean} CC_TEST - Running in the engine's unit test.
?*/
/**
?* @property {Boolean} CC_RUNTIME - Running in runtime environments.
?*/
// window may be undefined when first load engine from editor
var _global = typeof window === 'undefined' ? global : window;
//再次聲明一個_global 沒關(guān)系报破,最終都指向了global 作為這個js的局部變量用【指向全局的global】
/*
?* @param defaultValue - The default value is only used in the editor or preview.
?*/
/* 在global下面添加默認變量的值:定義全局宏
主要有下面幾個,具體說明在下面進行列出
CC_DEV
CC_DEBUG
CC_JSB
CC_NATIVERENDERER
CC_SUPPORT_JIT
CC_PHYSICS_BUILTIN
CC_PHYSICS_CANNON
CC_EDITOR
CC_PREVIEW
CC_TEST
CC_RUNTIME
CC_JSB
*/
function defineMacro (name, defaultValue) {
? ? // if "global_defs" not preprocessed by uglify, just declare them globally,
? ? // this may happened in release version's preview page.
? ? if (typeof _global[name] === 'undefined') {
? ? ? ? _global[name] = defaultValue;
? ? }
}
/* 以下進行數(shù)據(jù)綁定千绪,當獲取name位某個平臺的時候 進行提示輸出 */
/*?
這里會在都require完事后充易,才會執(zhí)行,此時CCDebug已經(jīng)初始化荸型,所以cc.warnID可以用蔽氨。
index.js里面require('./cocos2d/core/predefine');
-->require('./asset-manager');--->require('./deprecated');--->require('../CCDirector');-->const game = require('./CCGame');
--->const debug = require('./CCDebug');
的過程里面會require
這里的require順序可以不用太關(guān)注。
*/
function defineDeprecatedMacroGetter (name, defaultValue) {
? ? if (typeof _global[name] === 'undefined') {
? ? ? ? Object.defineProperty(_global, name, {
? ? ? ? ? ? get: function () {
? ? ? ? ? ? ? ? let recommandedUsage;
? ? ? ? ? ? ? ? if (name === 'CC_WECHATGAMESUB') {
? ? ? ? ? ? ? ? ? ? recommandedUsage = 'cc.sys.platform === cc.sys.WECHAT_GAME_SUB';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (name === 'CC_WECHATGAME') {
? ? ? ? ? ? ? ? ? ? recommandedUsage = 'cc.sys.platform === cc.sys.WECHAT_GAME';? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (name === 'CC_QQPLAY') {
? ? ? ? ? ? ? ? ? ? recommandedUsage = 'cc.sys.platform === cc.sys.QQ_PLAY';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? cc.warnID(1400, name, recommandedUsage);
? ? ? ? ? ? ? ? return defaultValue;
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
function defined (name) {
? ? return typeof _global[name] === 'object';
}
// ensure CC_BUILD is defined
// should not use window.CC_BUILD because we need get global_defs defined in uglify
defineMacro('CC_BUILD', false);
// These default values can only be defined after building
// If you need to modify them
// please modify the `global_defs` in the option returned by `gulp/util/utils.js: getUglifyOptions`.
/*?
設(shè)置多個編譯宏帆疟,在構(gòu)建的時候進行裁剪代碼用的
*/
/* 拓展執(zhí)行說明:
defined('jsb')? 具體執(zhí)行? typeof jsb === 'object' 是否為 true
*/
if (CC_BUILD) {
? ? _global.CC_BUILD = CC_BUILD;
? ? _global.CC_DEV = CC_DEV;
? ? _global.CC_DEBUG = CC_DEBUG;
? ? _global.CC_JSB = CC_JSB;
? ? _global.CC_NATIVERENDERER = CC_NATIVERENDERER;
? ? _global.CC_SUPPORT_JIT = CC_SUPPORT_JIT;
? ? _global.CC_PHYSICS_BUILTIN = CC_PHYSICS_BUILTIN;
? ? _global.CC_PHYSICS_CANNON = CC_PHYSICS_CANNON;
? ? _global.CC_EDITOR = CC_EDITOR;
? ? _global.CC_PREVIEW = CC_PREVIEW;
? ? _global.CC_TEST = CC_TEST;
? ? _global.CC_RUNTIME = CC_RUNTIME;
? ? _global.CC_JSB = CC_JSB;
}
else {//這里定義的宏都是 bool類型
? ? defineMacro('CC_DEV', true); //編輯器 或 預覽? ?// (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST
? ? defineMacro('CC_DEBUG', true);//編輯器 或 預覽 或者debug模式的build? // CC_DEV || Debug Build
? ? defineMacro('CC_JSB', defined('jsb'));//CC_JSB 來判斷是否為 native 環(huán)境 了解和機器的橋接jsb==在本機平臺(移動應(yīng)用程序鹉究、桌面應(yīng)用程序或模擬器)上運行
? ? defineMacro('CC_NATIVERENDERER', defined('jsb'));//
? ? defineMacro('CC_SUPPORT_JIT', true);//是否支持jit?
? ? defineMacro('CC_PHYSICS_BUILTIN', false);//物理引擎相關(guān)
? ? defineMacro('CC_PHYSICS_CANNON', true);//物理引擎相關(guān)
? ? defineMacro('CC_EDITOR', defined('Editor') && defined('process') && ('electron' in process.versions));//編輯器內(nèi)部
? ? defineMacro('CC_PREVIEW', !CC_EDITOR);//預覽模式,
? ? defineMacro('CC_TEST', defined('tap') || defined('QUnit'));//單元測試
? ? defineMacro('CC_RUNTIME', 'function' === typeof loadRuntime);//在runtime環(huán)境中運行
? ? defineMacro('CC_JSB', defined('jsb') && !CC_RUNTIME);//為什么重復定義了一個CC_JSB 踪宠?自赔??不知道為啥歡迎補充
}
/*?
定義幾個宏判斷是微信 或者qqplay平臺
*/
// deprecated?
const WECHATGAMESUB = !!(defined('wx') && wx.getSharedCanvas);
const WECHATGAME = !!(defined('wx') && (wx.getSystemInfoSync || wx.getSharedCanvas));
const QQPLAY = defined('bk');
defineDeprecatedMacroGetter('CC_WECHATGAMESUB', WECHATGAMESUB);
/*
宏定義微信平臺 CC_WECHATGAME?
*/
defineDeprecatedMacroGetter('CC_WECHATGAME', WECHATGAME);
defineDeprecatedMacroGetter('CC_QQPLAY', QQPLAY);
if (CC_DEV) {
? ? /**
? ? ?* contains internal apis for unit tests
? ? ?* @expose
? ? ?*/
? ? cc._Test = {};
}
/**
?* @module cc
?*/
/**
?* The current version of Cocos2d being used.<br/>
?* Please DO NOT remove this String, it is an important flag for bug tracking.<br/>
?* If you post a bug to forum, please attach this flag.
?* @property {String} ENGINE_VERSION
?* 定義引擎版本號
?*/
const engineVersion = '2.4.1';
_global['CocosEngine'] = cc.ENGINE_VERSION = engineVersion;