- Home /
Removing background from a sprite
Guys, 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.
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.
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.
@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.
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);
}
}
mageTool.cs(102,10): error CS0103: The name `ssEditorTools' does not exist in the current context
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.
Let me know if this helps!
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.
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
Follow this Question
Related Questions
Background process running when Unity Editor window is deactivated 1 Answer
Problem with GuiTexture behind the 3d objects 0 Answers
How to deactivate background music when I land on an object? 1 Answer
Game launches as background process in windows 2012 0 Answers
A way to set a custom camera.viewport background color? 1 Answer