/// <summary>
        /// Replaces the old platform with the updated version
        /// </summary>
        private async void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            if (!UnityGame.OnMainThread)
            {
                _ = UnityMainThreadTaskScheduler.Factory.StartNew(() => OnFileChanged(sender, e));
                return;
            }

            if (_platformManager.AllPlatforms.TryGetFirst(x => x.fullPath == e.FullPath, out CustomPlatform platform))
            {
                bool           wasActivePlatform = platform == _platformManager.ActivePlatform;
                CustomPlatform?newPlatform       = await _platformManager.CreatePlatformAsync(e.FullPath);

                if (!wasActivePlatform || newPlatform is null)
                {
                    return;
                }
                _ = _platformSpawner.ChangeToPlatformAsync(newPlatform);
            }
        }
Exemplo n.º 2
0
        private async Task <CustomPlatform> ReplaceDescriptorAsync(CustomPlatform descriptor)
        {
            CustomPlatform?platform = await _platformManager.CreatePlatformAsync(descriptor.fullPath);

            if (platform is null)
            {
                return(_platformManager.DefaultPlatform);
            }
            _platformManager.AllPlatforms.Replace(descriptor, platform);
            UnityEngine.Object.Destroy(descriptor.gameObject);
            return(platform);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Changes to a specific <see cref="CustomPlatform"/>
        /// </summary>
        /// <param name="platform">The <see cref="CustomPlatform"/> to change to</param>
        public async Task ChangeToPlatformAsync(CustomPlatform platform)
        {
            try
            {
                if (platform == _platformManager.ActivePlatform)
                {
                    return;
                }
                _cancellationTokenSource?.Cancel();
                _cancellationTokenSource?.Dispose();
                _cancellationTokenSource = new CancellationTokenSource();
                CancellationToken cancellationToken = _cancellationTokenSource.Token;

                DestroyCustomObjects();
                _platformManager.ActivePlatform.gameObject.SetActive(false);
                _platformManager.ActivePlatform = platform;

                if (platform.isDescriptor)
                {
                    CustomPlatform?newPlatform = await _platformManager.CreatePlatformAsync(platform.fullPath);

                    if (newPlatform is not null)
                    {
                        _platformManager.AllPlatforms.Replace(platform, newPlatform);
                        UnityEngine.Object.Destroy(platform.gameObject);
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    if (newPlatform is null)
                    {
                        _ = ChangeToPlatformAsync(_platformManager.DefaultPlatform);
                        return;
                    }

                    _platformManager.ActivePlatform = newPlatform;
                }

                _siraLog.Info($"Switching to {_platformManager.ActivePlatform.name}");
                _environmentHider.HideObjectsForPlatform(_platformManager.ActivePlatform);
                _platformManager.ActivePlatform.gameObject.SetActive(true);
                SpawnCustomObjects();
            }
            catch (OperationCanceledException) { }
        }