Example #1
0
    public void OnPreprocessBuild(BuildReport report)
    {
        if (Autorun.buildNotReady || AssetDatabase.IsValidFolder("Assets/_DO NOT COMMIT RIGHT NOW - Unity is using the project"))
        {
            EditorUtility.DisplayDialog("MAGIS", "MAGIS has detected that you are attempting to perform a build while it is not in a ready state. This will lead to a corrupted build.\n\nDue to Unity missing the necessary callback, it is not possible for MAGIS to cancel the build. Please manually cancel it by pressing OK in this dialog and pressing Cancel in the progress window that appears.", "OK");
            return;
        }

        // do this before build

        // if we don't call this before creating the dummy folder, unity will be in a non-compiling state
        // and the dummy folder gets removed prematurely
        AssetDatabase.SaveAssets();

        // upon building, hide unnecessary resources
        AssetDatabase.CreateFolder("Assets", "_DO NOT COMMIT RIGHT NOW - Unity is using the project");

        // to ensure that the only markers included in the built package are that of the current game,
        // we move all other markers to a temporary folder
        if (!Autorun.singleGameProject && !AssetDatabase.IsValidFolder("Assets/StreamingAssetsBackup"))
        {
            Debug.Log("Backing up StreamingAssets that are not used for this project");
            AssetDatabase.MoveAsset("Assets/StreamingAssets", "Assets/StreamingAssetsBackup");
            AssetDatabase.CreateFolder("Assets", "StreamingAssets");
            AssetDatabase.CreateFolder("Assets/StreamingAssets", "Vuforia");
            AssetDatabase.MoveAsset("Assets/StreamingAssetsBackup/Vuforia/magis-default.dat",
                                    "Assets/StreamingAssets/Vuforia/magis-default.dat");
            AssetDatabase.MoveAsset("Assets/StreamingAssetsBackup/Vuforia/magis-default.xml",
                                    "Assets/StreamingAssets/Vuforia/magis-default.xml");
            AssetDatabase.MoveAsset("Assets/StreamingAssetsBackup/Vuforia/" + DeviceInput.GameName() + ".dat",
                                    "Assets/StreamingAssets/Vuforia/" + DeviceInput.GameName() + ".dat");
            AssetDatabase.MoveAsset("Assets/StreamingAssetsBackup/Vuforia/" + DeviceInput.GameName() + ".xml",
                                    "Assets/StreamingAssets/Vuforia/" + DeviceInput.GameName() + ".xml");
        }

#if MAGIS_NOGPS && !MAGIS_BLE
        BackupFile("Assets/AREngine/Plugins/iOS/Location.mm");
#endif

#if MAGIS_BLE
        ReplaceFile("Assets/Plugins/Android/AndroidManifest.xml", "Assets/AREngine/Plugins/Android/AndroidManifestBT.xml");
#else
        BackupFile("Assets/Plugins/Android/unityandroidbluetoothlelib.jar");
        BackupFile("Assets/Plugins/iOS/UnityBluetoothLE.mm");
#endif

        AssetDatabase.Refresh();
        Debug.Log("Writing " + report.summary.platform + " build to " + report.summary.outputPath);
    }
