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 Jockey4her · Apr 16, 2013 at 11:42 PM · drawtexturesetpixel

Draw a pixel and change its color in code

I'm trying to draw a single GUI pixel, and be able to change its color. I tried this but I cannot figure out how to change the color. brush is a public Texture.

 GUI.DrawTexture(new Rect(100,100,1,1),brush,ScaleMode.StretchToFill,false,0);

I tried changing brush to a Texture2D, then using brush.SetPixel(0,0,Color.red); but the compiler says 'texture is not readable, the texture memory cannot be accessed from scripts.

There's probably a really simple way to draw a single pixel and set the color. Let me know what that is please.

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
2
Best Answer

Answer by iwaldrop · Apr 17, 2013 at 02:37 AM

You could create your own texture and change it's values, or you could set the texture to readable.

To do the former, just make a new texture:

 private Texture2D brush;
 private int size = 256;
     
 void Awake()
 {
     brush = new Texture2D(size, size);
     ChangeTheColor(Color.red);
 }
     
 void ChangeTheColor(Color c)
 {
     for (int x = 0; x < size / 2; x++)
         for (int y = 0; y < size / 2; y++)
             brush.SetPixel(x, y, c);
     brush.Apply();
 }
     
 void OnGUI()
 {
     GUILayout.Label(brush);
 }

For the latter, browse to your texture import settings, set 'Texture Type' to 'Advanced', and check 'Read/Write Enabled'.

Comment
Add comment · Show 4 · 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 Jockey4her · Apr 17, 2013 at 03:48 PM 0
Share

Thank you! I tried both concepts. Your first suggestion is the best for me, because the brush takes the exact color you specify, and this application only needs solid colors.

When I tried your latter solution, making the texture asset readable/writable, that worked also BUT, the color you Apply() overlays the color of the original asset. So you don't get the pure color. There is some mixing going on.

So solids, approach 1, fabrics and complex textures, approach 2.
Happiness.

avatar image juanitogan Jockey4her · Oct 20, 2017 at 08:42 PM 0
Share

I wish Apply() had some sort of blending mode like this, but it doesn't. Something else must have been causing it, outside of what has been described here.

avatar image iwaldrop · Apr 17, 2013 at 03:55 PM 0
Share

Great to hear that you've gotten it to work! Awesome!

Could you do the board a favor though and accept the answer so this question will be marked as answered? Thanks, and happy coding! :)

avatar image whydoidoit · Apr 17, 2013 at 03:57 PM 0
Share

And please don't post comments as answers - on UA Answer means Solution and not Reply.

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

14 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

Related Questions

GUI.DrawTexture not working 2 Answers

How to change rect size? 2 Answers

[Closed]GUI.DrawTexture inside GUI.DrawTexture 1 Answer

Why doesn't my texture show up? 1 Answer

create readable colour map 1 Answer


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