/// <summary>
        /// Update all tile objects if tile prefab data was changed and create tile objects for tiles with new prefab data.
        /// Call this method only when a tile prefab data has been changed to update changes. You may need to call UpdateMesh after this.
        /// </summary>
        public void RefreshTileObjects()
        {
            // Destroy tile objects where tile prefab is now null
            for (int i = 0; i < m_tileObjList.Count; ++i)
            {
                TileObjData tileObjData = m_tileObjList[i];
                uint        tileData    = m_tileDataList[tileObjData.tilePos];
                int         tileId      = (int)(tileData & Tileset.k_TileDataMask_TileId);
                Tile        tile        = Tileset.GetTile(tileId);
                if (tile == null || tile.prefabData.prefab == null)
                {
                    DestroyTileObject(tileObjData.tilePos);
                }
            }

            // Recreate or update all tile objects
            for (int tileIdx = 0; tileIdx < m_tileDataList.Count; ++tileIdx)
            {
                uint tileData = m_tileDataList[tileIdx];
                int  tileId   = (int)(tileData & Tileset.k_TileDataMask_TileId);
                Tile tile     = Tileset.GetTile(tileId);
                if (tile != null && tile.prefabData.prefab != null)
                {
                    CreateTileObject(tileIdx, tile.prefabData);
                }
            }
        }
        private GameObject CreateTileObject(int tileIdx, TilePrefabData tilePrefabData)
        {
            if (tilePrefabData.prefab != null)
            {
                TileObjData tileObjData = m_tileObjList.Find(x => x.tilePos == tileIdx);
                GameObject  tileObj     = null;
                int         gx          = tileIdx % m_width;
                int         gy          = tileIdx / m_width;
                if (tileObjData == null || tileObjData.tilePrefabData != tilePrefabData || tileObjData.obj == null)
                {
#if UNITY_EDITOR
                    tileObj = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(tilePrefabData.prefab);
                    // allow destroy the object with undo operations
                    if (ParentTilemap.IsUndoEnabled)
                    {
                        UnityEditor.Undo.RegisterCreatedObjectUndo(tileObj, Tilemap.k_UndoOpName + ParentTilemap.name);
                    }
#else
                    tileObj = (GameObject)Instantiate(tilePrefabData.prefab, Vector3.zero, transform.rotation);
#endif
                    _SetTileObjTransform(tileObj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]);
                    if (tileObjData != null)
                    {
                        m_tileObjToBeRemoved.Add(tileObjData.obj);
                        tileObjData.obj            = tileObj;
                        tileObjData.tilePrefabData = tilePrefabData;
                    }
                    else
                    {
                        m_tileObjList.Add(new TileObjData()
                        {
                            tilePos = tileIdx, obj = tileObj, tilePrefabData = tilePrefabData
                        });
                    }
                    tileObj.SendMessage(k_OnTilePrefabCreation,
                                        new OnTilePrefabCreationData()
                    {
                        ParentTilemap = ParentTilemap,
                        GridX         = GridPosX + gx, GridY = GridPosY + gy
                    }, SendMessageOptions.DontRequireReceiver);
                    return(tileObj);
                }
                else if (tileObjData.obj != null)
                {
                    _SetTileObjTransform(tileObjData.obj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]);
                    tileObjData.obj.SendMessage(k_OnTilePrefabCreation,
                                                new OnTilePrefabCreationData()
                    {
                        ParentTilemap = ParentTilemap,
                        GridX         = GridPosX + gx,
                        GridY         = GridPosY + gy
                    }, SendMessageOptions.DontRequireReceiver);
                    return(tileObjData.obj);
                }
            }
            return(null);
        }
        private void DestroyTileObject(int tileIdx)
        {
            TileObjData tileObjData = FindTileObjDataByTileIdx(tileIdx);

            if (tileObjData != null)
            {
                m_tileObjToBeRemoved.Add(tileObjData.obj);
                m_tileObjList.Remove(tileObjData);
            }
        }
        private void DestroyTileObject(int tileIdx)
        {
            TileObjData tileObjData = m_tileObjList.Find(x => x.tilePos == tileIdx);

            if (tileObjData != null)
            {
                m_tileObjToBeRemoved.Add(tileObjData.obj);
                m_tileObjList.Remove(tileObjData);
            }
        }
