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 ArtOfWarfare · Nov 07, 2014 at 03:33 AM · guiguiutilityguielement

Create my own non-compound GUI Control? (GUIElement?)

I would like to create a control in my GUI that allows the user to pick a color. What I have in mind is a rectangle full of colors (dynamically generated, not a bitmap, to ensure that it looks good on any screen).

I looked through the GUI documentation but didn't see anything that would allow me to do anything along these lines. I would expect that there's some kind of base GUI Widget or Box or View class that all of the other GUI controls inherit from, and I was figuring that I could somehow write my own decedent of that class which, when some display or render method is called on it, I could run through a loop of x and y positions and assign the colors to individual pixels, and then when a mouse is clicked, it could adjust a variable, or something like that.

Actually, just before I hit the "Ask Your Question" button I looked over the classes list one last time and noticed the classes GUIElement and GUIUtility... these both look like they might be relevant, but they seem like they're missing crucial parts to me. IE, there's no onDraw method that I can see, or anything like that.

I'd love if somebody could share an example of a custom non-compound GUI Control, or at least point out the essential methods that I need to use in order to pick the color for each individual pixel and draw it to the screen, as well as detect when the widget is clicked.

Comment
Add comment · Show 2
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 Kiwasi · Nov 07, 2014 at 06:15 AM 0
Share

The code for the standard colour picker should be out there somewhere. You could s$$anonymous$$l that.

avatar image ArtOfWarfare · Nov 07, 2014 at 11:40 AM 0
Share

@Bored$$anonymous$$ormon - I couldn't find anyone doing quite what I want, but I found that I can create my own texture pixel-by-pixel with Texture2D's setPixel() or setPixels() method. Then GaborD over in this thread (http://forum.unity3d.com/threads/making-a-color-wheel-color-picker.32760/) has a method for turning a Texture2D into color picker by applying it to a RepeatButton and tracking where in the button the mouse is when it's down. I'll try that, then if I can get it working, I'll post it as an answer here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArtOfWarfare · Nov 08, 2014 at 09:12 PM

Okay, this (Boo) worked pretty well:

 # Helper function for generating colors
 def channelValue(x as double, y as double):
     return (2.0 * x) - (x * y) + y - 0.9

 # Another helper function for generating colors
 def colorFor(x as double, y as double):
     return Color(x + y, channelValue(x, y), channelValue(y, x))

 # Function that utilizes the above two helpers to make a texture with the colors.
 def ColorPicker(maxX as int, maxY as int):
     ret = Texture2D(maxX, maxY)
     colors = array(Color, maxX * maxY)
     for x in range(maxX):
         for y in range(maxY):
             colors[x * maxX + y] = colorFor(1.0 * x / maxX, 1.0 * y / maxY)
     ret.SetPixels(colors)
     ret.Apply()
     return ret

 pickerTexture as Texture2D

 def Start():
     pickerTexture = ColorPicker(100, 100)

 def OnGUI():
     if GUI.RepeatButton(Rect(25, 25, 100, 100), colorPicker, GUIStyle.none):
         position = Event.current.mousePosition
         x = position.x - 25
         y = 125 - position.y  # For some reason the texture's Y values needed to be flipped
         print(pickerTexture.GetPixel(x, y))

This should nicely demonstrate how to make a unique GUI Control that neither looks nor behaves like a normal button, which is what I wanted.

Comment
Add comment · 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

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Rotation and Depth problem 1 Answer

rotate a single GUI element around pivotPoint 0 Answers

Position jitter when using GUIUtility.RotateAroundPivot 0 Answers

GUI GetLastRect in absolute coordinates? 2 Answers

s-pen GUIUtility.BeginGUI performance problems 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