Example #2
0
    static bool CheckIfProjectSwitchNeeded()
    {
        if (singleGameProject || EditorApplication.isPlaying || EditorApplication.isCompiling || EditorApplication.isUpdating)
        {
            // do not switch game projects if no ARGameList.txt is found or when Unity is busy doing stuff
            return(false);
        }

        if (restarting || !productNameToCloudProjectId.ContainsKey(Application.productName))
        {
            // do not waste time in Update() when we are in an invalid state
            return(true);
        }

        if (CloudProjectSettings.projectId != "" && productNameToCloudProjectId[Application.productName] != CloudProjectSettings.projectId)
        {
            if (!AssetDatabase.IsValidFolder("Assets/_DO NOT COMMIT RIGHT NOW - If Unity crashed, restart it now"))
            {
                EditorUtility.DisplayDialog("MAGIS", "The current MAGIS game has switched to '" + Application.productName + "'.\n\nRestart of Unity is required.\n\n" + "Current project id = " + CloudProjectSettings.projectId + "\nNew project id = " + productNameToCloudProjectId[Application.productName], "Restart Unity");
                EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
                AssetDatabase.SaveAssets();
                AssetDatabase.CreateFolder("Assets", "_DO NOT COMMIT RIGHT NOW - If Unity crashed, restart it now");
                AssetDatabase.Refresh();
                return(true);
            }

            restarting = true;

            StreamReader reader = new StreamReader("Assets/ARGames/ARGameList.txt");
            string[]     rows   = reader.ReadToEnd().Split('\n');
            reader.Close();
            for (int i = 0; i < rows.Length; i++)
            {
                if (rows[i].EndsWith("\r"))
                {
                    rows[i] = rows[i].Substring(0, rows[i].Length - 1);
                }
                string[] cols = rows[i].Split(',');
                if (cols[(int)ARGameList.PRODUCT_NAME] == Application.productName)
                {
                    reader = new StreamReader("ProjectSettings/ProjectSettings.asset");
                    rows   = reader.ReadToEnd().Split('\n');
                    reader.Close();

                    for (i = 0; i < rows.Length; i++)
                    {
                        if (rows[i].EndsWith("\r"))
                        {
                            rows[i] = rows[i].Substring(0, rows[i].Length - 1);
                        }

                        int    versionMajor    = System.DateTime.Now.Year - 2015; // Igpaw's first release was in 2015
                        int    versionMinor    = System.DateTime.Now.Month;
                        int    versionRevision = System.DateTime.Now.Day;
                        string versionString   = versionMajor + "." + versionMinor + "." + versionRevision;
                        int    versionCode     = versionMajor * 10000 + versionMinor * 100 + versionRevision;

                        if (rows[i].StartsWith("  bundleVersion: "))
                        {
                            rows[i] = "  bundleVersion: " + versionString;
                        }
                        if (rows[i].StartsWith("    iPhone: ") && rows[i].Length > 13 && rows[i][12] >= '0' && rows[i][12] <= '9' && rows[i][13] >= '0' && rows[i][13] <= '9')
                        {
                            rows[i] = "    iPhone: " + versionCode;
                        }
                        if (rows[i].StartsWith("  AndroidBundleVersionCode: "))
                        {
                            rows[i] = "  AndroidBundleVersionCode: " + versionCode;
                        }
                        if (rows[i].StartsWith("  productName: "))
                        {
                            rows[i] = "  productName: " + Quotify(cols[(int)ARGameList.PRODUCT_NAME]);
                        }
                        if (rows[i].StartsWith("    iPhone: ") && rows[i][12] >= 'a' && rows[i][12] <= 'z')
                        {
                            rows[i] = "    iPhone: " + cols[(int)ARGameList.IOS_ID];
                        }
                        if (rows[i].StartsWith("    Android: ") && rows[i][13] >= 'a' && rows[i][13] <= 'z')
                        {
                            rows[i] = "    Android: " + cols[(int)ARGameList.ANDROID_ID];
                        }
                        if (rows[i].StartsWith("  cloudProjectId: "))
                        {
                            rows[i] = "  cloudProjectId: " + cols[(int)ARGameList.CLOUD_PROJECT_ID];
                        }
                        if (rows[i].StartsWith("  projectName: "))
                        {
                            rows[i] = "  projectName: " + Quotify(cols[(int)ARGameList.CLOUD_PROJECT_NAME]);
                        }
                        if (rows[i].StartsWith("  AndroidKeystoreName: "))
                        {
                            if (cols[(int)ARGameList.ANDROID_KEYALIAS_NAME] == "")
                            {
                                rows[i] = "  AndroidKeystoreName: ";
                            }
                            else
                            {
                                rows[i] = "  AndroidKeystoreName: '{inproject}: " + DeviceInput.GameName() + ".keystore'";
                            }
                        }
                        if (rows[i].StartsWith("  AndroidKeyaliasName: "))
                        {
                            if (cols[(int)ARGameList.ANDROID_KEYALIAS_NAME] == "")
                            {
                                rows[i] = "  AndroidKeyaliasName: ";
                            }
                            else
                            {
                                rows[i] = "  AndroidKeyaliasName: " + Quotify(cols[(int)ARGameList.ANDROID_KEYALIAS_NAME]);
                            }
                        }
                        if (rows[i].StartsWith("  androidUseCustomKeystore: "))
                        {
                            if (cols[(int)ARGameList.ANDROID_KEYALIAS_NAME] == "")
                            {
                                rows[i] = "  androidUseCustomKeystore: 0";
                            }
                            else
                            {
                                rows[i] = "  androidUseCustomKeystore: 1";
                            }
                        }
                        if (rows[i].StartsWith("  androidSplashScreen: {fileID: 2800000, guid: "))
                        {
                            reader = new StreamReader("Assets/ARGames/" + DeviceInput.GameName() + "/Resources/LoadingScreen.png.meta");
                            string text = reader.ReadToEnd();
                            reader.Close();
                            string guid = text.Substring(text.IndexOf("guid: ") + 6, 32);
                            rows[i] = "  androidSplashScreen: {fileID: 2800000, guid: " + guid + ", type: 3}";
                        }
                        if (rows[i].StartsWith("      m_Icon: {fileID: 2800000, guid: "))
                        {
                            reader = new StreamReader("Assets/ARGames/" + DeviceInput.GameName() + "/Resources/AppIcon.png.meta");
                            string text = reader.ReadToEnd();
                            reader.Close();
                            string guid = text.Substring(text.IndexOf("guid: ") + 6, 32);
                            rows[i] = "      m_Icon: {fileID: 2800000, guid: " + guid + ", type: 3}";
                        }
                        if (rows[i].StartsWith("    4: VUFORIA_IOS_SETTINGS"))
                        {
                            rows[i] = "    4: VUFORIA_IOS_SETTINGS;MAGIS_" + DeviceInput.GameName() + (int.Parse(cols[(int)ARGameList.GPS_SUPPORT]) == 0 ? ";MAGIS_NOGPS" : "") + (int.Parse(cols[(int)ARGameList.BLE_SUPPORT]) == 0 ? "" : ";MAGIS_BLE");
                        }
                        if (rows[i].StartsWith("    7: VUFORIA_ANDROID_SETTINGS"))
                        {
                            rows[i] = "    7: VUFORIA_ANDROID_SETTINGS;MAGIS_" + DeviceInput.GameName() + (int.Parse(cols[(int)ARGameList.GPS_SUPPORT]) == 0 ? ";MAGIS_NOGPS" : "") + (int.Parse(cols[(int)ARGameList.BLE_SUPPORT]) == 0 ? "" : ";MAGIS_BLE");
                        }
                    }

                    while (rows[rows.Length - 1] == "")
                    {
                        Array.Resize(ref rows, rows.Length - 1);
                    }
                    StreamWriter writer = new StreamWriter("ProjectSettings/ProjectSettings.asset");
                    foreach (string row in rows)
                    {
                        writer.WriteLine(row);
                    }
                    writer.Close();

                    reader = new StreamReader("Assets/Resources/VuforiaConfiguration.asset");
                    rows   = reader.ReadToEnd().Split('\n');
                    reader.Close();

                    for (i = 0; i < rows.Length; i++)
                    {
                        if (rows[i].EndsWith("\r"))
                        {
                            rows[i] = rows[i].Substring(0, rows[i].Length - 1);
                        }

                        if (rows[i].StartsWith("    vuforiaLicenseKey: "))
                        {
                            rows[i] = "    vuforiaLicenseKey: " + cols[(int)ARGameList.VUFORIA_LICENSE_KEY];
                        }
                        else if (rows[i].StartsWith("    ufoLicenseKey: "))
                        {
                            rows[i] = "    ufoLicenseKey: ";
                        }
                        else if (rows[i].StartsWith("    deviceNameSetInEditor: "))
                        {
                            rows[i] = "    deviceNameSetInEditor: ";
                        }
                        else if (rows[i].StartsWith("    turnOffWebCam: "))
                        {
                            rows[i] = "    turnOffWebCam: 1";
                        }
                    }

                    while (rows[rows.Length - 1] == "")
                    {
                        Array.Resize(ref rows, rows.Length - 1);
                    }
                    writer = new StreamWriter("Assets/Resources/VuforiaConfiguration.asset");
                    foreach (string row in rows)
                    {
                        writer.WriteLine(row);
                    }
                    writer.Close();

                    writer = new StreamWriter("ProjectSettings/EditorBuildSettings.asset");
                    writer.WriteLine("%YAML 1.1");
                    writer.WriteLine("%TAG !u! tag:unity3d.com,2011:");
                    writer.WriteLine("--- !u!1045 &1");
                    writer.WriteLine("EditorBuildSettings:");
                    writer.WriteLine("  m_ObjectHideFlags: 0");
                    writer.WriteLine("  serializedVersion: 2");
                    writer.WriteLine("  m_Scenes:");
                    writer.WriteLine("  - enabled: 1");
                    writer.WriteLine("    path: Assets/AREngine/Scenes/CommonScene.unity");
                    writer.WriteLine("  - enabled: 1");
                    writer.WriteLine("    path: Assets/AREngine/Scenes/ARScene.unity");
                    writer.WriteLine("  - enabled: 1");
                    writer.WriteLine("    path: Assets/AREngine/Scenes/ARSubscene.unity");
                    writer.WriteLine("  - enabled: 1");
                    writer.WriteLine("    path: Assets/AREngine/Scenes/TitleScene.unity");
                    foreach (string scene in Directory.GetFiles("Assets/ARGames/" + DeviceInput.GameName() + "/Scenes"))
                    {
                        if (scene.EndsWith(".unity"))
                        {
                            writer.WriteLine("  - enabled: 1");
                            writer.WriteLine("    path: Assets/ARGames/" + DeviceInput.GameName() + "/Scenes/" + Path.GetFileName(scene));
                        }
                    }
                    writer.Close();

                    EditorApplication.OpenProject(Application.dataPath + "/..");
                    return(true);
                }
            }

            // if we get here, the project is not defined (but this should normally not happen!)
            EditorUtility.DisplayDialog("MAGIS", "The project name '" + Application.productName + "' is not defined in Assets/ARGames/ARGameList.txt.\n\nPlease add an entry for this project name and restart Unity.", "Close Unity");
            EditorApplication.Exit(1);
            return(true);
        }

        return(false);
    }
