const EPlatform = cc.Enum( { Unknown : 0, WebBrowser_Mobile : 1, WebBrowser_Fixed : 2, Native_Android : 11, Native_IOS : 12, WindowsOrEditor : 15, Other : 999 }); const PlatformName = { "0": "Unknown", "1": "WebBrowser_Mobile", "2": "WebBrowser_Fixed", "11": "Native_Android", "12": "Native_IOS", "15": "WindowsOrEditor", "999": "Other", }; let CMiscHelp = cc.Class({ statics: { instance: null }, ctor: function () { this.mPlatform = EPlatform.Unknown; }, getPlatform : function() { if ( EPlatform.Unknown !== this.mPlatform ) { return this.mPlatform; } if ( cc.sys.isBrowser ) { this.mPlatform = ( cc.sys.OS_ANDROID === cc.sys.os || cc.sys.OS_IOS === cc.sys.os ) ? EPlatform.WebBrowser_Mobile : EPlatform.WebBrowser_Fixed; } else { if( cc.sys.isNative ) { if( cc.sys.isMobile ) { if ( cc.sys.OS_ANDROID === cc.sys.os ) { this.mPlatform = EPlatform.Native_Android; } else if ( cc.sys.OS_IOS === cc.sys.os ) { this.mPlatform = EPlatform.Native_IOS; } else { this.mPlatform = EPlatform.Other; } } else { this.mPlatform = EPlatform.WindowsOrEditor; } } else { this.mPlatform = EPlatform.Other; } } return this.mPlatform; }, /** * 获取当前平台名 * @return {string} */ getPlatformName: function() { let pt = this.getPlatform(); return PlatformName[pt] || PlatformName[0]; }, /** * 是否浏览器环境 * @return {boolean} */ isBrowser : function() { return this.isMobileBrowser() || this.isWin32WebBrowser(); }, /** * 是否移动平台的浏览器 * @return {boolean} */ isMobileBrowser: function() { return EPlatform.WebBrowser_Mobile === this.getPlatform(); }, /** * 是否 win32,Mac等 非移动平台的浏览器 * @return {boolean} */ isWin32WebBrowser : function () { return EPlatform.WebBrowser_Fixed === this.getPlatform(); }, isNative : function () { return this.isNativeAndroid() || this.isNativeIOS(); }, /** * 是否 Android 源生环境 * @return {boolean} */ isNativeAndroid : function() { return EPlatform.Native_Android === this.getPlatform(); }, /** * 是否 IOS 源生环境 * @return {boolean} */ isNativeIOS : function() { return EPlatform.Native_IOS === this.getPlatform(); }, /** * 是否 Win32,Mac,编辑器 等源生环境 * @return {boolean} */ isWindowsOrEditor: function() { return EPlatform.WindowsOrEditor === this.getPlatform(); }, /** * @return {boolean} */ isValid : function( target ) { return undefined !== target && null !== target; }, /** * @return {boolean} */ isValidString : function( targetString ) { return undefined !== targetString && null !== targetString && 0 !== targetString['length']; }, /** * @return {cc.Node} */ getValidNode : function( node ) { if( !node ) { return null; } let realNodeVal = null; if( node instanceof cc.Node ) { realNodeVal = node; } else if( node instanceof cc.Component ) { realNodeVal = node.node; } else { return null; } if ( realNodeVal && realNodeVal.isValid ) { return realNodeVal; } return null; }, /** * @return {boolean} */ isValidNode( node ) { return ( null != this.getValidNode( node ) ); }, }); CMiscHelp.getInstance = function () { if (!CMiscHelp.instance) { CMiscHelp.instance = new CMiscHelp(); } return CMiscHelp.instance; } module.exports = CMiscHelp;