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 Khena_B · Dec 23, 2016 at 08:18 PM · texture2dtransparencyalphacreatesprite renderer

Sprite.Create alpha

Can anybody tell me why created sprites are translucent?

 void Start ()
     {
         Texture2D texture = new Texture2D(32, 16);
         spriteRenderer.sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 4.0f);
     }

White sprite has a higher render order and has an alpha of 1 yet we can see through it? alt text

sprites.jpg (32.8 kB)
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
1
Best Answer

Answer by TBruce · Dec 23, 2016 at 08:57 PM

This is the default way textures are created - see the docs.

As the docs state

Usually you will want to set the colors of the texture after creating it, using SetPixel, SetPixels and Apply functions

Check out this modification to your script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SpriteManager : MonoBehaviour
 {
     public SpriteRenderer spriteRenderer;
     public Color defaultColor = Color.white;
     void Start ()
     {
         if (spriteRenderer != null)
         {
             Texture2D texture = new Texture2D(32, 16);
             List<Color> cols = new List<Color>();
             for (int i = 0; i < (32 * 16); i ++)
             {
                 cols.Add(defaultColor);
             }
             texture.SetPixels(cols.ToArray());
             texture.Apply();
             spriteRenderer.sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 4.0f);
         }
     }
 }

Comment
Add comment · Show 7 · 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 Khena_B · Dec 23, 2016 at 09:19 PM 0
Share

That worked, however it didn't give me the results that i expected, it does when i use my own sprite and use SetPixel on it, however the changes to that sprite are permanent, how can i create a clone of that sprite on Start and set the pixels of that clone ins$$anonymous$$d?

avatar image TBruce Khena_B · Dec 23, 2016 at 10:51 PM 1
Share

Hi $$anonymous$$henaB, This can be done with a slight modification to the above script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Sprite$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
     public SpriteRenderer spriteRenderer;
     public Sprite sprite; // sprite to copy
     void Start ()
     {
         if ((spriteRenderer != null) && (sprite != null))
         {
             Texture2D texture = new Texture2D(sprite.texture.width, sprite.texture.height);
             texture.SetPixels(sprite.texture.GetPixels());
             texture.Apply();
             spriteRenderer.sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 4.0f); // you may want to change or even remove the last parameter of Sprite.Create()
         }
     }
 }

This requires however that you change the Texture Type of your sprite to Advanced and then check the Read/Write Enabled check box (don't forget to hit apply).

avatar image Khena_B TBruce · Dec 23, 2016 at 11:18 PM 0
Share

Thanks, however i get the same result than with the previous solution, here's a comparison of when i set the pixel at 0,0 of the sprite directly, and if i create a sprite and use a Texture2D. With the Texture2D the green pixel is blurry. Could this have anything to do with filtering?

alt textalt text

1.jpg (112.1 kB)
2.jpg (97.4 kB)
Show more comments

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

61 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

Related Questions

Fragment shader shows alpha, surface shader doesn't 0 Answers

Transparency of a specific area 0 Answers

vertex shader with alpha channel 0 Answers

UI Panel, transparency problem 1 Answer

The interactable part of my button is random and do not respect the alpha setting 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