void OnWillRenderObject() { if (!ParentTilemap.Tileset) { return; } if (ParentTilemap.PixelSnap && ParentTilemap.Material.HasProperty("PixelSnap")) { Material matCopyWithPixelSnap; if (!s_dicMaterialCopyWithPixelSnap.TryGetValue(ParentTilemap.Material, out matCopyWithPixelSnap)) { matCopyWithPixelSnap = new Material(ParentTilemap.Material); matCopyWithPixelSnap.name += "_pixelSnapCopy"; matCopyWithPixelSnap.hideFlags = HideFlags.DontSave; matCopyWithPixelSnap.EnableKeyword("PIXELSNAP_ON"); matCopyWithPixelSnap.SetFloat("PixelSnap", 1f); s_dicMaterialCopyWithPixelSnap[ParentTilemap.Material] = matCopyWithPixelSnap; } m_meshRenderer.sharedMaterial = matCopyWithPixelSnap; } else { m_meshRenderer.sharedMaterial = ParentTilemap.Material; } UpdateMaterialPropertyBlock(); if (m_animatedTiles.Count > 0) //TODO: add fps attribute to update animated tiles when necessary { Dictionary <TilesetBrush, Vector2[]> animTileCache = new Dictionary <TilesetBrush, Vector2[]>(); for (int i = 0; i < m_animatedTiles.Count; ++i) { AnimTileData animTileData = m_animatedTiles[i]; Vector2[] uvs; if (!animTileCache.TryGetValue(animTileData.Brush, out uvs)) { //NOTE: GetAnimTileData will be called only once per brush, because after this call, the brush will be in the cache dictionary uint tileData = animTileData.Brush.GetAnimTileData(); Rect tileUV = animTileData.Brush.GetAnimUV(); bool flipH = (tileData & Tileset.k_TileFlag_FlipH) != 0; bool flipV = (tileData & Tileset.k_TileFlag_FlipV) != 0; bool rot90 = (tileData & Tileset.k_TileFlag_Rot90) != 0; //NOTE: xMinMax and yMinMax is opposite if width or height is negative float u0 = tileUV.xMin + Tileset.AtlasTexture.texelSize.x * InnerPadding; float v0 = tileUV.yMin + Tileset.AtlasTexture.texelSize.y * InnerPadding; float u1 = tileUV.xMax - Tileset.AtlasTexture.texelSize.x * InnerPadding; float v1 = tileUV.yMax - Tileset.AtlasTexture.texelSize.y * InnerPadding; if (flipV) { float v = v0; v0 = v1; v1 = v; } if (flipH) { float u = u0; u0 = u1; u1 = u; } uvs = new Vector2[4]; if (rot90) { uvs[0] = new Vector2(u1, v0); uvs[1] = new Vector2(u1, v1); uvs[2] = new Vector2(u0, v0); uvs[3] = new Vector2(u0, v1); } else { uvs[0] = new Vector2(u0, v0); uvs[1] = new Vector2(u1, v0); uvs[2] = new Vector2(u0, v1); uvs[3] = new Vector2(u1, v1); } animTileCache[animTileData.Brush] = uvs; } m_uv[animTileData.VertexIdx + 0] = uvs[0]; m_uv[animTileData.VertexIdx + 1] = uvs[1]; m_uv[animTileData.VertexIdx + 2] = uvs[2]; m_uv[animTileData.VertexIdx + 3] = uvs[3]; } m_meshFilter.sharedMesh.uv = m_uv.ToArray(); } }
void OnWillRenderObject() { if (!ParentTilemap.Tileset) { return; } if (ParentTilemap.PixelSnap && ParentTilemap.Material.HasProperty("PixelSnap")) { Material matCopyWithPixelSnap; if (!s_dicMaterialCopyWithPixelSnap.TryGetValue(ParentTilemap.Material, out matCopyWithPixelSnap)) { matCopyWithPixelSnap = new Material(ParentTilemap.Material); matCopyWithPixelSnap.name += "_pixelSnapCopy"; matCopyWithPixelSnap.hideFlags = HideFlags.DontSave; matCopyWithPixelSnap.EnableKeyword("PIXELSNAP_ON"); matCopyWithPixelSnap.SetFloat("PixelSnap", 1f); s_dicMaterialCopyWithPixelSnap[ParentTilemap.Material] = matCopyWithPixelSnap; } m_meshRenderer.sharedMaterial = matCopyWithPixelSnap; } else { m_meshRenderer.sharedMaterial = ParentTilemap.Material; } UpdateMaterialPropertyBlock(); if (m_animatedTiles.Count > 0) //TODO: add fps attribute to update animated tiles when necessary { for (int i = 0; i < m_animatedTiles.Count; ++i) { AnimTileData animTileData = m_animatedTiles[i]; Vector2[] uvs = animTileData.Brush.GetAnimUVWithFlags(InnerPadding); if (animTileData.SubTileIdx >= 0) { for (int j = 0; j < 4; ++j) { if (j == animTileData.SubTileIdx) { m_uv[animTileData.VertexIdx + j] = uvs[j]; } else { m_uv[animTileData.VertexIdx + j] = (uvs[j] + uvs[animTileData.SubTileIdx]) / 2f; } } } else { m_uv[animTileData.VertexIdx + 0] = uvs[0]; m_uv[animTileData.VertexIdx + 1] = uvs[1]; m_uv[animTileData.VertexIdx + 2] = uvs[2]; m_uv[animTileData.VertexIdx + 3] = uvs[3]; } } if (m_meshFilter.sharedMesh) #if UNITY_5_0 || UNITY_5_1 { m_meshFilter.sharedMesh.uv = m_uv.ToArray(); } #else { m_meshFilter.sharedMesh.SetUVs(0, m_uv); } #endif } }
void OnWillRenderObject() { if (!parent_tileMap.tileSet) { return; } if (parent_tileMap.is_pixel_snap && parent_tileMap.material.HasProperty("PixelSnap")) { Material material_copy_with_pixel_snap; if (!material_copy_with_pixel_snap_dict.TryGetValue(parent_tileMap.material, out material_copy_with_pixel_snap)) { material_copy_with_pixel_snap = new Material(parent_tileMap.material); material_copy_with_pixel_snap.name += "_pixelSnapCopy"; material_copy_with_pixel_snap.hideFlags = HideFlags.DontSave; material_copy_with_pixel_snap.EnableKeyword("PIXELSNAP_ON"); material_copy_with_pixel_snap.SetFloat("PixelSnap", 1f); material_copy_with_pixel_snap_dict[parent_tileMap.material] = material_copy_with_pixel_snap; } meshRenderer.sharedMaterial = material_copy_with_pixel_snap; } else { meshRenderer.sharedMaterial = parent_tileMap.material; } UpdateMaterialPropertyBlock(); if (animated_tile_list.Count > 0) //TODO: add fps attribute to update animated tiles when necessary { for (int i = 0; i < animated_tile_list.Count; ++i) { AnimTileData animTileData = animated_tile_list[i]; Vector2[] uvs = animTileData.tileSetBrush.GetAnimUVWithFlags(inner_padding); if (animTileData.sub_tile_index >= 0) { for (int j = 0; j < 4; ++j) { if (j == animTileData.sub_tile_index) { uv_list[animTileData.vertex_index + j] = uvs[j]; } else { uv_list[animTileData.vertex_index + j] = (uvs[j] + uvs[animTileData.sub_tile_index]) / 2f; } } } else { uv_list[animTileData.vertex_index + 0] = uvs[0]; uv_list[animTileData.vertex_index + 1] = uvs[1]; uv_list[animTileData.vertex_index + 2] = uvs[2]; uv_list[animTileData.vertex_index + 3] = uvs[3]; } } if (meshFilter.sharedMesh) { meshFilter.sharedMesh.SetUVs(0, uv_list); } } }