Skip to content

Instantly share code, notes, and snippets.

@luisparravicini
Created May 14, 2019 22:02
Show Gist options
  • Save luisparravicini/4e892b0968699fbee97f99a05eecc420 to your computer and use it in GitHub Desktop.
Save luisparravicini/4e892b0968699fbee97f99a05eecc420 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -W
require 'yaml'
require 'fileutils'
#
# Copies a physics shape from a sprite (reading it's .meta file)
# and aplies the same shape to other sprites.
# Tested only with .meta files created with Unity 2019.1.0f2 and MY
# use case. I'm sure it will break in any other case!
# MAKE A BACKUP BEFORE USING!
#
def read_meta(path)
YAML.load_file(path)
end
IMPORTER_KEY = 'TextureImporter'
src_sprite = ARGV.shift
if src_sprite.nil?
puts "usage: #{$0} sprite_with_shape dst_1[ dst_2[ ..]]"
exit 1
end
meta = read_meta(src_sprite)
shape = meta[IMPORTER_KEY]['spriteSheet']['physicsShape']
shape_as_s = ' - - ' + shape.map do |item|
item.map { |subitem| '{x: %d, y: %d}' % [subitem['x'], subitem['y']] }
end.join("\n - ")
puts "copying shape:\n["
#puts shape
puts shape_as_s
puts "]"
while (dst=ARGV.shift) do
print '.'
# to_yaml produces something like the original but not the same
# so it changes it as a string
# meta = read_meta(dst)
# meta[IMPORTER_KEY]['spriteTessellationDetail'] = 0
# meta[IMPORTER_KEY]['spriteSheet']['physicsShape'] = shape
meta = IO.read(dst)
unless meta.gsub!(%r{(?<=spriteTessellationDetail: )[\d-]+}, '0')
raise "couldn't replace tesselation detail in #{dst}"
end
unless meta.gsub!(%r{(?<=physicsShape:) \[\]}, "\n" + shape_as_s)
raise "couldn't replace physicsShape detail in #{dst}"
end
tmp_path = dst + '.tmp'
File.open(tmp_path, 'w') do |io|
io.write(meta)
#YAML.dump(meta, io)
end
FileUtils.mv(tmp_path, dst)
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment