• 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
0
Question by CyberMew · Mar 04, 2014 at 05:41 PM · 2dtexturesprite

Sprite.Create is not working

 void OnPostprocessTexture(Texture2D texture)
 {
     GameObject go = new GameObject("TEST");
     SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
 
     sr.sprite = Sprite.Create(texture, new Rect(0,0,500,500), new Vector2 (0.5f, 0.0f));
 }

The above sample code does not work properly. The sprite does not point to any Sprite texture. The SpriteRenderer's sprite points to none in the Inspector. However it seems like the data was valid while walking through the code. What is the problem here?

Comment
Add comment · Show 9
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 Loius · Mar 04, 2014 at 05:53 PM 0
Share

I use Sprite.Create succcessfully, so it's probably some weird issue. (0,0,500,500) means a box from 0,0 to 500,500; 0.5f,0.0f means the pivot will be top-center. Would these settings put the object out of view?

Is it possible to create gameobjects during the import process? That seems like an easy way to create memory leaks, perhaps Unity blocks it. Have you tried using a [$$anonymous$$enuItem("")] function?

avatar image CyberMew · Mar 05, 2014 at 07:31 AM 0
Share

I made it big so that I can see it easily. It creates a sprite which I would like to assign to a SpriteRenderer but it just points to nothing. Also the pivot point should point to the Bottom.

Yes it is possible to create gameobjects during the import process, but make sure you call DestroyImmediately to delete it once you are done. I am using it to create prefabs. I am trying to automate the import process to create prefabs so a $$anonymous$$enuItem won't do for me :(

avatar image CyberMew · Mar 05, 2014 at 04:38 PM 0
Share

I tried several code variations but it still doesn't work no matter what I do. It is like Sprite.Create is doing something funky with the texture. Even tried both OnPostprocessTexture and OnPostprocessAllAssets, same thing. Of course, it works great during runtime code, but that is not what we want.

If anyone is able to do Sprite.Create fine in these 2 functions, please post them as answers!

avatar image Loius · Mar 05, 2014 at 05:21 PM 0
Share

Of course it's bottom-center, why would we have any consistency in our coordinate systems. Consistency is for lazy people. [/sarcasm]

$$anonymous$$y only other suggestion is, is the texture a Sprite-type texture? You might need to do things with TextureImporter to be sure you have a Sprite texture to work with.

avatar image Dblfstr · Mar 05, 2014 at 07:10 PM 0
Share

What texture is actually being passed to the function? I do not know if you have it set in the inspector, or if you set it elsewhere? Are you trying to do this for a texture in a folder, automatically? Is the texture actually 500x500? Just looking for info.

avatar image CyberMew · Mar 05, 2014 at 07:44 PM 0
Share

It is a .png file. The function and texture is from http://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.OnPostprocessTexture.html. I am trying to do create a prefab from a .png file automatically. The texture is not really 500x500 but I was just testing it. It should be texture.width and texture.height ins$$anonymous$$d.

I just managed to workaround this problem. I gave up on this function and attempted on http://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html ins$$anonymous$$d. Same issue.

Then I used this to assign the Sprite to the SpriteRenderer

 sr.sprite = AssetDatabase.LoadAssetAtPath(fullPath, typeof(Sprite)) as Sprite;


Works great!

It could work for OnPostprocessTexture as well but I haven't tried it yet. Hope it helps somebody!

avatar image Dblfstr · Mar 05, 2014 at 08:03 PM 0
Share

I was going to post this. Which mainly came from the OnPostProcessTexture docs. Because you need to specify where the texture is.

void OnPostprocessTexture (Texture2D texture) {
// Only post process textures if they are in a folder // "sprite texture" or a sub folder of it.

             string lowerCaseAssetPath = assetPath.ToLower();
             
             if (lowerCaseAssetPath.IndexOf ("/sprite texture/") == -1){
                 return;}
             
                 GameObject go = new GameObject("TEST");
                 SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
  
                 sr.sprite = Sprite.Create(texture, new Rect(0,0,500,500), new Vector2 (0.5f, 0.0f),100f);
         }
avatar image CyberMew · Mar 05, 2014 at 08:21 PM 0
Share

That is what I did as well. Did it work for you? I didn't check if the gameobject's spriterender actually hold that sprite, since I converted it to a prefab. And the prefab's sprite was null. But then again it could be my textureimporter settings that caused it not to work properly, since I just discovered a bug and fixed it. :o

avatar image Dblfstr · Mar 05, 2014 at 08:24 PM 0
Share

I will try this evening... hopefully.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Janibasha · Dec 27, 2017 at 11:41 PM

Rect rect = new Rect(0, 0, 8, 8); ==>> use 8x8 or as in ur error like 64x64 then Rect rect = new Rect(0, 0, 64, 64); ArgumentException: Could not create sprite (0.000000, 0.000000, 64.000000, 64.000000) from a 8x8 texture.

fbpic.sprite = Sprite.Create(result.Texture, rect, new Vector2(0f, 0f), 125);

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

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

23 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

Related Questions

Texture type -> Advanced -> Mesh type: how it works? 1 Answer

reset 2d animation(sprite sheet texture) 0 Answers

Tilling material on sprites it's not working correctly 1 Answer

How to break a sprite into shapes with script 1 Answer

How can I interacte with Sprites from a Texture with Sprite Mode multi? 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