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
1
Question by Magnomous · May 21, 2015 at 04:47 PM · uicolorimagesetpixel

SetPixel() for UI Image?

Hello,

is there a way to edit pixels on UI Image? Lets say I want to make an application that has white full-screen Image and user can paint pixels on black by holding left mouse button. In the inspector I would create a UI image, set its color to white and make it stretch to full-screen. Then lets create a script.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PaintController : MonoBehaviour {
 
     public Image screenImage; // assigned in the inspector
 
     void Update() {
 
         if(Input.GetMouseButton(0)) {
 
             // and here would be something like SetPixel() that Texture2D has. Something like:
             screenImage.SetPixel(Input.mousePosition.x, Input.mousePosition.y, Color.black);
         }
     }
 }
 

However, Image does not contain SetPixel function so I wonder. How could I make it? Thanks for any help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Ramazoid · May 26, 2017 at 05:13 AM

@Magnomous use material property of Image Component:

 Texture2D texture = new Texture2D(512, 512);
     GetComponent<Image>().material.mainTexture = texture;
 and then textere.SetPixel(x,y,color)

don't forget

 texture.Apply(); 

after all

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

Answer by jamesflowerdew · May 21, 2015 at 07:08 PM

errr... how about "public Texture screenImage;"?

or "public Texture2D screenImage;"

your texture would have to be read write enabled, but that would work..

also you'd have to compare mouseposition xy with screen width/height or your image gui rect stuff and then multiply by texture width/height to get proper coords for setpixxel, but other than that it should be fine :)

Comment
Add comment · Show 2 · 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 Magnomous · May 22, 2015 at 03:30 PM 0
Share

Thanks for response, but I don't want to use old UI. I want to have new UI Image. There must be some way of doing it.

avatar image jamesflowerdew · May 25, 2015 at 07:39 PM 0
Share

Hi, sorry if that didn't help. As a note you don't need to use gui, it could be a 3d plane. I had a quick look at the new Image class and can see no getpixel equivlents either. Unity's global fog class has a mesh drawn to exactly match the camera in it if that's helpful. We've used getpixel both in onGUI environments and 3D meshes and using editable textures (with alpha chanels) works well. At a guess you could enginner your own Graphic class that allows texture2D input, but that sounds a bit hardcore to me.

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

21 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

Related Questions

How to access color & texture functions of a "RawImage Script" simontaneously?? 0 Answers

Change the button target image alpha 1 Answer

Material doesn't have a color property '_Color' 4 Answers

How to store colors for future use 2 Answers

Changing two different objects renderer colour 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