public bool is_show_prefab_preview_in_tile_palette; //是否在tile_palette面板中显示预设预览图 public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj.GetType() != this.GetType()) { return(false); } TilePrefabData other = (TilePrefabData)obj; return((other.prefab == this.prefab) && (other.offset == this.offset) && (other.offsetMode == this.offsetMode)); }
private GameObject CreateTileObject(int tile_index, TilePrefabData tilePrefabData) { if (tilePrefabData.prefab != null) { TileObjData tileObjData = FindTileObjDataByTileIndex(tile_index); GameObject tile_gameObject = null; int grid_x = tile_index % width; int grid_y = tile_index / width; if (tileObjData == null || tileObjData.tilePrefabData != tilePrefabData || tileObjData.gameObject == null) { #if UNITY_EDITOR tile_gameObject = (GameObject)PrefabUtility.InstantiatePrefab(tilePrefabData.prefab); // allow destroy the object with undo operations if (parent_tileMap.is_undo_enabled) { Undo.RegisterCreatedObjectUndo(tile_gameObject, TileMapConst.Undo_Operation_Name + parent_tileMap.name); } #else tile_gameObject = (GameObject)Instantiate(tilePrefabData.prefab, Vector3.zero, transform.rotation); #endif SetTileObjTransform(tile_gameObject, grid_x, grid_y, tilePrefabData, tileData_list[tile_index]); if (tileObjData != null) { tile_gameObject_to_be_removed_list.Add(tileObjData.gameObject); tileObjData.gameObject = tile_gameObject; tileObjData.tilePrefabData = tilePrefabData; } else { tileObjData_list.Add(new TileObjData() { index = tile_index, gameObject = tile_gameObject, tilePrefabData = tilePrefabData }); } tile_gameObject.SendMessage(TileMapConst.OnTilePrefabCreation, new OnTilePrefabCreationData() { parent_tileMap = parent_tileMap, grid_x = offset_grid_x + grid_x, grid_y = offset_grid_y + grid_y }, SendMessageOptions.DontRequireReceiver); return(tile_gameObject); } else if (tileObjData.gameObject != null) { #if UNITY_EDITOR //+++ Break tilemap prefab and restore tile prefab link GameObject parentPrefab = PrefabUtility.FindRootGameObjectWithSameParentPrefab(tileObjData.gameObject); if (parentPrefab != tileObjData.gameObject) { DestroyImmediate(tileObjData.gameObject); tileObjData.gameObject = PrefabUtility.InstantiatePrefab(tileObjData.tilePrefabData.prefab) as GameObject; } #endif SetTileObjTransform(tileObjData.gameObject, grid_x, grid_y, tilePrefabData, tileData_list[tile_index]); tileObjData.gameObject.SendMessage(TileMapConst.OnTilePrefabCreation, new OnTilePrefabCreationData() { parent_tileMap = parent_tileMap, grid_x = offset_grid_x + grid_x, grid_y = offset_grid_y + grid_y }, SendMessageOptions.DontRequireReceiver); return(tileObjData.gameObject); } } return(null); }
private void SetTileObjTransform(GameObject tile_gameObject, int grid_x, int grid_y, TilePrefabData tilePrefabData, uint tileData) { // 比如格子[0,0],中点坐标应该是[0.5 * cell_size.x,0.5 * cell_size.y] Vector3 chunk_localPosition = new Vector3((grid_x + .5f) * cell_size.x, (grid_y + .5f) * cell_size.y, tilePrefabData.prefab.transform.position.z); if (tilePrefabData.offsetMode == TilePrefabDataOffsetMode.Pixels) { float ppu = tileSet.tile_pixel_size.x / cell_size.x; chunk_localPosition += tilePrefabData.offset / ppu; } else //if (tilePrefabData.offsetMode == TileOffsetMode.Unit) { chunk_localPosition += tilePrefabData.offset; } Vector3 worldPosition = transform.TransformPoint(chunk_localPosition); tile_gameObject.transform.position = worldPosition; tile_gameObject.transform.rotation = transform.rotation; tile_gameObject.transform.parent = transform.parent; tile_gameObject.transform.localRotation = tilePrefabData.prefab.transform.localRotation; tile_gameObject.transform.localScale = tilePrefabData.prefab.transform.localScale; //+++ Apply tile flags Vector3 localScale = tile_gameObject.transform.localScale; if (TileSetUtil.IsTileFlagRot90(tileData)) { tile_gameObject.transform.localRotation *= Quaternion.Euler(0, 0, -90); } //For Rot180 and Rot270 avoid changing the scale if (TileSetUtil.IsTileFlagFlipH(tileData) && TileSetUtil.IsTileFlagFlipV(tileData)) { tile_gameObject.transform.localRotation *= Quaternion.Euler(0, 0, -180); } else { if (TileSetUtil.IsTileFlagFlipH(tileData)) { localScale.x = -tile_gameObject.transform.localScale.x; } if (TileSetUtil.IsTileFlagFlipV(tileData)) { localScale.y = -tile_gameObject.transform.localScale.y; } } tile_gameObject.transform.localScale = localScale; }
private void DisplayPrefab() { if (tileSet.selected_tileSetBrushId != TileSetConst.TileSetBrushId_Default) { EditorGUILayout.LabelField("TileSetBrush tile不能被编辑", EditorStyles.boldLabel); } else { bool is_multi_selection = tileSet.tileSelection != null; Tile selected_tile = is_multi_selection ? tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[0])] : tileSet.selected_tile; GUILayoutUtility.GetRect(1, 1, GUILayout.Width(tileSet.visual_tile_size.x), GUILayout.Height(tileSet.visual_tile_size.y));//Pos1 Rect tile_uv = selected_tile.uv; GUI.color = tileSet.background_color; GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), EditorGUIUtility.whiteTexture, tile_uv, true); //即在Pos1处画图片 GUI.color = Color.white; GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), tileSet.atlas_texture, tile_uv, true); //即在Pos1处画图片 if (is_multi_selection) { EditorGUILayout.LabelField("* Multi-selection Edition", EditorStyles.boldLabel); } using (var check1 = new EditorGUIBeginChangeCheckScope()) { TilePrefabData tilePrefabData = selected_tile.tilePrefabData; float saved_label_width = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 80; //offset tilePrefabData.offset = EditorGUILayout.Vector3Field("offset", tilePrefabData.offset); tilePrefabData.offsetMode = (TilePrefabDataOffsetMode)EditorGUILayout.EnumPopup("offset mode", tilePrefabData.offsetMode); //prefab bool is_prefab_changed = false; using (var check2 = new EditorGUIBeginChangeCheckScope()) { tilePrefabData.prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", tilePrefabData.prefab, typeof(GameObject), false); if (check2.IsChanged) { is_prefab_changed = true; } } //prefab preview Texture2D prefab_preview; using (new GUILayoutBeginHorizontalScope()) { prefab_preview = AssetPreview.GetAssetPreview(selected_tile.tilePrefabData.prefab); GUILayout.Box(prefab_preview, prefab_preview != null ? (GUIStyle)"Box" : GUIStyle.none); } if (tilePrefabData.prefab) { EditorGUIUtility.labelWidth = 260; if (prefab_preview) { tilePrefabData.is_show_prefab_preview_in_tile_palette = EditorGUILayout.Toggle("在tile palette中显示预设", tilePrefabData.is_show_prefab_preview_in_tile_palette); } EditorGUIUtility.labelWidth = saved_label_width; tilePrefabData.is_show_tile_with_prefab = EditorGUILayout.Toggle("Show Tile With Prefab", tilePrefabData.is_show_tile_with_prefab); } if (check1.IsChanged) { Undo.RecordObject(tileSet, "Tile Prefab Data Changed"); if (is_multi_selection) { for (int i = 0; i < tileSet.tileSelection.selection_tileData_list.Count; ++i) { Tile tile = tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[i])]; GameObject saved_prefab = tile.tilePrefabData.prefab; tile.tilePrefabData = tilePrefabData; if (!is_prefab_changed) { tile.tilePrefabData.prefab = saved_prefab; } } } else { selected_tile.tilePrefabData = tilePrefabData; } EditorUtility.SetDirty(tileSet); } } } }