Beispiel #1
0
 /// <summary>
 /// 强制回收所有资源
 /// </summary>
 public static void ForceUnloadAllAssets()
 {
     if (_isInitialize)
     {
         AssetSystem.ForceUnloadAllAssets();
     }
 }
Beispiel #2
0
        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="assetInfo">场景的资源信息</param>
        /// <param name="sceneMode">场景加载模式</param>
        /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
        /// <param name="priority">优先级</param>
        public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
        {
            DebugCheckInitialize();
            var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority);

            return(handle);
        }
        /// <summary>
        /// 在满足条件的前提下,销毁所有资源提供者
        /// </summary>
        public void TryDestroyAllProviders()
        {
            if (IsDone() == false)
            {
                return;
            }

            // 注意:必须等待所有Provider可以销毁的时候,才可以释放Bundle文件。
            foreach (var provider in _providers)
            {
                if (provider.CanDestroy() == false)
                {
                    return;
                }
            }

            // 除了自己没有其它引用
            if (RefCount > _providers.Count)
            {
                return;
            }

            // 销毁所有Providers
            foreach (var provider in _providers)
            {
                provider.Destroy();
            }

            // 从列表里移除Providers
            AssetSystem.RemoveBundleProviders(_providers);
            _providers.Clear();
        }
Beispiel #4
0
        /// <summary>
        /// 异步卸载子场景
        /// </summary>
        public UnloadSceneOperation UnloadAsync()
        {
            // 如果句柄无效
            if (IsValid == false)
            {
                string error     = $"{nameof(SceneOperationHandle)} is invalid.";
                var    operation = new UnloadSceneOperation(error);
                OperationSystem.StartOperaiton(operation);
                return(operation);
            }

            // 如果是主场景
            if (IsMainScene())
            {
                string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !";
                YooLogger.Error(error);
                var operation = new UnloadSceneOperation(error);
                OperationSystem.StartOperaiton(operation);
                return(operation);
            }

            // 卸载子场景
            Scene sceneObject = SceneObject;

            AssetSystem.UnloadSubScene(Provider);
            {
                var operation = new UnloadSceneOperation(sceneObject);
                OperationSystem.StartOperaiton(operation);
                return(operation);
            }
        }
Beispiel #5
0
 /// <summary>
 /// 资源回收(卸载引用计数为零的资源)
 /// </summary>
 public static void UnloadUnusedAssets()
 {
     if (_isInitialize)
     {
         AssetSystem.Update();
         AssetSystem.UnloadUnusedAssets();
     }
 }
Beispiel #6
0
        /// <summary>
        /// 获取调试信息
        /// </summary>
        internal static void GetDebugReport(DebugReport report)
        {
            if (report == null)
            {
                YooLogger.Error($"{nameof(DebugReport)} is null");
            }

            AssetSystem.GetDebugReport(report);
        }
Beispiel #7
0
        private static SubAssetsOperationHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete)
        {
            var handle = AssetSystem.LoadSubAssetsAsync(assetInfo);

            if (waitForAsyncComplete)
            {
                handle.WaitForAsyncComplete();
            }
            return(handle);
        }
Beispiel #8
0
        internal static void InternalUpdate()
        {
            // 更新异步操作系统
            OperationSystem.Update();

            // 更新下载管理系统
            DownloadSystem.Update();

            // 更新资源系统
            AssetSystem.Update();
        }
Beispiel #9
0
        public BundledProvider(AssetInfo assetInfo) : base(assetInfo)
        {
            OwnerBundle = AssetSystem.CreateOwnerAssetBundleLoader(assetInfo);
            OwnerBundle.Reference();
            OwnerBundle.AddProvider(this);

            var dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetInfo);

            DependBundleGroup = new DependAssetBundleGroup(dependBundles);
            DependBundleGroup.Reference();
        }
Beispiel #10
0
        internal static void UnloadSubScene(ProviderBase provider)
        {
            string providerGUID = provider.MainAssetInfo.ProviderGUID;

            if (_sceneHandles.ContainsKey(providerGUID) == false)
            {
                throw new Exception("Should never get here !");
            }

            // 释放子场景句柄
            _sceneHandles[providerGUID].ReleaseInternal();
            _sceneHandles.Remove(providerGUID);

            // 卸载未被使用的资源(包括场景)
            AssetSystem.UnloadUnusedAssets();

            // 检验子场景是否销毁
            if (provider.IsDestroyed == false)
            {
                throw new Exception("Should never get here !");
            }
        }
