Vertical Slider in EditorWindow

hi All,

I’m curious… is there a way to Draw a Vertical version of the Min Max Slider?

ex:

I rather not do…

		origMatrix = GUI.matrix;

		pivotPoint = new Vector2( rSlider2.x + 122, rSlider2.y + 138);
		GUIUtility.RotateAroundPivot( -90.0f, pivotPoint );	
		EditorGUILayout.MinMaxSlider(ref minVal, ref maxVal, minLimit, maxLimit, GUILayout.Width(256) );
		
		GUI.matrix = origMatrix;

…since the clipping of the slider can happen when the window is shrunk.

thanks all,
Izzy.

1 Like

No One? :sad:

If I give you the Draw Linear Gradient function, would someone help? :smile:

public static void DrawGradient_Linear( this Texture2D tex, Rect rArea, int nSteps, Color col1, Color col2 )
{
	int fromX = (int) rArea.x;
	int toX = (int) (rArea.x + rArea.width);
	int fromY = (int) rArea.y;
	int toY = (int) (rArea.y + rArea.height);
	float fStepSize = (float) rArea.width / (float) nSteps;
	float fCurStepPos = fromX;

	for( int x = fromX; x < toX; x++ )
	{
		if( x >= fStepSize + fCurStepPos )
			fCurStepPos += fStepSize;

		float fPercent = (fCurStepPos - fromX) / (rArea.width - fStepSize);

		Color c = Color.Lerp( col1, col2, fPercent );

		for( int y = fromY; y < toY; y++ )
			tex.SetPixel( x, y, c );
	}
}

and call it:

void Start ()
{
	int texSize = 256;
	Texture2D tex = new Texture2D(texSize, texSize, TextureFormat.ARGB32, false, true);
	tex.alphaIsTransparency = true;

	tex.FloodFillArea( texSize, texSize, new Color(0, 0, 0, 0) );

	Color color1 = new Color( .8f, .3f, .8f, 0.2f );
	Color color2 = new Color( .3f, .2f, .3f, 0.2f );

	tex.DrawCheckers( texSize, checkerSize, color1, color2 );

	int nRectSize = 64;
	int nSteps = 16;
	tex.DrawGradient_Linear( new Rect(0, 0, nRectSize, nRectSize), nSteps, Color.white, new Color(0,0,0,1) );

	tex.Apply();

	renderer.material.mainTexture = tex;
}

nSteps controls when the gradient color is used.

I found the DumpARGB32ToDisk in one of the forum threads and was wondering how that worked. (using System.Drawing.dll)

I tried it and it works. I think the Rows have to be Flipped Vertically because GetPixels32 starts reading from bottom left , and Bitmap reads it from top left. :slight_smile:
Is this code below correct? Did i miss anything?

...
	void Start ()
	{
		int texSize = 128;		
		int checkerSize = 64;		
		//int circles = 10;

		Color color1 = new Color( .8f, .3f, .8f, 0.2f );
		Color color2 = new Color( .3f, .2f, .3f, 0.2f );

		Texture2D tex = new Texture2D(texSize, texSize, TextureFormat.ARGB32, false, true);
		tex.alphaIsTransparency = true;

		tex.FloodFillArea( texSize, texSize, new Color(0, 0, 0, 0) );

		tex.DrawCheckers( texSize, checkerSize, color1, color2 );

		int nRectSize = 64;
		int nSteps = 16;
		tex.DrawGradient_Linear( new Rect(0, 0, nRectSize, nRectSize), nSteps, color1, color2 );

		tex.Apply();
		renderer.material.mainTexture = tex;

		Color32[] pix = tex.GetPixels32();
		int cSize = System.Runtime.InteropServices.Marshal.SizeOf( typeof(Color32) );
		byte[] pix4 = new byte[ pix.Length * cSize ]; 
		int row, col, rowOffset;
		int targetRow, targetRowStart;
		int rowPixels = tex.width * cSize;

		for( int i = 0; i < pix.Length;  )
		{
			row = i / tex.width;
			col = i % tex.width;
			targetRow = tex.height - row;
			targetRowStart = (targetRow - 1) * rowPixels;

			for( int j = targetRowStart; j < targetRowStart + rowPixels; j += cSize, i++ )
			{
				pix4[j] = pix[i].r;
				pix4[j+1] = pix[i].g;
				pix4[j+2] = pix[i].b;
				pix4[j+3] = pix[i].a;
			}
		}
		
		DumpARGB32ToDisk( pix4, (uint) tex.width, (uint) tex.height );
	}

	public static void DumpARGB32ToDisk(byte[] argb, uint width, uint height)
	{
		System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
		System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
		System.Runtime.InteropServices.Marshal.Copy(argb, 0, bmpData.Scan0, argb.Length);
		bmp.UnlockBits(bmpData);
		bmp.Save("Assets/_GeneratedImage.png", System.Drawing.Imaging.ImageFormat.Png);
	}

