GUITexture component's border settings only stretching left? - c#

I am having trouble using the GUITexture component's border settings, in the inspector I have set leftBorder, rightBorder, topBorder, and bottomBorder, to specify how many pixels on each edge will be unstretched.
However this is the result, as you can see my image only correctly stretches to the left.
And this is even though I have correctly (I think?) sliced the sprite as following.
Incase it helps here are the settings on the rect transform that I use to hold the image.

I solved the question, turns out when importing my spritesheet I had to turn off "Generate Mip Maps"

Related

Properly Scale UI Images in Unity

I have a png that's size 150x100 , and I set the UI image to the same, but it makes a bunch of extra space around it (that can be interacted with). How do I fix this?
Image of Problem: https://imgur.com/a/2ILXY1t
Unity isn't adding extra space. The image itself HAS that space.
There are options to crop out the alpha space in Unity by using the sprite editor, but by my experience i prefer using a proper Image editor like Gimp. using one is the best way to handle your image assets.
To crop out the extra space you just have to reduce the canvas size.
Well, first you could check (in Unity) wether your Image has its property Preserve Aspect set to True.
You could click Set Native Size which is right below it, so the 'box' around your image will take it's size.
Edit: Nevermind the first two. I do not know why i thought they could solve it, i looked at your image again and i, too, think there are transparent pixels above and below it. So you should try this:
Then you could check whether your picture has any transparent pixels around it, using an image editor. If it has, you would need to cut them out.

XNA sprite loses pixels on a very pixelated image

So this may be a bit vague, but I'm having some issues. Basically, I am drawing my little ninja sprite (16x16px) and on 2 frames of his animation, he loses pixels on his headband. I have looked everywhere in an attempt to find an answer, but to no avail.
Original Sprite Sheet (Bugged frames are 4th row, 3rd and 4th frames):
After a little more investigation, it seems that the bugged pixels are due to the width of the sprite sheet. It almost seems random what pixels get messed up depending on the width. However, with a constant width, the messed up pixels are constant regardless of creating a new build or using an old one.
As you can see, the back of his headband is missing pixels. While it may not be game changing, it is very annoying and if anyone knows of a fix, that'd be great! Thanks guys!
My guess is that those pixels are magenta (255,0,255) which is automatically keyed out of the image as transparent by the SpriteTextureProcessor. If that is the case, change the pixels to be a slightly different color (255,1,255) or use the TextureProcessor for the content. You can do this in the properties window of the image file in your content project.
So a workaround I found is actually changing the size of the rectangle used to display the image. It was set to 100, setting it to 99 works and there is barely any visual difference.

How can I programmatically load a texture into an Image the same way the Unity Editor does?

In the image above, the first image is loaded via C# script. The second is assigned via the inspector in Unity editor. Note the dark gray border around the first image. How can I load the image via C# and have it not have the border?
The source image is a white-on-transparent PNG 512x512 pixels. It's being displayed in an UnityEngine.UI.Image sized at 30x30 with a red color assigned. The source image is identical (same location on disk) for both examples above.
The code I am using for the first image is as follows;
var texture = new Texture2D(512, 512);
texture.LoadImage(File.ReadAllBytes(Path.Combine(TexturePath, name)));
image.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(.5f,.5f), 100);
where image is the appropriate UnityEngine.UI.Image.
Note
The advantage of using the code above is that the images do not need to be embedded in the game that unity ends up building. It means these images can be distributed separately from the game. Using Resources.Load does not cater for this, and I suspect, is the same as assigning the image via the inspector, meaning that unity has already done something to the texture prior to assignment (likely something by the UnityEditor.TextureImporter)
Update
I investigated the Texture2D constructor some more and determined that the following code results in the image above, where the edges of the sprite no longer have the grey border, but now appear jagged. (Setting the last parameter to true retains the grey border).
var texture = new Texture2D(512, 512, TextureFormat.Alpha8, false);
Some googling has me thinking that the issue is mipmap related, and that the Unity Editor may be resolving this on import due to whatever occurs with UnityEditor.TextureImporter.borderMipMap as seen here. However, the UnityEditor namespace is not available when building the project.
The issue is that the PNG format uses a non-premultiplied Alpha and Unity uses straight alpha blending designed to work best with pre-multiplied alpha colors.
Better in-depth descriptions of Pre vs non-Pre can be found from:
NVidia
Microsoft
A Unity-specific discussion can be found Here (however, note this problem has nothing to do with mip-mapping, but can be exacerbated by filtering and resizing techniques)
You can also look to Unity's documentation on alpha importing to see a visual example of a common solution to this problem if you have access to pipeline-side creation of these PNGs (I have typically solved this by applying a post-process to modify PNGs after/during their creation).
Using a cutout shader is also a solution though it can result in jaggy/visual artifacts.
You would think rendering a sprite using the features of Unity's primary supported runtime load image file format would be a simple affair but, alas, it is quite a bit more complicated than that. I don't know the inner workings of UnityEditor.TextureImporter.borderMipMap but I suspect its inception is built around a similar problem (weight between the edge pixel alpha and any neighbors used in the filtering)
Use Resources.Load, thus you can configure the texture settings in the editor.

Cropping a Bitmap to an Area of Interest

Similar to my previous question which I as yet have not solved (Comparing Frames of a live Feed) I have another issue.
Scenario
I have an image taken by a camera that contains a rectangle in it. I need to crop the image to only show the rectangle plus a small margin.
My Efforts
I have accomplished this by iterating through the pixels using LockBits and attempting to find potential edges but these seems terribly slow and inefficient
My Thoughts
I was thinking I could take an empty image as a baseline and then remove the differences between the two, however I cannot be sure that the lighting will be exactly the same and that potential contaminants such as an accidental fly getting into the image will not be present which could muck up this process.
Is there any easier way? The rectangle should (usually) be in the bottom left corner, but not always (long story) but this cant be relied upon.
My Environment
Visual Studio 2012 (2010 if neccessary is available)
Ueye camera
C#
The images are of type System.Drawing.Bitmap
The rectangle will often be something like a credit card or an ID card or anything of a similar size and shape
The empty image (background) looks like this:
Using EmguCV you can detect shapes such as a rectangle. Click here for the emgu code. Once you have detected the rectangle it is fairly easy to crop it out using a new Bitmap with the size of the rectangle.
The sample demonstrates how to crop the image from specific Picturebox control into destination Picturebox control using mouse selection or specified coordinates.
1.How to use mouse to select an area (rectangle) in a Picturebox control.
2.How to crop the image by the rectangle.
http://code.msdn.microsoft.com/windowsdesktop/CSWinFormCropImage-d4beb1fa

How to load textures to XNA?

Alright, so I need a texture for a game that I'm making and I found a texture that fit to my project(some kind of ball). The texture is next to other textures(Textures sheet).
So I had to download all the sheet. Now, I need to get only the texure that I want (that ball). And I can only circle it by a rectangle(using Paint). Is it true that XNA has an ignorant color? Or there's any other way that I can load ONLY MY BALL? and not the background of it?
Please, I really need an answer for it.. Thanks alot.
To enable colour-key transparency, select your texture in your content project and press F4 to bring up the properties window. Expand the "Content Processor" option to reveal the colour key settings. Turn on colour key and select the appropriate colour.
If you need to use a sprite sheet within XNA (ie: if you're not cropping it in your image editor), you can use the sourceRectangle parameter to SpriteBatch.Draw to treat only a particular rectangular region of your source texture as a sprite.
By default SpriteBatch will draw with BlendState.AlphaBlend (ie: if you use the Begin() overload, you don't need to set it), which will correctly handle the transparency for you.

Categories