Ursina Engine自定义动画实现问题求助
I get it, it’s frustrating when built-in animations work flawlessly but your custom frame setup doesn’t show anything at all. Let’s break down why your code isn’t working and fix it step by step.
What’s Wrong With Your Original Code?
The main issue is that you’re passing 'test' as the first argument to the Animation class. When using the frames parameter to define custom textures, Ursina still tries to load a built-in or named animation resource matching 'test'—this conflicts with your custom frames and prevents them from rendering properly.
There are also a few small tweaks to ensure your animation shows up reliably:
- Double-check your file paths (make sure your
animationfolder exists in your project root, and filenames are spelled correctly—note that paths are case-sensitive on macOS/Linux!). - Adjust the
scalevalue (a scale of 1 might be too small depending on your window size, making the animation hard to spot). - Verify your textures are loading successfully by printing them (e.g.,
print(coin1)should return aTextureobject, notNone).
Fixed Code Example (Simplified)
Instead of loading each texture individually, you can pass a list of file paths directly to frames—Ursina will handle loading them automatically. This reduces clutter and potential errors:
from ursina import * app = Ursina() window.color = color._20 # Leave the first argument empty to avoid conflicting with built-in animations animation = Animation( '', fps=6, # Adjust to make the animation smoother scale=2, # Larger scale ensures the animation is visible filtering=None, autoplay=True, frames=[ 'animation/coin1', 'animation/coin2', 'animation/coin3', 'animation/coin4', 'animation/coin5', 'animation/coin6' ] ) EditorCamera() app.run()
Alternative: Load Textures Manually
If you prefer to load textures individually (e.g., for pre-processing), just make sure the first Animation parameter is empty so Ursina doesn’t try to load a conflicting resource:
from ursina import * app = Ursina() window.color = color._20 # Use a loop to load textures cleanly coin_frames = [load_texture(f'animation/coin{i}') for i in range(1, 7)] animation = Animation( '', fps=6, scale=2, filtering=None, autoplay=True, frames=coin_frames ) EditorCamera() app.run()
Quick Troubleshooting Tips
- File Path Clarity: If your textures have specific extensions (like
.png), include them in the path (e.g.,'animation/coin1.png') to avoid ambiguity. - Check Loading Status: Add
print(coin_frames)after loading—anyNoneentry means the corresponding file wasn’t found. - Ensure Visibility: Temporarily set
animation.position = (0,0,0)to confirm it’s not off-screen, and increasescaleif needed.
内容的提问来源于stack exchange,提问作者Another_coder