Beispiel #11
0
        internal static void UnloadAllScene()
        {
            // 释放所有场景句柄
            foreach (var valuePair in _sceneHandles)
            {
                valuePair.Value.ReleaseInternal();
            }
            _sceneHandles.Clear();

            // 卸载未被使用的资源(包括场景)
            AssetSystem.UnloadUnusedAssets();

            // 检验所有场景是否销毁
            foreach (var provider in _providers)
            {
                if (provider.IsSceneProvider())
                {
                    if (provider.IsDestroyed == false)
                    {
                        throw new Exception("Should never get here !");
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// 异步初始化
        /// </summary>
        public static InitializationOperation InitializeAsync(InitializeParameters parameters)
        {
            if (parameters == null)
            {
                throw new Exception($"YooAsset create parameters is null.");
            }

            if (parameters.LocationServices == null)
            {
                throw new Exception($"{nameof(IBundleServices)} is null.");
            }
            else
            {
                _locationServices = parameters.LocationServices;
            }

#if !UNITY_EDITOR
            if (parameters is EditorSimulateModeParameters)
            {
                throw new Exception($"Editor simulate mode only support unity editor.");
            }
#endif

            // 创建驱动器
            if (_isInitialize == false)
            {
                _isInitialize = true;
                UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]");
                driverGo.AddComponent <YooAssetDriver>();
                UnityEngine.Object.DontDestroyOnLoad(driverGo);
            }
            else
            {
                throw new Exception("YooAsset is initialized yet.");
            }

            // 检测参数范围
            if (parameters.AssetLoadingMaxNumber < 1)
            {
                parameters.AssetLoadingMaxNumber = 1;
                YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1");
            }
            if (parameters.OperationSystemMaxTimeSlice < 30)
            {
                parameters.OperationSystemMaxTimeSlice = 30;
                YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 30 milliseconds");
            }

            // 鉴定运行模式
            if (parameters is EditorSimulateModeParameters)
            {
                _playMode = EPlayMode.EditorSimulateMode;
            }
            else if (parameters is OfflinePlayModeParameters)
            {
                _playMode = EPlayMode.OfflinePlayMode;
            }
            else if (parameters is HostPlayModeParameters)
            {
                _playMode = EPlayMode.HostPlayMode;
            }
            else
            {
                throw new NotImplementedException();
            }

            // 初始化异步操作系统
            OperationSystem.Initialize(parameters.OperationSystemMaxTimeSlice);

            // 初始化下载系统
            if (_playMode == EPlayMode.HostPlayMode)
            {
#if UNITY_WEBGL
                throw new Exception($"{EPlayMode.HostPlayMode} not supports WebGL platform !");
#else
                var hostPlayModeParameters = parameters as HostPlayModeParameters;
                DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
#endif
            }

            // 初始化资源系统
            InitializationOperation initializeOperation;
            if (_playMode == EPlayMode.EditorSimulateMode)
            {
                _editorSimulateModeImpl = new EditorSimulateModeImpl();
                _bundleServices         = _editorSimulateModeImpl;
                AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
                var editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
                initializeOperation = _editorSimulateModeImpl.InitializeAsync(
                    editorSimulateModeParameters.LocationToLower,
                    editorSimulateModeParameters.SimulatePatchManifestPath);
            }
            else if (_playMode == EPlayMode.OfflinePlayMode)
            {
                _offlinePlayModeImpl = new OfflinePlayModeImpl();
                _bundleServices      = _offlinePlayModeImpl;
                AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
                initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower);
            }
            else if (_playMode == EPlayMode.HostPlayMode)
            {
                _hostPlayModeImpl = new HostPlayModeImpl();
                _bundleServices   = _hostPlayModeImpl;
                AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
                var hostPlayModeParameters = parameters as HostPlayModeParameters;
                initializeOperation = _hostPlayModeImpl.InitializeAsync(
                    hostPlayModeParameters.LocationToLower,
                    hostPlayModeParameters.ClearCacheWhenDirty,
                    hostPlayModeParameters.DefaultHostServer,
                    hostPlayModeParameters.FallbackHostServer);
            }
            else
            {
                throw new NotImplementedException();
            }

            // 监听初始化结果
            initializeOperation.Completed += InitializeOperation_Completed;
            return(initializeOperation);
        }