Example #3
0
    static void Update()
    {
        if (CheckIfProjectSwitchNeeded())
        {
            return;
        }

        if (EditorApplication.isPlaying)
        {
            if (!AssetDatabase.IsValidFolder("Assets/_DO NOT COMMIT RIGHT NOW - Unity is using the project"))
            {
                if (buildNotReady)
                {
                    // do not allow running at an inconsistent state
                    Debug.LogError("Cannot play because MAGIS project is in an inconsistent state. Please fix any issues that weren't resolved by Autorun.CleanUp() and reload the project.");
                    EditorApplication.isPlaying = false;
                    return;
                }
                buildNotReady = true;

                // upon running, hide unnecessary resources
                AssetDatabase.CreateFolder("Assets", "_DO NOT COMMIT RIGHT NOW - Unity is using the project");
                AssetDatabase.Refresh();

                // force editor to play at 1x scale or lower
                Type         type       = typeof(EditorWindow).Assembly.GetType("UnityEditor.GameView");
                EditorWindow w          = EditorWindow.GetWindow(type);
                var          areaField  = type.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                var          areaObj    = areaField.GetValue(w);
                var          scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                Vector2      value      = (Vector2)scaleField.GetValue(areaObj);
                if (value.x > 1.0f)
                {
                    scaleField.SetValue(areaObj, new Vector2(1.0f, 1.0f));
                }

                loadedLevel = null;
                if (GameObject.FindWithTag("ARMarker") != null && GameObject.FindWithTag("AREngine") == null)
                {
                    // temporarily halt the loading of an AR editor level to load CommonScene
                    loadedLevel = SceneManager.GetActiveScene().name;
                    SceneManager.LoadScene("CommonScene");
                    DeviceInput.RequestCameraPermission();
                    Debug.Log("Starting ARScene");
                }
                else if (GameObject.Find("GameState") == null)
                {
                    // for other levels that come with arengine, always run from the beginning
                    if (SceneManager.GetActiveScene().name == "ARScene" ||
                        SceneManager.GetActiveScene().name == "ARSubscene" ||
                        SceneManager.GetActiveScene().name == "CommonScene" ||
                        SceneManager.GetActiveScene().name == "TitleScene" ||
                        SceneManager.GetActiveScene().name == "MapScene" ||
                        SceneManager.GetActiveScene().name == "")
                    {
                        SceneManager.LoadScene("CommonScene");
                        Debug.Log("Starting MAGIS from the title screen");
                    }
                }
            }
            else if (buildNotReady && loadedLevel != null)
            {
                // actually load the current editor level, and also load ARScene automatically if needed
                SceneManager.LoadScene(loadedLevel);
                SceneManager.LoadScene("ARScene", LoadSceneMode.Additive);
                loadedLevel = null;
            }
        }
        else if (!EditorApplication.isPlaying && !EditorApplication.isCompiling && !EditorApplication.isUpdating)
        {
            // automatically switch target to iOS or Android if the current target is Windows, macOS, etc.
            // (doing it here intentionally because we don't want to do it during Autorun constructor)
            if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.iOS &&
                EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
            {
                buildNotReady = true;
                if ((int)System.Environment.OSVersion.Platform == 4 || (int)System.Environment.OSVersion.Platform == 6)
                {
                    EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
                }
                else
                {
                    EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
                }
                return;
            }
            else
            {
                if (!EditorUserBuildSettings.GetBuildLocation(BuildTarget.iOS).EndsWith("ios_" + DeviceInput.GameName()))
                {
                    EditorUserBuildSettings.development = true;
                }
                EditorUserBuildSettings.buildAppBundle = !EditorUserBuildSettings.development;
                EditorUserBuildSettings.SetBuildLocation(BuildTarget.iOS, "ios_" + DeviceInput.GameName());
                if (EditorUserBuildSettings.buildAppBundle)
                {
                    EditorUserBuildSettings.SetBuildLocation(BuildTarget.Android, "aab_" + DeviceInput.GameName() + ".aab");
                }
                else
                {
                    EditorUserBuildSettings.SetBuildLocation(BuildTarget.Android, "apk_" + DeviceInput.GameName() + ".apk");
                }
            }

            // fix to remove empty .fbm folders that create spurious meta files
            // (doing it here intentionally because we don't want to do it during Autorun constructor)
            foreach (string asset in AssetDatabase.FindAssets(".fbm"))
            {
                string folder = AssetDatabase.GUIDToAssetPath(asset);
                if (AssetDatabase.IsValidFolder(folder))
                {
                    if (AssetDatabase.FindAssets("t:Object", new[] { folder }).Length == 0)
                    {
                        buildNotReady = true;
                        Debug.Log("Deleting empty folder " + folder);
                        AssetDatabase.DeleteAsset(folder);
                    }
                }
            }

            // fix to remove extraneous _TerrainAutoUpgrade
            // (doing it here intentionally because we don't want to do it during Autorun constructor)
            if (AssetDatabase.IsValidFolder("Assets/_TerrainAutoUpgrade"))
            {
                buildNotReady = true;
                Debug.Log("Deleting migration folder _TerrainAutoUpgrade");
                AssetDatabase.DeleteAsset("Assets/_TerrainAutoUpgrade");
            }

            CleanUp();
        }
        else
        {
            buildNotReady = true;
        }
    }