Beispiel #1
0
        /// <summary>
        /// 获取游戏对象
        /// </summary>
        public SpawnGameObject Spawn()
        {
            SpawnGameObject spawn;

            // 如果还未加载完毕
            if (IsDone == false)
            {
                spawn = new SpawnGameObject(this);
                _loadingSpawn.Add(spawn);
            }
            else
            {
                if (_collector.Count > 0)
                {
                    spawn = _collector.Dequeue();
                    SetSpawnCloneObject(spawn.Go);
                }
                else
                {
                    GameObject cloneObj = GameObject.Instantiate(_cloneObject);
                    spawn = new SpawnGameObject(this, cloneObj);
                    SetSpawnCloneObject(cloneObj);
                }
            }

            SpawnCount++;
            spawn.IsSpawning = true;
            return(spawn);
        }
        /// <summary>
        /// 获取游戏对象
        /// </summary>
        public SpawnGameObject Spawn(bool forceClone, params System.Object[] userDatas)
        {
            SpawnGameObject spawn;

            // 如果资源还未加载完毕
            if (IsDone == false)
            {
                spawn = new SpawnGameObject(this, userDatas);
                _loadingSpawn.Add(spawn);
            }
            else
            {
                if (forceClone == false && _cache.Count > 0)
                {
                    GameObject go = _cache.Dequeue();
                    spawn = new SpawnGameObject(this, go, userDatas);
                    SetSpawnCloneObject(go);
                }
                else
                {
                    GameObject go = InstantiateGameObject();
                    spawn = new SpawnGameObject(this, go, userDatas);
                    SetSpawnCloneObject(go);
                }
            }

            SpawnCount++;
            return(spawn);
        }
Beispiel #3
0
        /// <summary>
        /// 回收游戏对象
        /// </summary>
        public void Restore(SpawnGameObject spawn)
        {
            SpawnCount--;
            spawn.IsSpawning = false;

            // 注意:资源有可能还未加载完毕
            if (spawn.Go != null)
            {
                SetRestoreCloneObject(spawn.Go);
            }

            _collector.Enqueue(spawn);
        }
        // 丢弃游戏对象
        internal void Discard(SpawnGameObject spawn)
        {
            SpawnCount--;
            if (SpawnCount <= 0)
            {
                _lastRestoreRealTime = Time.realtimeSinceStartup;
            }

            // 注意:资源有可能还未加载完毕
            if (spawn.Go != null)
            {
                DiscardGameObject(spawn.Go);
            }
        }
Beispiel #5
0
        private void Handle_Completed(AssetOperationHandle obj)
        {
            _cloneObject = _handle.InstantiateObject;

            // 如果加载失败,创建临时对象
            if (_cloneObject == null)
            {
                _cloneObject = new GameObject(Location);
            }

            // 设置克隆对象
            SetRestoreCloneObject(_cloneObject);

            // 创建初始对象
            for (int i = 0; i < Capacity; i++)
            {
                GameObject      cloneObj = GameObject.Instantiate(_cloneObject);
                SpawnGameObject spawn    = new SpawnGameObject(this, cloneObj);
                SetRestoreCloneObject(cloneObj);
                _collector.Enqueue(spawn);
            }

            // 最后返回结果
            for (int i = 0; i < _loadingSpawn.Count; i++)
            {
                GameObject      cloneObj = GameObject.Instantiate(_cloneObject);
                SpawnGameObject spawn    = _loadingSpawn[i];
                spawn.Go = cloneObj;
                if (spawn.IsSpawning)
                {
                    SetSpawnCloneObject(cloneObj);
                    spawn.UserCallback?.Invoke(cloneObj);
                }
                else
                {
                    // 注意:直接回收
                    Restore(spawn);
                }
            }
            _loadingSpawn.Clear();
        }
        private void Handle_Completed(AssetOperationHandle obj)
        {
            // 创建初始对象
            for (int i = 0; i < InitCapacity; i++)
            {
                GameObject cloneObj = InstantiateGameObject();
                SetRestoreCloneObject(cloneObj);
                _cache.Enqueue(cloneObj);
            }

            // 最后返回结果
            for (int i = 0; i < _loadingSpawn.Count; i++)
            {
                GameObject      cloneObj = InstantiateGameObject();
                SpawnGameObject spawn    = _loadingSpawn[i];
                spawn.Go = cloneObj;

                // 注意Spawn对象的当前状态
                if (spawn.SpawnState == SpawnGameObject.ESpawnState.Restore)
                {
                    if (spawn.Go != null)
                    {
                        RestoreGameObject(spawn.Go);
                    }
                }
                else if (spawn.SpawnState == SpawnGameObject.ESpawnState.Discard)
                {
                    if (spawn.Go != null)
                    {
                        DiscardGameObject(spawn.Go);
                    }
                }
                else
                {
                    SetSpawnCloneObject(cloneObj);
                    spawn.UserCallback?.Invoke(spawn);
                }
            }
            _loadingSpawn.Clear();
        }