Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by monkeyThunk · Jan 18, 2013 at 11:14 PM · guieditorslowtilingsetpixels

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image monkeyThunk · Jan 19, 2013 at 04:46 AM 0
Share

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.

alt text

ninepartsetpixel.png (15.2 kB)
avatar image robertbu · Jan 19, 2013 at 05:58 AM 0
Share

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.

avatar image monkeyThunk · Jan 19, 2013 at 08:10 AM 0
Share

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)

avatar image robertbu · Jan 19, 2013 at 03:37 PM 0
Share

It is not Pro.

avatar image robertbu · Jan 19, 2013 at 07:34 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges