• 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
3
Question by Stone-Legion · Nov 23, 2013 at 12:45 AM · texturespritetextureimporter

Creating sprite in code using TextureImporter with C#?

I'm trying to create a Sprite based on an external texture. The intended functionality once I finish is that a user would be able to select a photo from an external source and use that as their sprite. My problem is trying to convert that texture into a sprite, which so far I haven't been able to do.

 using UnityEngine;
 using UnityEditor;

 public Material defaultMaterial; //prefab material set already
 IEnumerator Start()
 {
 
 SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
 string path = "file://C:\\Users\\i5\\Documents\\My2DProject\\Assets\\oranges.png";
 
 TextureImporter tImport =  AssetImporter.GetAtPath(path) as TextureImporter;
 TextureImporterSettings tImportSettings = new TextureImporterSettings();
 
 tImport.isReadable = true;
 //Throws an error NullReferenceException: Object reference not set to an instance of an object
 tImport.textureType = TextureImporterType.Sprite;
 tImportSettings.spriteMode = 1;
 tImport.SetTextureSettings(tImportSettings);
 
 renderer.sprite = //How can I convert the oranges.png texture into a sprite with TextureImporter tImport? I'm stuck on this part.
 renderer.material = defaultMaterial;
 }
 
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

1 Reply

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

Answer by Stone-Legion · Nov 23, 2013 at 01:19 AM

Solved my problem, here is the solution. Note you can swap the path for a URL if you want to load an image from the web.

 using UnityEngine;
 using UnityEditor;
  
 public Material defaultMaterial; //prefab material set already
 IEnumerator Start()
     {
         string path = "file://C:\\Users\\i5\\Documents\\My2DProject\\Assets\\oranges.png";
         WWW www = new WWW(path);
         yield return www; 
 
         SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
         Sprite sprite = new Sprite();
         sprite = Sprite.Create(www.texture, new Rect(0, 0, 170, 170),new Vector2(0, 0),100.0f);
 
         renderer.sprite = sprite;
         renderer.material = mat;
 }

For more see http://docs.unity3d.com/Documentation/ScriptReference/Sprite.Create.html

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 flowery · Feb 23, 2016 at 11:08 AM 0
Share

Hey, I'm searching for the same functionality. What do you mean by "prefab material set already" ? How do I do that? Thanks.

avatar image bintangd · Apr 22, 2016 at 01:00 PM 0
Share

nice information.. but now the picture's quality will be down (resolution). how to make the picture's resolution same with the picture real ??

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Override texture import path 0 Answers

How to skip already imported textures on AssetPostprocessor change? 3 Answers

Sprite Renderer is wrapping bottom pixel when it shouldn't be 1 Answer

Differentiating between first import and reimport in the TextureImporter 0 Answers

Combine Array of Sprites to Form One Sprite 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