Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by smallB · May 14, 2012 at 06:26 PM · background

Removing background from a sprite

alt textGuys, I'm trying to "load" some 2d image as a material for a mesh, (I know about spritemanager) but I'm unfortunately getting this sprite with it's white background. How can I "make it go"(the background)?
Thanks.

GreyShip_1j.jpg (13.8 kB)
Comment
Add comment · Show 1
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 Hordaland · Feb 19, 2014 at 12:01 AM 1
Share

The image you attached is a jpeg. As far as I know jpegs can't have transparency. Open the file in photoshop (or a similar program) and remove all white, then save it as png. Then use the transparent shader with cutout when adding it to an object in Unity.

3 Replies

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

Answer by Bunny83 · May 14, 2012 at 06:27 PM

Give your image an alpha channel and remove all parts you don't want to see. Of course you need either a transparent or cut-out-shader.

Comment
Add comment · Show 3 · 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 rileydabozo · Feb 18, 2014 at 11:49 PM 0
Share

What is an alpha channel?????! Where is it???

avatar image Bunny83 · Feb 19, 2014 at 01:40 AM 1
Share

@rileydabozo:
Try this one.

There are many tutorials on the net which explains what an alpha channel is and how you create an image which contains an alpha channel in almost every image editing program.

avatar image Toby_Smith Bunny83 · Apr 27, 2017 at 11:40 PM 0
Share

A beautiful link ;)

avatar image
3

Answer by karl_ · May 14, 2012 at 06:44 PM

Here's a simple editor script I use to remove a color from images without opening an external editor. You can drag multiple files into the object field to batch alpha-fy images. To use this script create a C# file called ImageTool.cs and place it in a folder called Editor. Then open Window/Tools/Alpha-fy Images.

 using UnityEngine;
 using UnityEditor;
 using System.IO;
 using System;
 using System.Collections.Generic;
 
 public class ImageTool: EditorWindow
 {
     Texture2D img;
     Texture2D newImg;
     Color colorToRemove = Color.red;
     public static ImageTool win;    
     
     [MenuItem("Window/Tools/Alpha-fy Images")]
     static void Init () 
     {
         win = ScriptableObject.CreateInstance( typeof(ImageTool) ) as ImageTool;
         win.ShowUtility();            
     }
     
     void OnGUI()
     {
         GUILayout.BeginHorizontal();
         
             /** Toolbar **/
             GUILayout.BeginVertical();
                 img = (Texture2D)EditorGUILayout.ObjectField(img, typeof(Texture2D), false, GUILayout.MinWidth(128), GUILayout.MinHeight(128), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128));
             
                 colorToRemove = EditorGUILayout.ColorField(colorToRemove, GUILayout.MaxWidth(128));
                 
                 if(GUILayout.Button("Preview", GUILayout.MinWidth(128), GUILayout.MinHeight(32), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128)))
                     newImg = RemoveColor(colorToRemove, img);
                 
                 if(GUILayout.Button("Alpha-fy All", GUILayout.MinWidth(128), GUILayout.MinHeight(32), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128)))
                     RemoveColor(colorToRemove, (UnityEngine.Object[])Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets) );
                     
             GUILayout.EndVertical();
             
             GUILayout.BeginVertical();
                 GUILayout.Label("Selected Files", EditorStyles.boldLabel);
                 foreach(Texture2D selected in Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets) )
                 {
                     GUILayout.Label(selected.name);
                 }
             GUILayout.EndVertical(); 
             
             /** Image Display **/    
         GUILayout.BeginVertical();
             GUILayout.Label("Preview", EditorStyles.boldLabel);
             if(newImg)
             {        
                 GUILayout.Label(newImg);        
             }
         GUILayout.EndVertical();
         
         GUILayout.EndHorizontal();
             
     }
     
     void RemoveColor(Color c, UnityEngine.Object[] imgs)
     {
         if(!Directory.Exists("Assets/AlphaImages/"))
         {
             Directory.CreateDirectory("Assets/AlphaImages/");
         }
         float inc = 0f;
         foreach(Texture2D i in imgs)
         {
             inc++;
             if(EditorUtility.DisplayCancelableProgressBar(
                 "Playin' With Pixels",
                 "Seaching for Color Matches",
                 ( (float)inc/(float)imgs.Length) ) )
                 {
                     break;
                 }
                 
             ssEditorTools.MaxImportSettings(i);        
             Color[] pixels = i.GetPixels(0, 0, i.width, i.height, 0);
             for(int p = 0; p < pixels.Length; p++)
             {            
                 if(pixels[p] == c)
                     pixels[p] = new Color(0,0,0,0);
             }
                             
             Texture2D n = new Texture2D(i.width, i.height);
             n.SetPixels(0, 0, i.width, i.height, pixels, 0);
             n.Apply();
             
             byte[] bytes = n.EncodeToPNG();
             File.WriteAllBytes("Assets/AlphaImages/" + i.name + "_alpha.png", bytes);
         }
         
         EditorUtility.ClearProgressBar();
 
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
     }    
     
     Texture2D RemoveColor(Color c, Texture2D i)
     {
         ssEditorTools.MaxImportSettings(i);        
 
         Color[] pixels = i.GetPixels(0, 0, i.width, i.height, 0);
 
         for(int p = 0; p < pixels.Length; p++)
         {
             if(EditorUtility.DisplayCancelableProgressBar(
                 "Playin' With Pixels",
                 "Seaching for Color Matches",
                 ( (float)p/pixels.Length) ) )
                 {
                     break;
                 }
                 
             if(pixels[p] == c)
                 pixels[p] = new Color(0,0,0,0);
         }
                         
         Texture2D n = new Texture2D(i.width, i.height);
         n.SetPixels(0, 0, i.width, i.height, pixels, 0);
         n.Apply();
         EditorUtility.ClearProgressBar();
         return(n);
     }
 }
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 yev.ponomarev · Apr 09, 2016 at 12:11 PM 2
Share

mageTool.cs(102,10): error CS0103: The name `ssEditorTools' does not exist in the current context

avatar image freedom667 · Jul 19, 2018 at 06:57 AM 0
Share

what is ssEditorTools? because giving error this name

avatar image
2

Answer by jimm84 · May 18, 2017 at 11:01 AM

Hello smallB, I may have come a few years too late but I though I would contribute. Assuming you have Photoshop, you can cut out or erase the white from you artwork.

Duplicate your existing layer with the spaceship on (CMD+J or ctrl+J) - Delete your white space either buy selecting or using the eraser on your new layer. (make sure to hide or mask the original layer, otherwise you wont see what you have deleted)

You can either save for web or Save as a .PNG. Make sure to retain the alpha, keep it at 24 bit.alt text

Let me know if this helps!


screen-shot-2017-05-15-at-104924.png (10.0 kB)
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 $$anonymous$$ · Jul 17, 2017 at 06:44 PM 0
Share

thanks, I was struggeling too. I am using Gimp, but what I did wrong is that I used .Jpeg, so for all the other people co$$anonymous$$g here: export your image as a .png otherwise it won't work.

avatar image ngoclinh11a · Nov 21, 2021 at 03:37 AM 0
Share

sorry maybe i have come after few years but i still can't fix it, when i detach with photoshop color change i add it in unity it wont slice can you suggest me Is there any way to do it?

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

13 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

Related Questions

How can I display a flat background - 2D image, not a skybox - behind everything in my camera? 4 Answers

Run in background for iOS 3 Answers

Animated background 0 Answers

Randomly Transition Camera Background Color? 2 Answers

How to make the background move for a nice effect. 3 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