Ehhh… No Help?

Ok.
I guess i’ll start working on a vertical min max slider.

so far:

1 Like

I was going to write a reply about manually setting the rect (using GUI not GUI Layout), but it seemed pretty clear that you know what you are doing so this was considered and rejected :wink:

Little note, System.Drawing.dll does not work on Mac/Linux. So careful if your tool is to be multi-platform.

hey johnny

You’re the Best. ;D

Ohhh?
I took the “C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll”
…and dropped it in the “Assets/Plugins” folder in my project.

I thought i read that some got it to work on a MAC with the Mono dll. (extracting it from the *.app container)
Not sure about Linux. :confused:

Hmm… Ok.
Know where i can get some more SetPixel Drawing routines that are in c# source form? :slight_smile:

I’m putting together a .CS that has a few Texture2D Drawing Functions
Here is what ive scavenged from the forums, wiki’s, etc…
link: Unity Texture2D Extra Drawing Functions - Pastebin.com

System.Drawing.dll, like many .NET library got references towards other libraries. It’s possible to make some method in it to work, while some don’t. In our case, we were trying to load a .PNG from a custom DLL and sadly the library had external reference that were not supported by Mono.

Unity already allows you to change pixel, read and write textures. Which feature is missing for what you need?

:slight_smile:

Everything that CAIRO does would be Sweet! :wink:
If you can find functions that do what Image Magick does… even better. ;D

But for now, i’d settle for simple functions like drawing a Filled Triangle (5 or more sides preferably), and possibly apply a Gradient as the Fill type.

Ohhhh… a .DrawTEXT() function that would draw to a Texture2D. Maybe pass in an existing Font Settings asset Name as a parameter, and it can look up and use the correct texture/material for that already imported font. Might need to use Font.GetChacterInfo and do a GetPixels() and draw each letter to the Texture2D?!?
I MUST HAVE that! :o

And later, i’m hoping to use a procedural approach to create textures for a slew of particle effects (atlas… UV offset, etc…), including masks for some of the shaders or decals or whatever is needed.

Re: The OP
Sadly the editor skin is lacking in terms of vertical sliders, the regular GUI.Vertical slider uses the in-game skin images, not the slick, minimal editor-color-scheme bars.
Interestingly, there is a “MiniMinMaxSliderVertical” in the editor skin, but since you can’t use a style parameter with minmax sliders, IDK what it’s for.

You can’t even fake one by overlapping 2 regular vertical sliders, since one of them will never take focus.

As for these other ‘features’, surely better to leave them to an actual image editor? (tho you’re about spot on for adding text to a texture)

Before i start on my custom MinMax Slider… i wanted to try some other things 1st.

So far:

Vertical MinMax slider
I’ve done another version that doesn’t rely on the private bools, and (i think) is closer to what Unity does internally with the other sliders, but its twice as much code.