Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
4
Question by SrgTooth · Dec 12, 2013 at 03:26 PM · spritetexture2druntimespriterenderercreate a texture

Can I create a sprite at runtime?

I was wondering if it was possible to create a new sprite at runtime. I'm basicly trying to slice a Sprite in 2 pieces (similar to Metal Gear Rising but in 2D)

In this example, i'm creating a copy of the current object and i'm trying to create a new sprite for it's SpriteRenderer. I'm able to create the new sprite and the new texture but when I assign them to the SpriteRenderer, it does not display anything. The result is an invisible object (since the SpriteRenderer is not diplaying). The strange part is that when I select the sprite in the SpriteRenderer component of the new object, the preview is showing the part of the texture I wanted...

It is possible that I am not using these components the intended way, so feel free to make suggestions. Thanks.

         Texture2D tex = new Texture2D(100,100);
         tex.SetPixels(0,0,100,100,this.GetComponent<SpriteRenderer>().sprite.texture.GetPixels(0,0,100,100));
         tex.Apply();

         GameObject newObj = Instantiate(gameObject) as GameObject;

         SpriteRenderer renderer =  newObj.GetComponent<SpriteRenderer>();
         Sprite sprite = new Sprite();
         sprite = Sprite.Create(tex,new Rect(0, 0, 100, 100),new Vector2(50,50));
         renderer.sprite = sprite;
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 hmortensen2907 · Dec 30, 2013 at 11:50 AM 0
Share

Did you figure out what the problem was/is? It sound like some of the same troubles I'm having (http://forum.unity3d.com/threads/219939-Prefab-created-at-runtime-is-missing-sprite-reference-(original-object-is-not)) So if you have found out anything, I would very much like to know. Thanks. - Henning

avatar image DESTRUKTORR · Jul 29, 2014 at 08:36 PM 0
Share

Sprite sprite = new Sprite(); found at line 8 should be removed. You are instantiating a sprite, then replacing it with Sprite.Create two lines later, and you don't want to instantiate Sprites with "new" anyway, since it doesn't properly initialize the object. You should, in s$$anonymous$$d, use "Sprite.Create" (as you did).

3 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Peaj · Jan 05, 2014 at 11:39 AM

I'm trying to achieve the same sprite splicing effect and I think i resolved your problem. You used "new Vector2(50,50)" for the pivot but the pivot is in 0,1 range. Just replace it with new "Vector2(0.5f,0.5f)". Your sprite should even be displayed atm but its far away from where you expect it to be. Although this fixed it for me I had problems with the scaling. It works using 40 for the texels per unit which is really strange. So here is my code:

 Texture2D old = renderer.sprite.texture;
 Texture2D left = new Texture2D((int)(old.width), old.height, old.format, false);
 Color[] colors = old.GetPixels(0, 0, (int)(old.width), old.height);
 left.SetPixels(colors);
 left.Apply();
 Sprite sprite = Sprite.Create(left,
        new Rect(0, 0, left.width, left.height),
        new Vector2(0.5f,0.5f),
        40);
 Debug.Log("Old Bounds: " + renderer.sprite.bounds + " Rect: " + renderer.sprite.rect + " TexRect: " + renderer.sprite.textureRect);
 Debug.Log("Bounds: " + sprite.bounds+" Rect: "+sprite.rect+" TexRect: "+sprite.textureRect);
 renderer.sprite = sprite;

If this will help you it would be nice to share your result with me.

Comment
Add comment · Show 1 · 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 otsov · Aug 14, 2014 at 11:46 AM 0
Share

Peaj, thanks a lot, this solved a similar problem I had!

Indeed, the sprite was there but far away from where it should have been. Also, when trying to scale it via the Editor, the sprite did not scale properly as the bounds were off.

avatar image
3

Answer by Drachenfels · Nov 02, 2014 at 09:14 PM

Hi guys,

I tried to achieve the same using UnityScript, after googling for this and some other similar question, that is what works for me:

   for (var key in data) {
     ui_texture = Resources.Load(images[key], Texture2D);
     ui_sprite = Sprite.Create(ui_texture, Rect(0f, 0f, 48f, 48f), new Vector2(0f, 0f), 128f);
     
     new_sprite = GameObject();
     new_sprite.name = key;
     new_sprite.AddComponent(SpriteRenderer);
     ui_renderer = new_sprite.GetComponent(SpriteRenderer);
     ui_renderer.sprite = ui_sprite;
   }


Hopefully it will happen to be useful for someone else as well. :)

Comment
Add comment · Show 1 · 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 Txguug · Dec 24, 2015 at 07:39 PM 0
Share

thanks! This worked for me

         // create sprite
         Texture2D tex = Resources.Load<Texture2D>("texture2") as Texture2D;
         Sprite sprite = new Sprite();
         sprite = Sprite.Create(tex, new Rect(64, 0, 64, 64), new Vector2(0.5f, 0.5f));
 
         // create gameobject
         newSprite = new GameObject();
         newSprite.AddComponent<SpriteRenderer>();
         SpriteRenderer SR = newSprite.GetComponent<SpriteRenderer>();
         SR.sprite = sprite;
avatar image
1

Answer by Thonbo · Nov 16, 2014 at 07:19 PM

C#

 public Sprite AddSprite (Texture2D tex) {
         Texture2D _texture = tex;
         Sprite newSprite = Sprite.Create(_texture, new Rect(0f, 0f, _texture.width, _texture.height), new Vector2(0.5f, 0.5f),128f);
         GameObject sprGameObj = new GameObject();
         sprGameObj.name = "something";
         sprGameObj.AddComponent<SpriteRenderer>();
         SpriteRenderer sprRenderer = sprGameObj.GetComponent<SpriteRenderer>();
         sprRenderer.sprite = newSprite;
         return sprGameObj;
     }
     

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 smelchers · Apr 13, 2015 at 10:15 PM 0
Share

For me it doesn't work. C# complains that it can not convert a gameobject to a sprite in the last line. Can you help?

avatar image Peaj · Apr 14, 2015 at 09:09 AM 1
Share

It is because this code has a return type Sprite but returns a GameObject. You could just change "public Sprite" to "public GameObject" and it will work

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change SpriteRenderers Texture2D in runtime 1 Answer

Slicing sliced sprite via script 0 Answers

White Edges on Sprites Loaded from PNG at Run-time 0 Answers

Instantiate a gameObject with a dynamically generated sprite? 0 Answers

How to programmatically change image properties? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges