Пример #1
0
        public static void BuildAndRunPlayer(bool developmentBuild)
        {
            //This should ask where people want to build the player
            //
            string outputPath = EditorUserBuildSettings.activeBuildTarget == BuildTarget.WebGL ? Utility.AssetBundlesOutputPath : (EditorUserBuildSettings.activeBuildTarget.ToString().IndexOf("Standalone") > -1 ? "Builds-Standalone" : "Builds-Devices");

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            //IMPORTANT Standalone Builds DELETE everything in the folder they are saved in- so building into the AssetBundles Folder DELETES ALL ASSETBUNDLES
            string[] levels = GetLevelsFromBuildSettings();
            if (levels.Length == 0)
            {
                Debug.LogWarning("There were no Scenes in you Build Settings. Adding the current active Scene.");
                levels = new string[1] {
                    UnityEngine.SceneManagement.SceneManager.GetActiveScene().path
                };
            }
            string targetName = GetBuildTargetName(EditorUserBuildSettings.activeBuildTarget);

            if (targetName == null)
            {
                return;
            }
            //For Standalone or WebGL that can run locally make the server write a file with its current setting that it can get when the game runs if the localserver is enabled
            if (SimpleWebServer.serverStarted && CanRunLocally(EditorUserBuildSettings.activeBuildTarget))
            {
                SimpleWebServer.WriteServerURL();
            }
            else if (SimpleWebServer.serverStarted && !CanRunLocally(EditorUserBuildSettings.activeBuildTarget))
            {
                Debug.LogWarning("Builds for " + EditorUserBuildSettings.activeBuildTarget.ToString() + " cannot access the LocalServer. AssetBundles will be downloaded from the remoteServerUrl's");
            }
            //BuildOptions
            BuildOptions option = BuildOptions.None;

            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.WebGL)
            {
                option = developmentBuild ? BuildOptions.Development : BuildOptions.None;
            }
            else
            {
                option = developmentBuild ? BuildOptions.Development | BuildOptions.AutoRunPlayer : BuildOptions.AutoRunPlayer;
            }
            string buildError = "";

#if UNITY_5_4 || UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0
            buildError = BuildPipeline.BuildPlayer(levels, outputPath + targetName, EditorUserBuildSettings.activeBuildTarget, option);
#else
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
            buildPlayerOptions.scenes                  = levels;
            buildPlayerOptions.locationPathName        = outputPath + targetName;
            buildPlayerOptions.assetBundleManifestPath = GetAssetBundleManifestFilePath();
            buildPlayerOptions.target                  = EditorUserBuildSettings.activeBuildTarget;
            buildPlayerOptions.options                 = option;
            buildError = BuildPipeline.BuildPlayer(buildPlayerOptions);
