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
0
Question by lkarus · Feb 21, 2016 at 09:43 PM · c#imagerecttransform

Trying to place an UI Canvas Image next to another UI Image

I've been trying to place 2 images on my canvas. Both image (ImageA, ImageB) will be side by side of each other. I get ImageA's width and move ImageB by half of the width. This is the code I currently have.

 // Define and assign posX and posY
 ImageA.GetComponent<RectTransform>().anchoredPosition= new Vector2(posX, posY);
 posX -= ImageA.GetComponent<RectTransform>().rect.width / 2.0f;
 ImageB.GetComponent().anchoredPosition= new Vector2(posX, posY);

However, the 2 Images are not exactly side by side to each other. Currently, ImageA and ImageB are a child object of gameobject that I am using to organize stuff on my inspector. Would these 2 images being a child of a parent gameobject interfere with positioning? The parent is placed at 0,0 on the canvas. Any help would be greatly appreciated.

Edit: A little more explanation. ImageA and ImageB do not have the same parent game object. The hierarchy looks like the following:

alt text

Both ImageA Container and ImageB Container has their anchors set to top left, and both ImageA and Image B has their anchor set to middle. All gameobjects has their pos X and pos Y are set to 0, 0 on the inspector, and ImageA Container and ImageB Container's width and height value under the Rect Transform component is set to 0,0 (since they are an empty game object without an image).

untitled.png (1.7 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
1

Answer by allenallenallen · Feb 21, 2016 at 11:28 PM

First, did you use widthOffSet for posX or something? Because it looks like you're setting ImageA and ImageB to be at exactly the same position. Shouldn't you do something like:

 ImageB.GetComponent().anchoredPosition= new Vector2(posX + widthOffSet, posY);

So that ImageB will actually have an offset?

Second, the empty GameObject will have an effect. The anchors for the two images will be at the parent position. But the main problem is that it's an empty GameObject not made for Canvas. I think it's best if you use an Image game object as a parent. Just remove the Image component from it so it still has RectTransform and Canvas Renderer components equipped.

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 lkarus · Feb 21, 2016 at 11:38 PM 0
Share

Sorry, updated the code snippet. But yes, I was changing the value of the position.

The parent game object had a RectTransform isn$$anonymous$$d of a transform coponent. I've manually added the Canvas Renderer Component to the parent game object. But it doesnt seem to have made a difference.

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

99 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

Related Questions

RawImage to world 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I set rectTransform properties of one image to another which differ in anchor and parent transforms (script)? 0 Answers

How to copy one Rect Transform data to other Rect Transform 2 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