/// <summary>
 /// Awake is called when the script instance is being loaded.
 /// </summary>
 async void Awake()
 {
     if (Scene != null && Scene.Music.Length > 0)
     {
         BGM = RandomBGM(Scene.Music);
     }
     if (BGM != null)
     {
         await LoadingScreen.Await(LoadBGM(BGM));
     }
     if (PlayOnAwake)
     {
         await PlayBGM(BGM);
     }
     else
     {
         Mediator.Global.CreateUnityContext(this).Subscribe <MatchStartEvent>(async evt => {
             await PlayBGM(BGM);
         });
     }
 }
예제 #2
0
        /// <summary>
        /// Awake is called when the script instance is being loaded.
        /// </summary>
#if UNITY_EDITOR
        void Awake()
        {
            foreach (var type in ValidImportTypes)
            {
                RegisterAll(EditorAssetUtil.LoadAll(type));
            }
#else
        async void Awake()
        {
            LoadingScreen.Await(LoadTask.Task);
            RegisterAll(GameModes);
            var paths = await GetAllValidPaths(BundleSearch);

            var bundles = paths.Select(async path => {
                var bundle = await AssetBundleManager.LoadAssetBundleAsync(path);
                var asset  = await LoadMainAsset(bundle);
                ProcessLoadedAsset(asset, path);
            });
            await Task.WhenAll(bundles);
#endif
            LoadTask.TrySetResult(new object());
            Debug.Log("Finished loading data");
        }

        void RegisterAll(IEnumerable <Object> data)
        {
            foreach (var datum in data)
            {
                Register(datum);
            }
        }

        bool Register(Object data)
        {
            foreach (var type in ValidImportTypes)
            {
                var dataObj = data as IEntity;
                if (dataObj != null && type.IsInstanceOfType(data))
                {
                    Registry.Register(type, dataObj);
                    Debug.Log($"Registered {type.Name}: {data.name} ({dataObj.Id})");
                    return(true);
                }
            }
            return(false);
        }

        async Task <IEnumerable <string> > GetAllValidPaths(string[] searchPatterns)
        {
            var allSets = await Task.WhenAll(searchPatterns.Select(pattern => AssetBundleManager.GetValidBundlePaths(pattern)));

            return(allSets.SelectMany(s => s).Distinct());
        }

        async Task <Object> LoadMainAsset(LoadedAssetBundle bundle)
        {
            var assetBundle = bundle.AssetBundle;

            if (assetBundle.mainAsset != null)
            {
                return(assetBundle.mainAsset);
            }
            else
            {
                var mainPath = assetBundle.GetAllAssetNames()[0];
                var request  = await assetBundle.LoadAssetAsync <Object>(mainPath).ToTask();

                return(request.asset);
            }
        }

        void ProcessLoadedAsset(Object asset, string path)
        {
            var identifiable = asset as IEntity;

            if (identifiable == null || !Register(asset))
            {
                Resources.UnloadAsset(asset);
                AssetBundleManager.UnloadAssetBundle(path);
            }
        }
    }