#endif
            //after the build completes destroy the serverURL file
            if (SimpleWebServer.serverStarted && CanRunLocally(EditorUserBuildSettings.activeBuildTarget))
            {
                SimpleWebServer.DestroyServerURLFile();
            }

            if (string.IsNullOrEmpty(buildError))
            {
                string fullPathToBuild = Path.Combine(Directory.GetParent(Application.dataPath).FullName, outputPath);
                Debug.Log("Built Successful! Build Location: " + fullPathToBuild);
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.WebGL)
                {
                    Application.OpenURL(SimpleWebServer.ServerURL + "index.html");
                }
            }
        }
        public override bool Update()
        {
            if (decryptedLoadOperation != null)
            {
                decryptedLoadOperation.Update();
                if (decryptedLoadOperation.IsDone())
                {
                    assetBundle      = decryptedLoadOperation.assetBundle;
                    downloadProgress = 1f;
                    m_WWW.Dispose();
                    m_WWW = null;
                    return(false);
                }
                else                 //keep updating
                {
                    downloadProgress = 0.9f + (decryptedLoadOperation.progress / 100);
                    return(true);
                }
            }
            else
            {
                base.Update();
            }

            // TODO: iOS AppSlicing and OnDemandResources will need something like this too
            // This checks that the download is actually happening and restarts it if it is not
            //fixes a bug in SimpleWebServer where it would randomly stop working for some reason
            if (!downloadIsDone)
            {
                //We actually need to know if progress has stalled, not just if there is none
                //so set the progress after we compare
                //downloadProgress = m_WWW.progress;
                if (!string.IsNullOrEmpty(m_WWW.error))
                {
                    if (Debug.isDebugBuild)
                    {
                        Debug.Log("[AssetBundleLoadOperation] download error for " + m_WWW.url + " : " + m_WWW.error);
                    }
                }
                else
                {
                    if (m_WWW.downloadProgress == downloadProgress)
                    {
                        zeroDownload++;
                    }
                    else
                    {
                        downloadProgress = m_WWW.downloadProgress;
                        zeroDownload     = 0;
                    }
#if UNITY_EDITOR
                    //Sometimes SimpleWebServer randomly looses it port connection
                    //Sometimes restarting the download helps, sometimes it needs to be completely restarted
                    if (SimpleWebServer.Instance != null)
                    {
                        if (zeroDownload == 150)
                        {
                            if (Debug.isDebugBuild)
                            {
                                Debug.Log("[AssetBundleLoadOperation] progress was zero for 150 frames restarting dowload");
                            }
                            m_WWW.Dispose();                            //sometimes makes a difference when the download fails
                            m_WWW = null;
#if UNITY_2018_1_OR_NEWER
                            m_WWW = UnityWebRequestAssetBundle.GetAssetBundle(m_Url);
#else
                            m_WWW = UnityWebRequest.GetAssetBundle(m_Url);
#endif
#if UNITY_2017_2_OR_NEWER
                            m_WWW.SendWebRequest();
#else
                            m_WWW.Send();
#endif
                        }

                        if (zeroDownload == 300)                        //If we are in the editor we can restart the Server and this will make it work
                        {
                            if (Debug.isDebugBuild)
                            {
                                Debug.LogWarning("[AssetBundleLoadOperation] progress was zero for 300 frames restarting the server");
                            }
                            //we wont be able to do the following from a build
                            int port = SimpleWebServer.Instance.Port;
                            SimpleWebServer.Start(port);
                            m_WWW.Dispose();
                            m_WWW = null;
#if UNITY_2018_1_OR_NEWER
                            m_WWW = UnityWebRequestAssetBundle.GetAssetBundle(m_Url);
#else
                            m_WWW = UnityWebRequest.GetAssetBundle(m_Url);
#endif
#if UNITY_2017_2_OR_NEWER
                            m_WWW.SendWebRequest();
#else
                            m_WWW.Send();
#endif
                            zeroDownload = 0;
                        }
                    }
                    else
#endif
                    if ((downloadProgress == 0 && zeroDownload == 500) || zeroDownload >= Mathf.Clamp((2500 * downloadProgress), 500, 2500))                                  //let this number get larger the more has been downloaded (cos its really annoying to be at 98% and have it fail)
                    {
                        //when we cannot download because WiFi is connected but a hotspot needs authentication
                        //or because the user has run out of mobile data the www class takes a while error out
                        if ((AssetBundleManager.ConnectionChecker != null && !AssetBundleManager.ConnectionChecker.InternetAvailable) || retryAttempts > maxRetryAttempts)
                        {
                            if (retryAttempts > maxRetryAttempts)
                            {
                                //there was some unknown error with the connection
                                //tell the user we could not complete the download
                                error = "Downloading of " + assetBundleName + " failed after 10 attempts. Something is wrong with the internet connection.";
                                m_WWW.Dispose();
                                m_WWW = null;
                            }
                            else
                            {
                                //if we have a connection checker and no connection, leave the www alone so it times out on its own
                                if (Debug.isDebugBuild)
                                {
                                    Debug.Log("[AssetBundleLoadOperation] progress was zero for " + zeroDownload + " frames and the ConnectionChecker said there was no Internet Available.");
                                }
                            }
                        }
                        else
                        {
                            if (Debug.isDebugBuild)
                            {
                                Debug.Log("[AssetBundleLoadOperation] progress was zero for " + zeroDownload + " frames restarting dowload");
                            }
                            m_WWW.Dispose();
                            m_WWW = null;
                            //m_WWW = new WWW(m_Url);//make sure this still caches
                            if (AssetBundleManager.AssetBundleIndexObject != null)
#if UNITY_2018_1_OR_NEWER
                            { m_WWW = UnityWebRequestAssetBundle.GetAssetBundle(m_Url, AssetBundleManager.AssetBundleIndexObject.GetAssetBundleHash(assetBundleName), 0); }
#else
                            { m_WWW = UnityWebRequest.GetAssetBundle(m_Url, AssetBundleManager.AssetBundleIndexObject.GetAssetBundleHash(assetBundleName), 0); }
#endif
                            else
#if UNITY_2018_1_OR_NEWER
                            { m_WWW = UnityWebRequestAssetBundle.GetAssetBundle(m_Url); }
#else
                            { m_WWW = UnityWebRequest.GetAssetBundle(m_Url); }
#endif
                            //but increment the retry either way so the failed Ui shows sooner
                            retryAttempts++;
                        }
                        zeroDownload = 0;
                    }
                }
Пример #3
0
        public override bool Update()
        {
            if (decryptedLoadOperation != null)
            {
                decryptedLoadOperation.Update();
                if (decryptedLoadOperation.IsDone())
                {
                    assetBundle      = decryptedLoadOperation.assetBundle;
                    downloadProgress = 1f;
                    m_WWW.Dispose();
                    m_WWW = null;
                    return(false);
                }
                else                 //keep updating
                {
                    downloadProgress = 0.9f + (decryptedLoadOperation.progress / 100);
                    return(true);
                }
            }
            else
            {
                base.Update();
            }

            // TODO: When can check iOS copy this into the iOS functions above
            // This checks that the download is actually happening and restarts it if it is not
            //fixes a bug in SimpleWebServer where it would randomly stop working for some reason
            if (!downloadIsDone)
            {
                downloadProgress = m_WWW.progress;
                if (!string.IsNullOrEmpty(m_WWW.error))
                {
                    Debug.Log("[AssetBundleLoadOperation] download error for " + m_WWW.url + " : " + m_WWW.error);
                }
                else
                {
                    if (m_WWW.progress == 0)
                    {
                        zeroDownload++;
                    }
#if UNITY_EDITOR
                    //Sometimes SimpleWebServer randomly looses it port connection
                    //Sometimes restarting the download helps, sometimes it needs to be completely restarted
                    if (SimpleWebServer.Instance != null)
                    {
                        if (zeroDownload == 150)
                        {
                            Debug.Log("[AssetBundleLoadOperation] progress was zero for 150 frames restarting dowload");
                            m_WWW.Dispose();                            //sometimes makes a difference when the download fails
                            m_WWW = null;
                            m_WWW = new WWW(m_Url);
                        }

                        if (zeroDownload == 300)                        //If we are in the editor we can restart the Server and this will make it work
                        {
                            Debug.LogWarning("[AssetBundleLoadOperation] progress was zero for 300 frames restarting the server");
                            //we wont be able to do the following from a build
                            int port = SimpleWebServer.Instance.Port;
                            SimpleWebServer.Start(port);
                            m_WWW.Dispose();
                            m_WWW        = null;
                            m_WWW        = new WWW(m_Url);
                            zeroDownload = 0;
                        }
                    }
                    else
#endif
                    if (zeroDownload == 500)
                    {
                        Debug.Log("[AssetBundleLoadOperation] progress was zero for 500 frames restarting dowload");
                        m_WWW.Dispose();
                        m_WWW        = null;
                        m_WWW        = new WWW(m_Url);
                        zeroDownload = 0;
                    }
                }
                return(true);
            }
            else
            {
                downloadProgress = 1f;
                return(false);
            }
        }