Пример #5
0
        public GameObject GetTileObject(int locGridX, int locGridY)
        {
            TileObjData tileObjData = null;

            if (locGridX >= 0 && locGridX < m_width && locGridY >= 0 && locGridY < m_height)
            {
                int tileIdx = locGridY * m_width + locGridX;
                tileObjData = FindTileObjDataByTileIdx(tileIdx);
            }
            return(tileObjData != null ? tileObjData.obj : null);
        }
 private TileObjData FindTileObjDataByTileIdx(int tileIdx)
 {
     for (int i = 0; i < m_tileObjList.Count; ++i)
     {
         TileObjData data = m_tileObjList[i];
         if (data.tilePos == tileIdx)
         {
             return(data);
         }
     }
     return(null);
 }
Пример #7
0
        private GameObject CreateTileObject(int tileIdx, TilePrefabData tilePrefabData)
        {
            if (tilePrefabData.prefab != null)
            {
                TileObjData tileObjData = FindTileObjDataByTileIdx(tileIdx);
                GameObject  tileObj     = null;
                int         gx          = tileIdx % m_width;
                int         gy          = tileIdx / m_width;
                if (tileObjData == null || tileObjData.tilePrefabData != tilePrefabData || tileObjData.obj == null)
                {
#if UNITY_EDITOR
                    GameObject sourcePrefabInstance = null;
                    if (BrushBehaviour.TileObjSourceTilemap)
                    {
                        // Use the brush instance to copy also the modified parameters
                        Vector2Int copyLoc = new Vector2Int(GridPosX + gx + BrushBehaviour.TileObjSourceTilemapOffset.x, GridPosY + gy + BrushBehaviour.TileObjSourceTilemapOffset.y);
                        sourcePrefabInstance = BrushBehaviour.TileObjSourceTilemap.GetTileObject(copyLoc.x, copyLoc.y);
                    }
                    if (sourcePrefabInstance && !EditorCompatibilityUtils.IsPrefab(sourcePrefabInstance))
                    {
                        tileObj      = (GameObject)Instantiate(sourcePrefabInstance, Vector3.zero, transform.rotation);
                        tileObj.name = tilePrefabData.prefab.name;
                    }
                    else
                    {
                        tileObj = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(tilePrefabData.prefab);
                        if (sourcePrefabInstance)
                        {
                            UnityEditor.PropertyModification[] modifications = UnityEditor.PrefabUtility.GetPropertyModifications(sourcePrefabInstance);
                            UnityEditor.PrefabUtility.SetPropertyModifications(tileObj, modifications);
                        }
                    }

                    // allow destroy the object with undo operations
                    if (ParentTilemap.IsUndoEnabled)
                    {
                        UnityEditor.Undo.RegisterCreatedObjectUndo(tileObj, STETilemap.k_UndoOpName + ParentTilemap.name);
                    }
#else
                    tileObj = (GameObject)Instantiate(tilePrefabData.prefab, Vector3.zero, transform.rotation);
#endif
                    _SetTileObjTransform(tileObj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]);
                    if (tileObjData != null)
                    {
                        m_tileObjToBeRemoved.Add(tileObjData.obj);
                        tileObjData.obj            = tileObj;
                        tileObjData.tilePrefabData = tilePrefabData;
                    }
                    else
                    {
                        m_tileObjList.Add(new TileObjData()
                        {
                            tilePos = tileIdx, obj = tileObj, tilePrefabData = tilePrefabData
                        });
                    }
                    tileObj.SendMessage(k_OnTilePrefabCreation,
                                        new OnTilePrefabCreationData()
                    {
                        ParentTilemap = ParentTilemap,
                        GridX         = GridPosX + gx, GridY = GridPosY + gy
                    }, SendMessageOptions.DontRequireReceiver);
                    return(tileObj);
                }
                else if (tileObjData.obj != null)
                {
#if UNITY_EDITOR && !UNITY_2018_3_OR_NEWER
                    //+++ Break tilemap prefab and restore tile prefab link
                    GameObject parentPrefab = UnityEditor.PrefabUtility.FindRootGameObjectWithSameParentPrefab(tileObjData.obj);
                    if (parentPrefab != tileObjData.obj)
                    {
                        DestroyImmediate(tileObjData.obj);
                        tileObjData.obj = UnityEditor.PrefabUtility.InstantiatePrefab(tileObjData.tilePrefabData.prefab) as GameObject;
                    }
                    ///---
#endif
                    _SetTileObjTransform(tileObjData.obj, gx, gy, tilePrefabData, m_tileDataList[tileIdx]);
                    tileObjData.obj.SendMessage(k_OnTilePrefabCreation,
                                                new OnTilePrefabCreationData()
                    {
                        ParentTilemap = ParentTilemap,
                        GridX         = GridPosX + gx,
                        GridY         = GridPosY + gy
                    }, SendMessageOptions.DontRequireReceiver);
                    return(tileObjData.obj);
                }
            }
            return(null);
        }