Example #1
0
        public virtual void ReleaseResources(string path, object holder)
        {
            var resourcePath = SpawnConfiguration.ProcessInputPath(path, out _);

            if (!loader.IsLoaded(resourcePath))
            {
                return;
            }

            loader.Release(resourcePath, holder, false);
            if (loader.CountHolders(resourcePath) == 0)
            {
                if (IsObjectSpawned(path))
                {
                    DestroySpawnedObject(path);
                }
                loader.Release(resourcePath, holder);
            }
        }
Example #2
0
        public virtual bool DestroySpawnedObject(string path)
        {
            var spawnedObj = GetSpawnedObject(path);

            if (spawnedObj is null)
            {
                Debug.LogWarning($"Failed to destroy spawned object `{path}`: the object is not found.");
                return(false);
            }

            var removed = spawnedObjects?.Remove(spawnedObj);

            ObjectUtils.DestroyOrImmediate(spawnedObj.Object);

            var resourcePath = SpawnConfiguration.ProcessInputPath(path, out _);

            loader.Release(resourcePath, this);

            return(removed ?? false);
        }
Example #3
0
        public virtual async UniTask SpawnAsync(string path, CancellationToken cancellationToken = default, params string[] parameters)
        {
            if (IsObjectSpawned(path))
            {
                Debug.LogWarning($"Object `{path}` is already spawned and can't be spawned again before it's destroyed.");
                return;
            }

            var resourcePath   = SpawnConfiguration.ProcessInputPath(path, out _);
            var prefabResource = await loader.LoadAndHoldAsync(resourcePath, this);

            if (cancellationToken.CancelASAP)
            {
                return;
            }
            if (!prefabResource.Valid)
            {
                Debug.LogWarning($"Failed to spawn `{resourcePath}`: resource is not valid.");
                return;
            }

            var obj = Engine.Instantiate(prefabResource.Object, path);

            var spawnedObj = new SpawnedObject {
                Object = obj, State = new SpawnedObjectState(path, parameters)
            };

            spawnedObjects.Add(spawnedObj);

            var parameterized = obj.GetComponent <Commands.Spawn.IParameterized>();

            parameterized?.SetSpawnParameters(parameters);

            var awaitable = obj.GetComponent <Commands.Spawn.IAwaitable>();

            if (awaitable != null)
            {
                await awaitable.AwaitSpawnAsync(cancellationToken);
            }
        }
Example #4
0
 public virtual async UniTask HoldResourcesAsync(string path, object holder)
 {
     var resourcePath = SpawnConfiguration.ProcessInputPath(path, out _);
     await loader.LoadAndHoldAsync(resourcePath, holder);
 }