예제 #1
0
        public static void Build()
        {
            var state = CodeBundleState.CheckAndLoad(null);

            if (state == null)
            {
                Debug.LogError("Cannot build CodeBundles without Options asset contains in Resources folder. " +
                               "Create asset from create context menu!");
                return;
            }

            var abPath = Path.GetFullPath(Application.dataPath + "/../AssetBundles");

            if (Directory.Exists(abPath))
            {
                Directory.Delete(abPath, true);
            }

            var androidPath = Path.GetFullPath(Application.dataPath +
                                               $"/../AssetBundles/{RuntimePlatform.Android.ToString()}");

            var winPath = Path.GetFullPath(Application.dataPath +
                                           $"/../AssetBundles/{RuntimePlatform.WindowsPlayer.ToString()}");

            var linuxPath = Path.GetFullPath(Application.dataPath +
                                             $"/../AssetBundles/{RuntimePlatform.LinuxPlayer.ToString()}");

            var macPath = Path.GetFullPath(Application.dataPath +
                                           $"/../AssetBundles/{RuntimePlatform.OSXPlayer.ToString()}");


            if (!state.DisableAndroid)
            {
                Build(androidPath, state.MainAssetBundleName, BuildTarget.Android, RuntimePlatform.Android);
            }

            if (!state.DisableWindows)
            {
                Build(winPath, state.MainAssetBundleName, BuildTarget.StandaloneWindows, RuntimePlatform.WindowsPlayer);
            }

            if (!state.DisableLinux)
            {
                Build(linuxPath, state.MainAssetBundleName, BuildTarget.StandaloneLinux, RuntimePlatform.LinuxPlayer);
            }

            if (!state.DisableOSX)
            {
                Build(macPath, state.MainAssetBundleName, BuildTarget.StandaloneOSX, RuntimePlatform.OSXPlayer);
            }

            File.WriteAllText(Application.dataPath + "/../AssetBundles/version.json", JsonUtility.ToJson(
                                  new AssetBundleVersionState()
            {
                hash = DateTime.UtcNow.ToString()
            }));

            Debug.Log("[CodeBundle building] build finished");
        }
예제 #2
0
        private IEnumerator Start()
        {
            if ((State = CodeBundleState.CheckAndLoad(State)) == null)
            {
                OnError.Invoke();
                yield break;
            }

            if (Application.isEditor)
            {
                yield break;
            }

            var waiter         = (object)new WaitForSeconds(CheckIntervalInSeconds);
            var countOfRetries = 0;

            while (true)
            {
                var url = new Uri(new Uri(State.RemoteUrl), "/version.json");
                using (var request = UnityWebRequest.Get(url.ToString()))
                {
                    yield return(request.SendWebRequest());

                    if (request.isNetworkError || request.isHttpError)
                    {
                        Debug.LogError(
                            $"Asset Bundle version request failed. Code:{request.responseCode}, error: {request.error}");
                        if (countOfRetries >= RetryCount)
                        {
                            Debug.LogError("Too much request retries for version check!");
                            OnRemoteRequestRetrySeriesError.Invoke();
                        }

                        OnRemoteRequestSingleError.Invoke();

                        countOfRetries++;
                        continue;
                    }

                    countOfRetries = 0;

                    var versionState = JsonUtility.FromJson <AssetBundleVersionState>(request.downloadHandler.text);

                    if (!versionState.Equals(State.CurrentRemoteState))
                    {
                        State.CurrentRemoteState = versionState;
                        Debug.Log("Found new version");
                        OnRemoteDetectChanges.Invoke();
                    }
                }

                yield return(waiter);
            }
        }
예제 #3
0
        public void Load()
        {
            if ((State = CodeBundleState.CheckAndLoad(State)) == null)
            {
                OnError.Invoke();
                return;
            }

            if (!Application.isEditor || ForceUseAssetBundles)
            {
                LoadFromAssetBundle();
            }
            else
            {
                LoadFromResources();
            }
        }
예제 #4
0
        /// <summary>
        ///     Load state asset or just check that argument is not null
        /// </summary>
        public static CodeBundleState CheckAndLoad(CodeBundleState state)
        {
            if (state != null)
            {
                return(state);
            }

            Debug.Log("State asset not set. Try find in resources");
            state = Resources.FindObjectsOfTypeAll <CodeBundleState>().FirstOrDefault();
            if (state == null)
            {
                Debug.LogError("State asset asset not found in resources. " +
                               "Create it from context menu in any Resources folder and configure");
            }
            else
            {
                Debug.Log($"State asset found. Use asset with name `{state.name}`");
            }

            return(state);
        }
예제 #5
0
        private IEnumerator Loading()
        {
            if ((State = CodeBundleState.CheckAndLoad(State)) == null)
            {
                OnError.Invoke();
                yield break;
            }

            var platformPrefix = Application.platform.ToString();

            if (!Application.isEditor || ForceUseAssetBundle)
            {
                var assetBundleRequests = new Queue <string>();
                assetBundleRequests.Enqueue(State.MainAssetBundleName);

                while (assetBundleRequests.Any())
                {
                    var bundleName = assetBundleRequests.Dequeue();
                    Debug.Log("Loading asset bundle " + bundleName);
                    using (var abRequest =
                               UnityWebRequestAssetBundle.GetAssetBundle($"{State.RemoteUrl}{platformPrefix}/{bundleName}"))
                    {
                        yield return(abRequest.SendWebRequest());

                        if (abRequest.isNetworkError || abRequest.isHttpError)
                        {
                            Debug.LogError(
                                $"Asset Bundle load request failed. Code:{abRequest.responseCode}, error: {abRequest.error}");
                            OnError.Invoke();
                            yield break;
                        }

                        var assetBundle = DownloadHandlerAssetBundle.GetContent(abRequest);
                        State.AssetBundles[bundleName] = assetBundle;

                        if (assetBundle.isStreamedSceneAssetBundle)
                        {
                            continue;
                        }

                        var manifest = assetBundle.LoadAsset <AssetBundleManifest>("assetbundlemanifest");

                        if (manifest == null)
                        {
                            continue;
                        }

                        foreach (var depenedetAsset in manifest.GetAllAssetBundles())
                        {
                            assetBundleRequests.Enqueue(depenedetAsset);
                        }
                    }
                }

                foreach (var assetBundle in State.AssetBundles.Values)
                {
                    foreach (var assetName in assetBundle.GetAllAssetNames())
                    {
                        if (DebugMode)
                        {
                            Debug.Log($"asset `{assetName}` place in {assetBundle.name}");
                        }

                        State.AssetBundles[assetName] = assetBundle;
                    }
                }
            }

            Debug.Log("Loading finish");

            OnLoaded.Invoke();

            yield return(null);
        }