예제 #1
0
        private void _InitData(SceneLevelData levelData)
        {
            this.chunkWidth   = levelData.ChunkWidth;
            this.chunkHeight  = levelData.ChunkHeight;
            this.chunkColumns = levelData.ChunkColumns;
            this.chunkRows    = levelData.ChunkRows;
            this.chunks       = new SceneChunk[this.chunkColumns * this.chunkRows];

            for (var i = 0; i < levelData.Chunks.Count; ++i)
            {
                var chunkRefData = levelData.Chunks[i];
                var x            = chunkRefData.PositionX / this.chunkWidth;
                var z            = chunkRefData.PositionZ / this.chunkHeight;
                var index        = Mathf.FloorToInt(z * this.chunkColumns + x);
                var go           = new GameObject("_Chunk");
                go.transform.SetParent(this.levelRoot.transform);

                var chunk = new SceneChunk(index, chunkRefData, go, this.validateData);
                this.chunks[index] = chunk;
            }

            if (SceneLevel.IsInEdit())
            {
                var levelObject = SceneLevel.GetOrAddComponent <LevelObject>(this.levelRoot);
                levelObject.Path         = this.levelPath;
                levelObject.ChunkWidth   = this.chunkWidth;
                levelObject.ChunkHeight  = this.chunkHeight;
                levelObject.ChunkColumns = this.chunkColumns;
                levelObject.ChunkRows    = this.chunkRows;
            }
        }
예제 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="validateData">是否校验数据</param>
 /// <returns></returns>
 public static SceneLevelData Deserialize(Stream stream, bool validateData)
 {
     using (var reader = new TableStreamReader(stream, validateData)) {
         var levelData = new SceneLevelData();
         levelData._Read(reader);
         return(levelData);
     }
 }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="validateData"></param>
        /// <param name="callback"></param>
        public static void DeserializeAsync(Stream stream, bool validateData, Action <SceneLevelData> callback)
        {
            var reader    = new TableStreamAsyncReader(stream, validateData);
            var levelData = new SceneLevelData();

            levelData._ReadAsync(reader);
            reader.StartAsync(100, () =>
            {
                if (null != callback)
                {
                    callback(levelData);
                }
            });
        }
예제 #4
0
        public void Load(Vector3 position, Quaternion orientation)
        {
            using (var asset = ServiceCenter.GetService <IAssetManager>().Load(levelPath + ".bytes", AssetType.BYTES)) {
                var bytes = asset.Cast <byte[]>();
                using (var stream = new MemoryStream(bytes)) {
                    // Parse data
                    var levelData = SceneLevelData.Deserialize(stream, this.validateData);
                    _InitData(levelData);

                    _LoadAllChunks();
                    this.currentChunk = _FindChunk(position.x, position.z);
                    this.currentChunk.OnEnter();
                }
            }
        }
예제 #5
0
        public void LoadAsync(Vector3 position, Quaternion orientation, LevelLoadPolicy policy, Action handler)
        {
            this.policy = policy;

            ServiceCenter.GetService <IAssetManager>().LoadAsync(levelPath + ".bytes", AssetType.BYTES, asset =>
            {
                if (null != asset)
                {
                    var stream = new MemoryStream(asset.Cast <byte[]>());
                    SceneLevelData.DeserializeAsync(stream, this.validateData, levelData =>
                    {
                        stream.Close();
                        stream.Dispose();

                        _InitData(levelData);

                        switch (this.policy)
                        {
                        case LevelLoadPolicy.ALL:
                            _LoadAllChunksAsync(() =>
                            {
                                this.currentChunk = _FindChunk(position.x, position.z);
                                this.currentChunk.OnEnter();
                                _NotifyCompleted(handler);
                            });
                            break;

                        case LevelLoadPolicy.SURROUNDING:
                            // Start to load related chunks
                            this.currentChunk = _FindChunk(position.x, position.z);
                            if (null != this.currentChunk)
                            {
                                this.currentChunk.LoadAsync((target, message) =>
                                {
                                    this.currentChunk.OnEnter();
                                    _NotifyCompleted(handler);
                                });
                            }

                            _LoadNearChunksAsync(this.currentChunk.Index);
                            break;
                        }
                    });
                }
                asset.Dispose();
            });
        }
예제 #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="levelData"></param>
 public static void Serialize(Stream stream, SceneLevelData levelData)
 {
     using (var writer = new TableStreamWriter(stream)) {
         levelData._Write(writer);
     }
 }