Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Omakron · Nov 06, 2018 at 05:45 AM · spriterenderingproceduralprocedural generationprocedural-generation

Generating a sprite from a script,Trying to generate a sprite from a script

So after a good 6 hours of trying, I'm at my wits end. I had a fun Idea that requires me to generate a simple sprite using script for later use. I tested out 3 ways to do it and none of them seem to work.

  1. Making a new texture from scratch:

Code (CSharp):

 Texture2D backgroundTexture = new Texture2D(backgroundWidth, backgroundHeight);
 Rect backgroundRect = new Rect(0, 0, backgroundWidth, backgroundHeight);
  
 for (int i = 0; i < backgroundWidth; i++)
         {
             for (int j = 0; j < backgroundHeight; j++)
             {
                 backgroundTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
             }
         }
  
 backgroundTexture.Apply();
 Sprite backgroundSprite = Sprite.Create(backgroundTexture, backgroundRect, new Vector2(backgroundWidth / 2f, backgroundHeight / 2f), pixelsPerUnit);
 backgroundSprite.name = "Background Sprite";
 GetComponent<SpriteRenderer>().sprite = backgroundSprite;
  
  

I can inspect this sprite in the Editor when the program is running and even put pretty colorfull pixels whereever i want but i can't for the life of me get it to show up on camera.

  1. Dropping a .png into the sprite renderer and trying to modify pixels:

Code (CSharp):

 SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
 Sprite backgroundSprite = spriteRenderer.sprite;
 Rect spriteRect = backgroundSprite.rect;
 Texture2D spriteTexture = backgroundSprite.texture;
 int backgroundHeight = Mathf.RoundToInt(spriteRect.height);
 int backgroundWidth = Mathf.RoundToInt(spriteRect.width);
  
 for (int i = 0; i < backgroundWidth; i++)
 {
       for (int j = 0; j < backgroundHeight; j++)
       {
          spriteTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
        }
  }

spriteTexture.Apply();

Apparently you can't do that because "the texture memory can not be accessed from scripts". Which is fine, i guess...

  1. Loading an empty .bytes image from within the script and trying to modify:

Code (CSharp):

 Texture2D backgroundTexture = new Texture2D(2, 2);
 backgroundTexture.LoadImage(imageSize100.bytes);
 Rect backgroundRect = new Rect(0, 0, size, size);
 SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
 Sprite backgroundSprite = Sprite.Create(backgroundTexture, backgroundRect, new Vector2(size / 2f, size / 2f), pixelsPerUnit);
 spriteRenderer.sprite = backgroundSprite;
 //spriteRenderer.material.mainTexture = backgroundTexture;
  
 for (int i = 0; i < size; i++)
 {
      for (int j = 0; j < size; j++)
      {
            backgroundTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
       }
 }

backgroundTexture.Apply();

This one too can be inspected in the small little bottom right window, but so far no luck on actually showing up on screen.

Anyone have any more Ideas? I'm usually pretty good at finding solutions myself using the documentation, but I am about ready to give up...

Edit: I realized that in the 1st and 3rd method something IS being displayed. with the first method it is a weird blue square at -5000, -5000. with the third it is a bright pink square at -5000, -5000. There must be an issue with how I am rendering the generated sprite rather than with the sprite itself. Can anyone help?

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

0 Replies

· Add your reply
  • Sort: 

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

205 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 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 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 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 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 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 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 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

Best algorithm for large scale procedural level generation? 0 Answers

Explanation for Procedural generation videos by Sebastian Lague. 0 Answers

Halo hidden under the sprites 3 Answers

2D Game Zooming Rendering Issues 0 Answers

Mesh wont render although there are no errors! 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