• 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 SovietSpartan · Oct 12, 2016 at 03:24 AM · uiui imagemaskpixeloverlapping

Getting the overlapping pixels between a UI mask and a UI image

Hello! I'm making a system where the user imports an image from their hard drive and then masks it with a white circle image (completly transparent on the outer sections of the circle) and then saves the resulting masked image.

However, the only problem i have right now is that i can't find a way to get the overlapping pixels between a UI mask and a UI image. What i need is to get the pixels from the UI image that overlap only the white circle portion of the UI Mask image, and with that, create a new texture.

This is how my UI setup looks like.

"Mask", well, is the mask image, and "TargetImage" is the image that's being masked and you can see inside the circle fully opaque. I need to get the pixels from "TargetImage" that are overlapping the mask image's circle and then create a new texture the same resolution as the mask's texture of the masked "TargetImage".

Any help on this is certainly appreciated!

Comment
Add comment · Show 4
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 Twin-Stick · Oct 12, 2016 at 01:57 PM 0
Share

Hey there, I'm not sure if I fully understand what you're wanting - but to me it sounds like you have a mask image which you want to use and cut out only what is relevant from the user image?

Here is some code that may help - you would need to correctly set up your mask image prior with alpha however this should do the job... Let me know how you go!

 Texture2D userImage;
 Texture2D mask;
         
 // check image size, resize if required
 if (userImage.width != mask.width || userImage.height != mask.height)
 {
     userImage.Resize(mask.width, mask.height);
 }
 
 // Set alpha from mask into userImage        
 for(int i = 0; i < userImage.width * userImage.height; i++)
 {            
     int y = i / mask.width;
     int x = i - (y*mask.width);
     Color col = userImage.GetPixel(x, y);
     col.a = (mask.GetPixel(x, y)).a;
     userImage.SetPixel(x, y, col);
 }
Show more comments
avatar image SunnyChow · Oct 13, 2016 at 05:21 AM 0
Share

Have you try using a camera with transparent color as clear flag, and render it to a render texture? i guess it should keep the transparency of the ui elements.

 RenderTexture.active = renderTexture;
 texture2D.ReadPixels (new Rect(0,0, renderTexture.width, renderTexture.height),0,0);
 texture2D.Apply ();
 RenderTexture.active = null;

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI Masking (Type visiblity of the content outside and inside of mask ) 0 Answers

Create UI Mask from Childrens images 2 Answers

Poke hole in UI Image? 1 Answer

UI image with button component not tracking mouse correctly 1 Answer

How to click on irregular buttons in new ui 4 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