Tiled 编辑器插件脚本
支持JS脚本插件,存放路径:
- Windows
C:/Users/<USER>/AppData/Local/Tiled/extensions/
- macOS
~/Library/Preferences/Tiled/extensions/
- Linux
~/.config/tiled/extensions/
示例插件:https://github.com/mapeditor/tiled-extensions
官方说明: https://doc.mapeditor.org/en/stable/reference/scripting/
插件1,查找object对象:
/*
* find-object-by-id.js
*
* This extension adds a 'Find Object by ID' (Ctrl+Shift+F) action to the Map
* menu, which can be used to quickly jump to and select an object when you
* know its ID.
*
* The script relies on the recently added TileMap.pixelToScreen conversion
* function to work properly for isometric maps.
*/
function findObjectById(thing, id) {
for (let i = thing.layerCount - 1; i >= 0; i--) {
const layer = thing.layerAt(i)
if (layer.isGroupLayer) {
const obj = findObjectById(layer, id)
if (obj)
return obj
} else if (layer.isObjectLayer) {
for (const obj of layer.objects)
if (obj.id == id)
return obj
}
}
return null
}
let jumpToObject = tiled.registerAction('JumpToObject', function(action) {
const map = tiled.activeAsset
if (!map.isTileMap) {
tiled.alert("Not a tile map!")
return
}
let id = tiled.prompt("Please enter an object ID:")
if (id == "")
return
id = Number(id)
const object = findObjectById(map, id)
if (!object) {
tiled.alert("Failed to find object with ID " + id)
return
}
const pos = map.pixelToScreen ? map.pixelToScreen(object.pos) : object.pos
tiled.mapEditor.currentMapView.centerOn(pos.x, pos.y)
map.selectedObjects = [object]
})
jumpToObject.text = "Find Object by ID"
jumpToObject.shortcut = "Ctrl+Shift+F"
tiled.extendMenu("Map", [
{ separator: true },
{ action: "JumpToObject" },
])
插件2,object对象自动对齐,并添加custom属性:
/*
* AlignObjectToGrid.js
*/
// if cb return ture, stop walk
function walkObjects(root, cb) {
for (let i = 0; i < root.layerCount; i++) {
const layer = root.layerAt(i)
if (layer.isGroupLayer) {
if (walkObjects(layer, cb)) return true
} else if (layer.isObjectLayer) {
if (cb(layer)) return true
}
}
}
let jumpToObject = tiled.registerAction('AlignObjectToGrid', function(action) {
const map = tiled.activeAsset
if (!map.isTileMap) {
tiled.alert("Not a tile map!")
return
}
// 计算pos所在grid,修正pos,并自动添加/更新 属性col, row
let doAlgin = function(layer) {
let alginedObjs = {}
// 地图方向:正常
if (TileMap.Orthogonal == map.orientation) {
// check layer.name to do align?
for (const obj of layer.objects) { // MapObject
// get grid, obj's anchorPoint is cc.p(0,1)
let col = Math.floor((obj.x + (map.tileWidth / 2)) / map.tileWidth)
col = Math.max(col, 0)
col = Math.min(col, map.width - 1)
let row = Math.floor((obj.y - (map.tileHeight / 2)) / map.tileHeight)
row = Math.max(row, 0)
row = Math.min(row, map.height - 1)
// align pos
obj.x = col * map.tileWidth
obj.y = (row + 1) * map.tileHeight
// fix properties
obj.setProperty("col", col)
obj.setProperty("row", row)
obj.setProperty("orientation", obj.tile.property("orientation"))
// check duplicate
let index = col + row * map.width
if (alginedObjs[index]) {
map.selectedObjects = [obj] // select object in map
tiled.alert("找到重复坐标对象,请检查!")
return true // stop walk
} else {
alginedObjs[index] = obj
}
}
}
}
walkObjects(map, doAlgin)
})
jumpToObject.text = "Align Object To Grid"
jumpToObject.shortcut = "Ctrl+Shift+A"
tiled.extendMenu("Map", [
{ separator: true },
{ action: "AlignObjectToGrid" },
])