private static void SetAndroidManifestFile(MultiPlatformItem pPlatformItem)
        {
            var pFilePath = Application.dataPath + "/Plugins/Android/AndroidManifest.xml";

            if (!File.Exists(pFilePath))
            {
                Debug.LogError("AndroidManifest file not exit! " + pFilePath);
                return;
            }

            var pDocument = new XmlDocument();

            pDocument.Load(pFilePath);

            var rootNode = pDocument.SelectSingleNode("manifest");

            if (rootNode == null)
            {
                Debug.LogError("root node is null ");
                return;
            }

            var pNodeList = rootNode.SelectNodes("uses-permission");

            if (pNodeList != null)
            {
                for (var i = 0; i < pNodeList.Count; i++)
                {
                    var pElement = (XmlElement)pNodeList [i];

                    if (pElement != null && pElement.GetAttribute("android:name") == "com.android.vending.BILLING")
                    {
                        rootNode.RemoveChild(pNodeList [i]);
                    }
                }
            }

            var pNewElement = pDocument.CreateElement("Node", "uses-permission", "");

            pNewElement.SetAttribute(":android:name", "com.android.vending.BILLING");

            if (pPlatformItem.curPlatform == MultiPlatformType.Amazon)
            {
                pNewElement.SetAttribute(":tools:node", "remove");
                pNewElement.SetAttribute(":xmlns:tools", "http://schemas.android.com/tools");
            }

            rootNode.AppendChild(pNewElement);

            pDocument.Save(pFilePath);
        }
        private static BuildTargetGroup GetBuildTargetGroup(MultiPlatformItem pPlatformItem)
        {
            switch (pPlatformItem.curPlatform)
            {
            case MultiPlatformType.iOS:
                return(BuildTargetGroup.iOS);

            case MultiPlatformType.GooglePlay:
            case MultiPlatformType.Amazon:
                return(BuildTargetGroup.Android);
            }

            return(BuildTargetGroup.Unknown);
        }
        private static void SetPlatformIcons(MultiPlatformItem pPlatformItem)
        {
            var targetGroup = GetBuildTargetGroup(pPlatformItem);

            var pIconsFoldPath = Application.dataPath + "/" + pPlatformItem.iconFolder;

            if (!Directory.Exists(pIconsFoldPath))
            {
                Debug.LogError("Icon folder [ " + pPlatformItem.iconFolder + "] is not exist (图标文件夹不存在)!");
                return;
            }

            var iconFilePaths = Directory.GetFiles(pIconsFoldPath, "*.png", SearchOption.AllDirectories);

            if (iconFilePaths.Length <= 0)
            {
                Debug.LogError("There is no png icon image in (不存在png图标) [" + pPlatformItem.iconFolder + "]");
                return;
            }

            var pIconTextureList = new List <Texture2D> ();

            foreach (var pIconPath in iconFilePaths)
            {
                var assetPath = pIconPath.Substring(Application.dataPath.Length);
                assetPath = "Assets" + assetPath;

                var pIconTex = AssetDatabase.LoadAssetAtPath <Texture2D> (assetPath);
                pIconTextureList.Add(pIconTex);
            }

            var pTextureSizes    = PlayerSettings.GetIconSizesForTargetGroup(targetGroup);
            var pTargetIconArray = new Texture2D[pTextureSizes.Length];

            for (var i = 0; i < pTextureSizes.Length; i++)
            {
                pTargetIconArray [i] = null;

                foreach (var pTex in pIconTextureList)
                {
                    if (pTex.width == pTextureSizes [i])
                    {
                        pTargetIconArray [i] = pTex;
                        break;
                    }
                }
            }

            PlayerSettings.SetIconsForTargetGroup(targetGroup, pTargetIconArray);
        }
        private static void SetAndroidAPILevel(MultiPlatformItem pPlatformItem)
        {
            PlayerSettings.companyName = "Cocoplay";

#if UNITY_5_6_OR_NEWER
            var targetGroup = GetBuildTargetGroup(pPlatformItem);
            PlayerSettings.SetApplicationIdentifier(targetGroup, pPlatformItem.bundleId);
            // PlayerSettings.applicationIdentifier = pPlatformItem.bundleId;
#else
            PlayerSettings.bundleIdentifier = pPlatformItem.bundleId;
#endif

            PlayerSettings.bundleVersion = pPlatformItem.bundleVersion;

            PlayerSettings.strippingLevel = StrippingLevel.StripAssemblies;

            switch (pPlatformItem.curPlatform)
            {
            case MultiPlatformType.iOS:
                break;

            case MultiPlatformType.GooglePlay:
                break;

            case MultiPlatformType.Amazon:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }


            PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK;
            // PlayerSettings.iOS.targetOSVersion = iOSTargetOSVersion.iOS_7_0;
            PlayerSettings.iOS.allowHTTPDownload = false;
            PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.iOS, true);

            PlayerSettings.Android.minSdkVersion     = AndroidSdkVersions.AndroidApiLevel16;         //
            PlayerSettings.Android.bundleVersionCode = pPlatformItem.bundleVersionCode;

            PlayerSettings.Android.keystoreName = pPlatformItem.keystoreName;
            PlayerSettings.Android.keystorePass = pPlatformItem.keystorePass;
            PlayerSettings.Android.keyaliasName = pPlatformItem.keyaliasName;
            PlayerSettings.Android.keyaliasPass = pPlatformItem.keyaliasPass;

            PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false);
            var pApisGroup = new[] { UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2 };
            PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, pApisGroup);
        }
        private static void SetDefaultOrientation(MultiPlatformItem pPlatformItem)
        {
            switch (pPlatformItem.curPlatform)
            {
            case MultiPlatformType.iOS:
            case MultiPlatformType.GooglePlay:
                PlayerSettings.defaultInterfaceOrientation       = UIOrientation.AutoRotation;
                PlayerSettings.allowedAutorotateToLandscapeLeft  = true;
                PlayerSettings.allowedAutorotateToLandscapeRight = true;
                break;

            case MultiPlatformType.Amazon:
                PlayerSettings.defaultInterfaceOrientation      = UIOrientation.LandscapeLeft;
                PlayerSettings.allowedAutorotateToLandscapeLeft = true;
                break;
            }
        }
        public static void SetSplitBinaryOption(MultiPlatformItem pPlatformItem)
        {
            switch (pPlatformItem.curPlatform)
            {
#if UNITY_IOS
            case MultiPlatformType.iOS:
                Debug.LogFormat("GlobalSettingEditor->SetSplitBinaryOption: useOnDemandResources [{0}]", pPlatformItem.splitBinary);
                PlayerSettings.iOS.useOnDemandResources = pPlatformItem.splitBinary;
                break;
#elif UNITY_ANDROID
            case MultiPlatformType.GooglePlay:
            case MultiPlatformType.Amazon:
                Debug.LogFormat("GlobalSettingEditor->SetSplitBinaryOption: useAPKExpansionFiles [{0}]", pPlatformItem.splitBinary);
                PlayerSettings.Android.useAPKExpansionFiles = pPlatformItem.splitBinary;
                break;
#else
            default:
                Debug.Log("SetSplitBinaryOption: Current platform [" + pPlatformItem.curPlatform + "] is not support ! (当前平台不支持)");
                break;
#endif
            }
        }
        private static void SetBillingModeFile(MultiPlatformItem pPlatformItem)
        {
            var pJsonFilePath = Application.dataPath + TARGET_BILLING_MODE_JSON_PATH;

            if (!File.Exists(pJsonFilePath))
            {
                return;
            }

            var pReader       = new StreamReader(pJsonFilePath);
            var pJsonContent  = pReader.ReadToEnd();
            var pJsonFileData = JsonMapper.ToObject(pJsonContent);

            pReader.Close();

            switch (pPlatformItem.curPlatform)
            {
            case MultiPlatformType.iOS:
            case MultiPlatformType.GooglePlay:
                pJsonFileData ["androidStore"] = "GooglePlay";
                break;

            case MultiPlatformType.Amazon:
                pJsonFileData ["androidStore"] = "AmazonAppStore";
                break;
            }
            pJsonContent = pJsonFileData.ToJson();
            //		Debug.LogWarning (pJsonContent);

            var fs = new FileStream(pJsonFilePath, FileMode.Create);
            var sw = new StreamWriter(fs);

            sw.Write(pJsonContent);
            sw.Close();
            fs.Close();
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        private static void SwitchPlatformByItem(MultiPlatformItem pPlatformItem)
        {
            var targetGroup = GetBuildTargetGroup(pPlatformItem);

            if (targetGroup == BuildTargetGroup.Unknown)
            {
                Debug.LogError("SwitchPlatformByItem: Current platform [" + pPlatformItem.curPlatform + "] is not support ! (当前平台不支持)");
                return;
            }

            UnSelectAllPlatformItems();

            pPlatformItem.isSelected = true;
            if (_switchPlatform)
            {
                switch (pPlatformItem.curPlatform)
                {
                case MultiPlatformType.iOS:
                    EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
                    break;

                case MultiPlatformType.GooglePlay:
                case MultiPlatformType.Amazon:
                    EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
                    break;
                }
            }

            SetAndroidAPILevel(pPlatformItem);
            SetSplitBinaryOption(pPlatformItem);
            SetOrientationAndSplash(pPlatformItem);
            SetPlatformIcons(pPlatformItem);
            SetScriptingDefineSymbols(pPlatformItem);
            SetBillingModeFile(pPlatformItem);
            SetAndroidManifestFile(pPlatformItem);
        }
        public static void SetScriptingDefineSymbols(MultiPlatformItem pPlatformItem)
        {
            if (GlobalSettingData.Instance == null)
            {
                return;
            }

            var targetGroup = GetBuildTargetGroup(pPlatformItem);
            var changed     = false;

            // current symbols
            var currGroupSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
            var currSymbolSet    = CollectScriptingDefineSymbols(currGroupSymbols);

            // will added symbols
            var willAddedSymbolSet = CollectScriptingDefineSymbols(pPlatformItem.scriptingDefs);
            // will removed symbols
            var willRemoveGroupSymbols = new List <string> ();

            foreach (var item in GlobalSettingData.Instance.allPlatformItems)
            {
                if (item.curPlatform != pPlatformItem.curPlatform)
                {
                    willRemoveGroupSymbols.Add(item.scriptingDefs);
                }
            }
            var willRemovedSymbolSet = CollectScriptingDefineSymbols(willRemoveGroupSymbols.ToArray());

            // global debug settings
            if (CocoDebugSettingsData.Instance.IsGlobalEnabled)
            {
                willAddedSymbolSet.Add(CocoDebugSettingsData.GLOBAL_SCRIPTING_SYMBOL);
            }
            else
            {
                willRemovedSymbolSet.Add(CocoDebugSettingsData.GLOBAL_SCRIPTING_SYMBOL);
            }

            // string strLog = string.Format (" start [{0}], remove [{1}], add [{2}]",
            //  currGroupSymbols, JoinScriptingDefineSymbols (willRemovedSymbolSet),
            //  pPlatformItem.scriptingDefs);

            // remove will added from will removed list
            RemoveScriptingDefineSymbols(willRemovedSymbolSet, willAddedSymbolSet);

            // remove other platform symbols
            changed |= RemoveScriptingDefineSymbols(currSymbolSet, willRemovedSymbolSet);

            // add current platform symbols
            changed |= AddScriptingDefineSymbols(currSymbolSet, willAddedSymbolSet);

            //strLog = string.Format (" {0} - end [{1}], changed {2}", strLog, JoinScriptingDefineSymbols (currSymbolSet), changed);
            //Debug.LogError (strLog);
            if (!changed)
            {
                return;
            }

            // write new symbols
            currGroupSymbols = JoinScriptingDefineSymbols(currSymbolSet);
            PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, currGroupSymbols);
        }
 private static bool IsOrientationAllowed(MultiPlatformItem pPlatformItem, MultiPlatformItem.Orientation orientation)
 {
     return((pPlatformItem.allowOrientations & orientation) == orientation);
 }
        public static void SetOrientationAndSplash(MultiPlatformItem pPlatformItem)
        {
            var orientationCount   = 0;
            var defaultOrientation = UIOrientation.LandscapeLeft;

            if (IsOrientationAllowed(pPlatformItem, MultiPlatformItem.Orientation.PortraitUpsideDown))
            {
                PlayerSettings.allowedAutorotateToPortraitUpsideDown = true;
                orientationCount++;
                defaultOrientation = UIOrientation.PortraitUpsideDown;
            }
            else
            {
                PlayerSettings.allowedAutorotateToPortraitUpsideDown = false;
            }

            if (IsOrientationAllowed(pPlatformItem, MultiPlatformItem.Orientation.Portrait))
            {
                PlayerSettings.allowedAutorotateToPortrait = true;
                orientationCount++;
                defaultOrientation = UIOrientation.Portrait;
            }
            else
            {
                PlayerSettings.allowedAutorotateToPortrait = false;
            }

            if (IsOrientationAllowed(pPlatformItem, MultiPlatformItem.Orientation.LandscapeRight))
            {
                PlayerSettings.allowedAutorotateToLandscapeRight = true;
                orientationCount++;
                defaultOrientation = UIOrientation.LandscapeRight;
            }
            else
            {
                PlayerSettings.allowedAutorotateToLandscapeRight = false;
            }

            if (IsOrientationAllowed(pPlatformItem, MultiPlatformItem.Orientation.LandscapeLeft))
            {
                PlayerSettings.allowedAutorotateToLandscapeLeft = true;
                orientationCount++;
                defaultOrientation = UIOrientation.LandscapeLeft;
            }
            else
            {
                PlayerSettings.allowedAutorotateToLandscapeLeft = false;
            }

            switch (orientationCount)
            {
            case 0:
                // no set, use default
                SetDefaultOrientation(pPlatformItem);
                break;

            case 1:
                PlayerSettings.defaultInterfaceOrientation = defaultOrientation;
                break;

            default:
                PlayerSettings.defaultInterfaceOrientation = UIOrientation.AutoRotation;
                break;
            }

            Debug.LogFormat("GlobalSettingEditor->SetOrientationAndSplash: orientation default[{0}], left [{1}], right [{2}], up [{3}], down [{4}]",
                            PlayerSettings.defaultInterfaceOrientation,
                            PlayerSettings.allowedAutorotateToLandscapeLeft, PlayerSettings.allowedAutorotateToLandscapeRight,
                            PlayerSettings.allowedAutorotateToPortrait, PlayerSettings.allowedAutorotateToPortraitUpsideDown);

            PlayerSettings.Android.splashScreenScale = AndroidSplashScreenScale.ScaleToFill;
        }