Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating and changing sprite's mesh at runtime?

Discussion in '2D' started by castor76, Aug 6, 2016.

  1. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,511
    I was really hoping I could create and then change its rendering mesh (vertices, triangle, uv) on runtime, but when I tried to use Sprite.OverrideGeometry(), it won't let me!

    It just always tells me that :

    "Not allowed to override geometry on sprite"

    Why is it not allowed? if it isn't allowed then when is it allowed? Is it only for Unity Editor mode asset import time?

    If so , this isn't really useful! It lets me create sprite on runtime, but it does not allow me to modify its mesh info? What? Who has even thought of that idea? It's terrible!

    Does anyone know how to change sprite's mesh using Sprite.OverrideGeometry() at runtime?
     
    PersianKiller and JoeStrout like this.
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Why don't you just use quads if you want to work with the geometry?
     
  3. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,511
    Well, I would like to create Unity sprite object to take advantage of what ever Unity offers on that regard. I suppose I can create a normal mesh and mesh-renderer , but that is not the point. It is this, Unity has feature, but it won't work during runtime and its documentation is lacking on when and where it can be used.

    If it has function like Sprite.OverrideGeometry(), I expect it to work during runtime, because it isn't specifically listed under UnityEditor function.
     
    fireanth and JoeStrout like this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    I'm in the exact same boat. My whole game is set up to work with sprites... and now I want to create these sprites dynamically, loaded from TexturePacker data (which includes the geometry info we need)... and it turns out that Sprite.OverrideGeometry only works at edit time (despite the docs still not saying anything about this!).

    So now it appears my choices are:
    1. Use full rects, which means a lot of wasted overdrawing.
    2. Let Unity come up with its own mesh, which is way too slow (about 8 seconds per sprite sheet on my machine, and one character will have 5-10 sprite sheets).
    3. Abandon Sprite and SpriteRenderer, using my own mesh-based substitute, and rework all the code that's currently assuming the game is built on sprites.
    These are all terrible choices. Is there really no way to create a sprite with desired triangle mesh at runtime?
     
    Last edited: Jul 14, 2021
    fireanth likes this.
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Just confirmed that this is still broken in 5.6.

    This despite this release note from 5.5.3f1:

    This sure seems to imply that somebody at Unity, under some conditions, is actually able to use OverrideGeometry at runtime (that's what "Standalone" means here, right?). But darned if I can see how. The code I'm using is trivial:

    Code (csharp):
    1. mySprite.OverrideGeometry(spriteDef.vertices, spriteDef.triangles);
    Yet it produces, every single time:
    I'm getting a bit desperate here... does anybody have any clues when/how this method can work at runtime?
     
    fireanth and Bloody-Swamp like this.
  6. Bloody-Swamp

    Bloody-Swamp

    Joined:
    Jul 30, 2012
    Posts:
    37
    bump
     
    fireanth likes this.