Removing background from a sprite

914-GreyShip_1j.jpg?
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.

3 Answers

3

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

what is ssEditorTools? because giving error this name

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.

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

@rileydabozo: Try [this one][1]. There are many [tutorials][2] 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. [1]: http://lmgtfy.com/?q=alpha+channel [2]: http://www.youtube.com/watch?v=A9aZdPqs17M

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.94124-screen-shot-2017-05-15-at-104924.png

Let me know if this helps!

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?