Пример #1
0
        private TilemapChunk GetOrCreateTileChunk(int gridX, int gridY, bool createIfDoesntExist = false)
        {
            if (m_dicChunkCache.Count == 0 && transform.childCount > 0)
            {
                BuildTilechunkDictionary();
            }

            int chunkX = (gridX < 0 ? (gridX + 1 - k_chunkSize) : gridX) / k_chunkSize;
            int chunkY = (gridY < 0 ? (gridY + 1 - k_chunkSize) : gridY) / k_chunkSize;

            TilemapChunk tilemapChunk = null;

            uint key = (uint)((chunkY << 16) | (chunkX & 0x0000FFFF));

            m_dicChunkCache.TryGetValue(key, out tilemapChunk);

            if (tilemapChunk == null && createIfDoesntExist)
            {
                string     chunkName = chunkX + "_" + chunkY;
                GameObject chunkObj  = new GameObject(chunkName);
                if (IsUndoEnabled)
                {
#if UNITY_EDITOR
                    Undo.RegisterCreatedObjectUndo(chunkObj, k_UndoOpName + name);
#endif
                }
                tilemapChunk = chunkObj.AddComponent <TilemapChunk>(); //NOTE: this call TilemapChunk.OnEnable before initializing the TilemapChunk. Make all changes after this.
                chunkObj.transform.parent        = transform;
                chunkObj.transform.localPosition = new Vector2(chunkX * k_chunkSize * CellSize.x, chunkY * k_chunkSize * CellSize.y);
                chunkObj.transform.localRotation = Quaternion.identity;
                chunkObj.transform.localScale    = Vector3.one;
                chunkObj.hideFlags = gameObject.hideFlags | HideFlags.HideInHierarchy; //NOTE: note the flags inheritance. BrushBehaviour object is not saved, so chunks are left orphans unless this inheritance is done
                // Reset is not called after AddComponent while in play
                if (Application.isPlaying)
                {
                    tilemapChunk.Reset();
                }
                tilemapChunk.ParentTilemap = this;
                tilemapChunk.GridPosX      = chunkX * k_chunkSize;
                tilemapChunk.GridPosY      = chunkY * k_chunkSize;
                tilemapChunk.SetDimensions(k_chunkSize, k_chunkSize);
                tilemapChunk.SetSharedMaterial(Material);
                tilemapChunk.SortingLayerID = m_sortingLayer;
                tilemapChunk.OrderInLayer   = m_orderInLayer;
                tilemapChunk.UpdateRenderProperties();

                m_dicChunkCache[key] = tilemapChunk;
            }

            return(tilemapChunk);
        }