Hi, im trying to develop a simple system but my knowledge is lacking to get started. Here is what I need: I have a spitesheet with various frames, in each of those frames, there is a single bright green (0,255,0) pixel on the character’s head, using that pixel, I want to track the character’s head world position. So basically, how can i get the pixels of the current sprite, check for the green one an then get its world position so a i can do some stuff with it later?
It won’t be practical to search for that pixel at runtime.
For one, you’d have to linearly scan each sprite, and if some art had a stray green pixel in it, you’d have a problem.
For two, any type of image compression will change that pixel away from (0,255,0) to some other fairly-green pixel, or might hide it entirely.
Your best bet is to make an editor tool that iterates all the sprites (probably only when you ask it to, like when you know you updated the art) and produces a new datafile that contains the offsets for each frame. I would just store them as some sort of JSON format, then load that file at runtime and use those values.
Another approach would be to make a prefab that contains the sprite as well as another invisible GameObject, and then animate that GameObject (with a traditional Unity animation process) to match the pixel each frame. Then just use the GameObject directly.
How im going to use the information im not really sure yet, im thinking of generating animation data from that info, i never intended to do it during runtime
Sure, seems reasonable: just make an editor script to scan the sprites looking for that green pixel, then calculate the offset and produce an animation asset. All of these things are doable through Editor scripting. Check out tutorials on making an editor script to write fresh assets to your project, in this case the animation track.
Definitely start with this huge blob of code here:
You can certainly shortcut a lot of it if your sprites are one-image-at-a-frame rather than a sheet.
steps:
FOR EACH SPRITE:
- get pixels array (either above code or just Texture2D.GetPixels())
- iterate 2D pixel array (nested x and y loops)
- get each pixel, see if it is green (or “close enough” if you use image compression)
- note the x,y at that point, write it out
