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
15
Question by BenoitFreslon · Feb 26, 2014 at 03:48 PM · spritetexture2d

Convert a Texture2D to Sprite

Hello,

I would like to convert an array of Texture2D to an array or Sprites.

I tried this method:

 Sprite.Create(texture, rect, pivot);

But how to set the exact rect and the exact pivot of a texture?

thanks.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
49
Best Answer

Answer by TrevorP · Feb 26, 2014 at 04:34 PM

The rectangle parameter is the source rectangle on the texture. If you want to use the whole texture as the sprite, use new Rect(0, 0, texture.width, texture.height);

I believe the pivot is actually a percentage and not a pixel/unit value. So if you wanted to pivot at the center of the texture you would use new Vector2(0.5f, 0.5f)

Sorry for the pseudo code, away from Unity at the moment.

Hope this helps!

Comment
Add comment · Show 6 · 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 BenoitFreslon · Feb 26, 2014 at 06:38 PM 0
Share

Thanks ;) Trevor

avatar image siddharth3322 · Jul 03, 2015 at 04:01 PM 0
Share

@TrevorP, Thanks this helped me a lot.

avatar image NinjaISV · Sep 11, 2015 at 03:49 PM 0
Share

Wow! This is awesome! Thanks! :)

avatar image hauzz · Nov 19, 2015 at 08:44 AM 0
Share

I use Rect rec = new Rect(0, 0, texture.width, texture.height);

dust.GetComponent ().sprite=Sprite.Create(texture,rec,new Vector2(0.5f,0.5f),100);

But result is image became small than before sprite.

Someone know how to solve.

Thanks for the help!

avatar image Priyanka-Rajwanshi hauzz · Dec 20, 2016 at 06:28 AM 2
Share

@hauzz You need to change "pixelsPerUnit" in Sprite.Create() , i.e. change "100" in your code. The pixelsPerUnit value controls the size of the sprite. Reducing this below 100 pixels per world increases the size of the sprite.

Also, The issue could have been that the texture that you would have imported would be of greater pixel size(lets says 2048*2048) and while importing in unity, you would have reduced the maximum size(e.g. 1024). This made the sprite created from the texture to be smaller than actual when you loaded it using Sprite.Create().

avatar image EpicWolffe Priyanka-Rajwanshi · Nov 16, 2020 at 02:58 PM 0
Share

^ Saved me loads of time, could not figure out why so tiny. Thanks =)

avatar image
11

Answer by Bilelmnasser · Apr 04, 2017 at 07:00 AM

hello, maybe it's a late reply but my suggestion is : when you dealing with sprite downloaded and created at Runtime , i recommand to use RawImage better than UIimage because Sprite.Create use more CPU example :

 using UnityEngine;
 using System.Collections;
 
 // Get the latest webcam shot from outside "Friday's" in Times Square
 public class ExampleClass : MonoBehaviour
 {
 public RawImgae Rimage;
     public string url = "https://worldcams.tv/images/small/89.jpg";
 
     IEnumerator Start()
     {
         // Start a download of the given URL
         WWW www = new WWW(url);
 
         // Wait for download to complete
         yield return www;
 
         // assign texture
        
         Rimage.texture = www.texture;
     }
 }

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 Eldoir · Aug 29, 2017 at 03:06 PM 2
Share

Thanks mate, ~40% speed gain for me!

avatar image hdeekshith · Feb 06, 2019 at 02:26 PM 1
Share

Works Great!

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading a Sprite (Unity 4.3) in resource folder and setting it in sprite renderer 6 Answers

Converting transparent Texture2D to Sprite 0 Answers

Load Unity 4.3 Sprites with AssetBundles 5 Answers

why texture2d sprite rendering not showing in game 0 Answers

How to read sprites/textures from an Animator current animation? 0 Answers


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