• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 05, 2016 at 05:21 PM by codebeans for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by codebeans · Mar 13, 2016 at 08:53 PM · spritespritesspriterendererspritesheetsprite packer

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ryan_unity · Mar 30, 2016 at 12:24 PM 0
Share

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.

avatar image codebeans ryan_unity · Mar 30, 2016 at 02:34 PM 0
Share

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 minimize 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".

1 Reply

  • Sort: 
avatar image
1
Best Answer

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;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image codebeans · Apr 04, 2016 at 07:39 PM 0
Share

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 Awakefunction ins$$anonymous$$d of the Start function

Anyways thanks for your help on this

avatar image ryan_unity codebeans · Apr 05, 2016 at 01:24 PM 0
Share

I've converted the comment to an answer for the benefit of anyone else coming 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 minimal project + repro steps attached, so the developers can investigate it and keep you informed.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

keep sprite same size when changing 1 Answer

override SpritePacker packing order 0 Answers

Gui Box - Texture - Spritesheet 0 Answers

Prefabs and multiple tilesets? 0 Answers

2D sprite renders a small part of another one with "Sprite Mode : Multiple" 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges