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 Totoro83y · Apr 05, 2018 at 05:13 PM · uisliceslicing

UI element large as sliced sprite internal rect

Hi. I want to have a RectTransform exactly large as the internal rect of a sliced image. The problem seems almost simple, you have to put the anchors as 0 and 1 and put some value for padding, but when the canvas has a canvas scaler, it can change the size of the borders, so the solution seems to be the a script. I started with the following one:

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(RectTransform))]
 public class FillSize : MonoBehaviour
 {
     private void Start()
     {
         var borders = GetComponentInParent<Image>().sprite.border;
         var rectTransform = GetComponent<RectTransform>();
 
         rectTransform.anchorMin = Vector2.zero;
         rectTransform.anchorMax = Vector2.one;
 
         rectTransform.offsetMin =  new Vector2(borders.x, borders.y);
         rectTransform.offsetMax = -new Vector2(borders.z, borders.w);
     }
 }

It almost works, but the offset should be scaled by a factor specifying how much pixels are large in the RectTransform. It is possible, obviously, to do the math with the values of the canvas scaler (even if I didn't totally understand the reference pixels per unit yet), but it's very complex because the scaler and the hierarchy can have a lot of different settings, and I should consider all of them. So, is there a different path?

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

Answer by Totoro83y · Apr 05, 2018 at 06:35 PM

I think I solved the problem, the factor to scale the offset seems to be the one obtained by canvas.referencePixelsPerUnit / sprite.pixelsPerUnit, so the full code is something like this:

 using UnityEngine;
 using UnityEngine.UI;
 
 [ExecuteInEditMode]
 [DisallowMultipleComponent]
 [RequireComponent(typeof(RectTransform))]
 ///Use it under a Sliced or Tiled UI.Image to have the RectTransform on the
 ///GameObject containg this script filling the space inside the former Image
 ///inner rect
 public class FillInSlicedImage : MonoBehaviour
 {
     private void Start()
     {
         Fill();
     }
 
     ///Call it when the borders could be changed (main cases are when the
     ///sprite changed, when the referencePixelsPerUnit in the canvas changed
     ///or when something in the hierarchy that can change borders changed)
     public void Fill()
     {
         var image         = transform.parent.GetComponent<Image>();
         var canvas        = GetComponentInParent<Canvas>();
         var rectTransform = GetComponent<RectTransform>();
         var sprite        = image.overrideSprite ?? image.sprite;
         var border        = sprite.border;
 
 #if UNITY_EDITOR
         if (image.type != Image.Type.Sliced && image.type != Image.Type.Tiled)
             throw new System.Exception("Parent Image not Sliced or Tiled");
 #endif
 
         rectTransform.anchorMin = Vector2.zero;
         rectTransform.anchorMax = Vector2.one;
 
         border *= canvas.referencePixelsPerUnit / sprite.pixelsPerUnit;
 
         rectTransform.offsetMin =  new Vector2(border.x, border.y);
         rectTransform.offsetMax = -new Vector2(border.z, border.w);
     }
 
 #if UNITY_EDITOR
     private void LateUpdate()
     {
         if (UnityEditor.EditorApplication.isPlaying)
             return;
 
         try
         {
             Fill();
         }
         catch
         {
         }
     }
 
     [UnityEditor.Callbacks.PostProcessScene]
     private static void OnPostProcessScene()
     {
         try
         {
             foreach (var o in FindObjectsOfType<FillInSlicedImage>())
                 o.Fill();
         }
         catch
         {
             Debug.LogError("FillSize must have an Image (the first one if  "  +
                            "many) of type Sliced or Tiled in the parent and " +
                            "a Canvas in a grandparent");
         }
     }
 #endif
 }
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

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

Related Questions

Sliced Spritesheet appears correctly in Editor, overlapping everywhere else 2 Answers

Why are these 9-slice UI images not showing any borders? 1 Answer

GUI on a secondary display 0 Answers

Force a uGUI scrollbar/scrollrect to stay at the bottom when the scrollrect is expanded 0 Answers

UI Clicking thought alpha of image 3 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