- Home /
Fastest way to tile in Editor GUI?
I'm doing nine part drawing in an Editor Window for a custom GUI where the non-corner parts are tiled not stretched.
I've got something working, but it seems to be too slow, so I'm hoping that there is a better approach.
I'm currently using GetPixels / SetPixels. For example, a three-part tiling function that is working looks like this:
public static void CopyPixelsHorizTiled( Texture2D srcTexture, int srcX, int srcY, int srcWidth, int srcHeight,
Texture2D dstTexture, int dstX, int dstY, int dstWidth)
{
int copies = dstWidth / srcWidth;
int extraWidth = dstWidth % srcWidth;
int dx = 0;
if (copies > 0) {
Color[] srcBlock = srcTexture.GetPixels(srcX, srcY, srcWidth, srcHeight);
for (int i=0; i < copies; i++, dx += srcWidth)
dstTexture.SetPixels(dstX + dx, dstY, srcWidth, srcHeight, srcBlock);
}
if (extraWidth > 0) {
Color[] srcExtraBlock = srcTexture.GetPixels(srcX, srcY, extraWidth, srcHeight);
dstTexture.SetPixels(dstX + dx, dstY, extraWidth, srcHeight, srcExtraBlock);
}
}
The problem is that it seems very slow. When I change the GUI element size, I need to call texture.Resize(), then redraw, and then texture.Apply(). Even with a 200 x 200 texture on my Powerbook Pro, that ends up making the Editor Window very jerky when dragging it onscreen (when I update the size every frame in onGUI())
Note: I'm not using a GUI Style because my understanding is that it would stretch the middle and edges instead of tiling them.
Is there a better way?
Answer by robertbu · Jan 19, 2013 at 01:35 AM
From your description, I don't have a solid understanding of the problem you are trying to solve. A picture might be helpful, and maybe a better understanding of how the corners are handled. Off the top of head, two ideas come to mind. The first to embed a second camera veiw. The second camera would look at a tiled surface. As the user increases the size of the window, change the Viewport rect and any other settings needed to keep the tiles the same size.
The second idea is to use a texture atlas, a custom plane, and to manipulate the mesh and UV coordinates directly. This one is digging deeper into Unity, but if I understand the problem correctly, it would give you the performance you need.
Here is a picture that should help. The 64x64 test image on the left is the sourceTexture. To it's right is the same source texture drawn as a nine part with tiling. So the corners are the same size as the source image, the top and bottom edges tile horizontally, left and right tile vertically, and the center tiles horizontally and vertically. The image on the right shows that partial tiles fill in as needed for the edges and center.
I do need the solution to work well with the Unity GUi, for example it should be clipped by the Begin/EndGroups. (It currently does since it draws the resulting Texture2D.)
It feels like what I really want is a way to draw a texture with a source rect not to another texture, but to the screen, but I can't figure out how to do that.
This is what I guessed you were doing based on a quick read of "nine part drawing." Playing nice with Begin/EndGroups is a difficult criteria. There is one way that might work for you. Graphics.DrawTexture(). You specify where on the screen you want to draw a texture and a region of a source texture. Assu$$anonymous$$g you can calculate the screen position, you would not need resize textures or move pixels at all...just reset the region in the source texture. Note the comments on its use in OnGUI in the reference.
That looks like it would work, but I've seen mention that it is a Unity Pro feature, although I don't see that mentioned in the API doc. Do you happen to know if it is Pro only? ( I have Pro, but I was wanting to create an editor that doesn't require it)
This newer post I think has the best answer for your problem yet. It shows how to do a texture atlas with GUI. You can just create your background and then scale your view into you atlas to tile. Should work nicely for the sides as well.
http://answers.unity3d.com/questions/383654/texture-atlas-with-gui-problem.html
Your answer
Follow this Question
Related Questions
How to know if something is selected in Hierarchy or in project. 5 Answers
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers
Add EditorUI elements when a button is pressed 0 Answers
EditorGUI like light explorer window 1 Answer
Editor -> Popupwindow -> OnLostFocus -> Close() -> Get Windowlayouts error, wait what? 0 Answers