Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by AndresPGC · Jan 06, 2017 at 02:21 PM · imageimage effectsfilledge detection

Detect fill amount edge

[Unity 5.5] Hello guys, I would like to know if there's a way to detect the edge of the fill amount of a sprite.

Example: There's an experience bar (an Image component with 'Image Type' as 'Filled') with width value set to 100 and the fillAmount is set to 0.3 (30%), and another Image called 'edgeImg'.

I would like edgeImg to always follow the edge of the experience bar's fill amount, that is, in this case, edgeImg would be at x position 30

EX: http://image.prntscr.com/image/54eb67d82a964731a4b2768a667a509e.png

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

Answer by KoenigX3 · Jan 06, 2017 at 03:12 PM

I have a solution, you will have to optimize it for the perfect appearance.

Create two gameobject variables called 'img', and 'edgeImg' - these will represent the experience bar and the edge. The 'edgeImg' should be a child of 'img', and the anchor preset should be at the middle center.

You only need to use the Update() method:

 void Update () 
     {
         edgeImg.GetComponent<RectTransform>().localPosition = new Vector3(img.GetComponent<Image>().fillAmount * 100 - 50, 0, 0);
     }

It is not perfect however. If the experience bar image has empty pixels at the two ends, the edge image will slide too far. If you can not modify the image itself, you can modify the code.

The X component consists of the fillAmount, and two integers: 100 and 50. 100 stands for the width of the image and 50 stands for the maximum amount of slide from the center. If the image has like 10% empty space at both ends, you should change 100 to 80, and 50 to 40. If you want to optimize this, you should change these two integers to two public variables and change them through the Inspector at runtime.

Comment
Add comment · Show 1 · 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 AndresPGC · Jan 06, 2017 at 04:31 PM 3
Share

Thanks for the reply, this worked perfectly. Here's the full code, to help others people with the same question:

     private Image _image;
     private RectTransform _edgeRect;
     private RectTransform _imgRect;
 
     //You can edit this on inspector, to set up the margin.
     public float Edge$$anonymous$$argin = 20;
 
     /// <summary>
     /// If <c>true</c> then the image edge will be hidden when the fill amount comes to 0
     /// </summary>
     public bool HideWhenNotFilled = true;
 
     void Awake()
     {
         _image = GetComponent<Image>();
         _imgRect = GetComponent<RectTransform>();
         _edgeRect = transform.GetChild(0).GetComponent<RectTransform>();
     }
 
     void Update()
     {
         if (_image.type != Image.Type.Filled) return;
 
         _edgeRect.gameObject.SetActive(!(_image.fillAmount == 0  && HideWhenNotFilled));
         _edgeRect.localPosition = new Vector2(_image.fillAmount * _imgRect.rect.width - Edge$$anonymous$$argin,
             _edgeRect.localPosition.y);
     }

avatar image
0

Answer by Slashik · Mar 25 at 09:26 PM

 float width = lProgress.GetComponent<RectTransform>().rect.width;
 Vector3 tempV = indicator.GetComponent<RectTransform>().anchoredPosition;
 tempV.x = -width/2;
 tempV.x += width * lProgress.fillAmount;
 indicator.GetComponent<RectTransform>().anchoredPosition = tempV;

Faced this problem today. This worked out for me. Hope it helps.

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

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

Related Questions

Can`t animation fillAmount via script(C#) 0 Answers

Is It Possible To Load 2 images And Compare Them using Edge Detection 0 Answers

Mathf.Lerp crashing build? 1 Answer

Como rellenar Image.fillAmount usando 270º 0 Answers

Managing multiple images fillamount by one function? 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