- Home /
The question is answered, right answer was accepted
Override sprite geometry of sprite generated at runtime
I have a lot of Sprites in my Game, therefore I collect their textures and pack them during runtime into a bigger texture atlas.
I then want replace the old spriteRenderers' sprites by creating a new Sprite via Sprite.Create(...)
As a next step I need to override the sprite's vertices and triangles using OverrideGeometry(verts,triangles)
But the catch seems to be that I can not override the geometry unless the sprite mode is SpriteImportMode.Polygon
, which I can only set in the TextureImporterSettings.
But since I create my atlas at runtime and do not want to safe it to the disk I can not change these either.
Is there a way to override a sprite's geometry from a sprite created at runtime?
Hey :) I'm interested, why is this being done at runtime? It's normally a lot more efficient to do this beforehand, using Sprite Packer for example to create your atlases.
I have a set of about 10000 sprtes total that are lazy loaded using asset bundles since I can't ship them with the app. Levels are "rooms"in isometric 2.5d which are user generated and contain any subset of about 500 of these 10k sprites similar to the game habbo. In order to $$anonymous$$imize draw calls I have to pack these 500 sprites at runtime. This works using Texture.PackTexutes and Sprite.Create. A very essential part in this process for me is to update the geomerty of the sprite to the projection of isometric shapes to make things visually "correct".
Answer by ryan_unity · Apr 04, 2016 at 02:03 PM
Hey again, what's the error that you get when you call Sprite.OverrideGeometry? This all seems to work with my little test in Unity 5.3.4p1 that displays half of the sprite in one triangle (put script on a GameObject with a SpriteRenderer and add a readable texture to the atlasTextures array in the Inspector):
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Texture2D[] atlasTextures;
// could make these public for debugging purposes
public Rect[] rects;
public Texture2D atlas;
Sprite sprite;
SpriteRenderer spriteRenderer;
// Use this for initialization
void Start () {
spriteRenderer = GetComponent<SpriteRenderer>();
if (!spriteRenderer)
return;
// pack textures
atlas = new Texture2D(8192, 8192);
rects = atlas.PackTextures(atlasTextures, 2, 8192);
// create sprite using rects[0]
Vector2 atlasSize = new Vector2(atlas.width, atlas.height);
Rect spriteRect = new Rect(Vector2.Scale(rects[0].position, atlasSize), Vector2.Scale(rects[0].size, atlasSize));
sprite = Sprite.Create(atlas, spriteRect, new Vector2(0.5f,0.5f));
// create new shape for sprite using array of vertices
Vector2[] vertices = new Vector2[3];
vertices[0] = new Vector2(0,sprite.rect.size.y);
vertices[1] = new Vector2(sprite.rect.size.x,0);
vertices[2] = new Vector2(0,0);
ushort[] triangles = new ushort[3];
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
// set sprite vertices
sprite.OverrideGeometry(vertices, triangles);
// send new sprite to the sprite renderer
spriteRenderer.sprite = sprite;
}
}
That was pretty much the approach I took as well. But I got an Not allowed to override geometry on sprite Exception on OverrideGeometry
. I tested your code on Unity 5.3.2.f1 and it seems to work fine now.
I experienced the same behaviour as in this question http://answers.unity3d.com/questions/946643/spriteoverridegeometry-doesnt-work.html
One way to end up with the same exception though is putting the above code into the Awake
function ins$$anonymous$$d of the Start
function
Anyways thanks for your help on this
I've converted the comment to an answer for the benefit of anyone else co$$anonymous$$g across this :) The error that happens when the code is in Awake() is odd. It's probably best for you to submit a bug report via the Editor, with a $$anonymous$$imal project + repro steps attached, so the developers can investigate it and keep you informed.