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
0
Question by Chris Richardson · Jun 25, 2014 at 05:09 PM · gameobjecttexturespritetexture2dalpha

Changing the alpha channel of a sprite using the new Unity sprite system

I'm trying to change the alpha channel of a sprite, i've already checked around and i couldn't find a solution to my problem because the solutions i've come across don't sound good in my situation. Many of the solutions i've found dealt with the old way of using Unity to create 2D games, I'm using sprites... and i would like to only use sprites.

I'm trying to create a trail effect when my player runs if he has a certain power up. So what i'm trying to do is create a trail game object that has a script that sets it's sprite to the players current sprite index. Then decrease the alpha channel and destroy the object once the alpha channel has reached or is below zero.

My first problem is that the color of sprites are read only so i can not set the alpha channel of the objects sprite.

I then tried to convert the sprite to a texture2d, and some how set the color of those pixels. The only way i know how to do that would be to use SetPixels( ) which takes an array of colors but that sounds crazy and i don't know exactly how to go about doing that.

I also tried converting the texture to a gui texture, but no luck there either.

I don't want to have to use a plane or create separate cropped images of my sprite sheet and set them to another texture type individual. Like i said i'd like to just read the sprite and modify the alpha channel in code.

This is what i have so far:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerTrail : MonoBehaviour 
 {
 
     public Sprite sprite;
     public float alpha = 255f;
 
     private Component spriteRenderer;
 
     private void Awake ()
     {
         spriteRenderer = GetComponent<SpriteRenderer> ();
 
         // Get sprite
         sprite = GameObject.FindWithTag ("Player").GetComponent<SpriteRenderer> ().sprite;
         
         // Convert sprite
         Texture2D croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height );
         
         var pixels = sprite.texture.GetPixels((int)sprite.textureRect.x, 
                                               (int)sprite.textureRect.y, 
                                               (int)sprite.textureRect.width, 
                                               (int)sprite.textureRect.height);
         
         croppedTexture.SetPixels (pixels);
         croppedTexture.Apply();
 
     }
 
     private void Update () 
     {
         
         alpha -= 1f;

         // Change the sprites alpha channel here
 
         if (alpha <= 0)
             GameObject.Destroy (gameObject);
     }
 }

Is what i'm trying to do possible or is there a better way of doing it? Thanks in advance.

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
0

Answer by Q-ro · Mar 01, 2016 at 01:35 AM

A bit lost as to what you are trying to achieve but i think when you talk about "alpha channel" you mean the transparency of the sprite, is thatcorrect ? if that is the case what you would do is the following:

  //Get all the sprite renderers for your game object
     SpriteRenderer[] normalizeSprites = GetComponentsInChildren<SpriteRenderer>();
     //Set up your starting alpha
     float alpha = 1.0f ;
     
     //then in your update method do as follows
 
 void update()
 {
 
 if (alpha <= 0)
              GameObject.Destroy (gameObject);
 
     for (int i = 0; i < sprites.Length; i++)
             {
                     // change the alpha the alpha
                     sprites[i].color = new Color(sprites[i].color.r, sprites[i].color.g, sprites[i].color.b, alpha);
     
 
                 
             }
     //decrease the alpha value
     alpa -= 0.1f;
 
 }

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Difficulty loading alpha textures at runtime 0 Answers

Get sprite to have top-left corner always at top-left of screen 0 Answers

Get a portion of a Texture2D to another Texture2D 1 Answer

Changing color of texture with transparency efficiently 0 Answers

Load Texture2D converted into Sprites; How can i add them to a GameObject sucessfully? 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