Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by peter_llamazoo · Jun 01, 2016 at 02:42 PM · uiinputfieldmaskscrollview

UI ScrollRect mask not hiding Input Field Caret and Highlight

Hello There!

I'm working on some UI element setups and have a ScrollRect that contains an input field within its viewport. Whenever I input a bunch of text into the input field and then scroll, the text gets masked. However the caret and any highlighting bleeds through the mask. I've looked around and only found a post from 2015 with this issue with no answers so I'm wondering if anyone's found a fix since then.

I've attached a picture showing the problem.

Thanks!

alt text

unit-text-highlighting-bug.png (21.2 kB)
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
4

Answer by zxkne · Feb 06, 2017 at 11:46 AM

Seems that the issue is still not fixed in Unity 5.5. Issue Tracker says that it is in 5.5.0 but I just checked Unity 5.5.0f3, and the bug is still here. Maybe it's fixed in patch releases, I don't know. But anyway, there may be people who can't upgrade to latest version, so, here is a workaround I found.

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(InputField))]
 public class FixInputSelectionMasking : MonoBehaviour {
     InputField _inputField;
     Transform _caret;
 
     public void Awake() {
         _inputField = GetComponent<InputField>();
     }
 
     public void Update() {
         if (!_caret) {
             _caret = _inputField.transform.Find(_inputField.transform.name + " Input Caret");
             if (_caret) {
                 var graphic = _caret.GetComponent<Graphic>();
                 if (!graphic)
                     _caret.gameObject.AddComponent<Image>();
             }
         }
     }
 }

Put this script on an InputField and the selection should be masked as expected: alt text

I've tested it in 5.4.4f1 and 5.5.0f3.

Some explanations

InputField on the fly creates a GameObject to render the selection and the caret. This object has a CanvasRenderer component and renders its geometry directly. I think that the issue is caused by that CanvasRenderer by itself doesn't know anything about masking. It doesn't update shader properties, related to masking (_ClipRect for Mask Rect 2D and _Stencil* for Mask).

The script that is shown above adds a dummy Image component to the GameObject which renders the selection. Image is a Graphic and it knows how to deal with the masking. I'm not sure, that this fix will work in all cases, I haven't dug too much into implementation details of Image and InputField. I suppose that theoretically Image may override the selection geometry generated by InputField but, as I understand it, InputField regenerates the selection geometry each frame (in CanvasUpdate.LatePreRender handler).


inputfield-fix-01.png (17.1 kB)
Comment
Add comment · Show 5 · 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 onatangent · Feb 28, 2017 at 08:13 AM 1
Share

I don't have enough permissions to post a comment, @zxkne, so I'll post my thanks to your solution here.

I do have one observation, though. When will _caret ever be non-null?

avatar image zxkne onatangent · Feb 28, 2017 at 09:36 AM 0
Share

@onatangent, you're right, _caret usage was absolute nonsense. I've edited code in the answer. The original idea was to remember the caret so that Transform.Find() isn't called every frame (but is called if the caret was destroyed somehow). Thanks for pointing it out!

avatar image Harinezumi · Mar 29, 2017 at 10:08 PM 0
Share

I've just tested it on Unity 5.5.2f1, this solution works perfectly! It should be marked as solution (for now).

avatar image JuliuszK · Aug 15, 2017 at 10:20 PM 0
Share

Thanks for this magic trick! It saved my day!

avatar image BIPINGGDEV · Jan 09, 2018 at 06:02 AM 0
Share

Thanks a lot. You save the day for 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

9 People are following this question.

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

Related Questions

Hide Textselection in Scrollable Inputfield 1 Answer

new UI Mask soft cutoff 3 Answers

TextMeshPro InputField masking not working 0 Answers

stopping scrollrect if it is about to leave the desired space 0 Answers

ScrollView mask object in seperate canvas 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