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 zMalik · Jun 12, 2017 at 04:54 PM · canvasui image

How to make an Image's position fixed on Canvas

Hello,my name is Saulo and I need some help with my UI.

So,I have this game that you can use a npc to go to a place and build a tower on top of it.When the builder arrives the place,I instatiate a "loading" UI such as the image below to derteminate when the building will be complete:alt text

The problem is,I have an Script to move the camera,and since I'm instantiating the UI on the Canvas,when I move my camera,everything moves together: alt text

And I get this horrible effect of a loading bar floating around. I would like to know how can I make my image to have a position fixed in the world and not to be related to the canvas position. So if I move my camera,the UI loading bar doesn't come with it.

I know there's an option on canvas to set it to be rendered as world space.Altough,as you can see I have other UI elemets that I would like to keep it with the camera,such as the scoreboard.

Thank you very much.

a.jpg (142.1 kB)
b.jpg (137.1 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zMalik · Jul 23, 2017 at 10:02 PM

Ok,so I apologize for my inconvinience. It turned out that it was simplier than I tought .As @hexagonius said,we can create more than one canvas in the scene.So,I just created another canvas(Canvas2) and set it's RenderMode to "World Space".Also,as my game is top-down camera,I rotated this canvas 90º on the x axis and adjust its size so it could cover all my map.After that I was looking for a way to convert the world space coordinates (where my timer would be instantiated) to screen coordinates and then instantiate the object on the canvas.Altough,as Canvas2 is set to world space,we don't have to convert anything(And took me a lot of testing to come up with that). If any of you are interested in this,here's my final code:

 public class FloatingTimeBuildingController : MonoBehaviour {
     private static FloatingTimeBuild popupTimer;
     private static GameObject canvas;
 
     public static void Initialize(){
         canvas = GameObject.Find ("Canvas2");
         if(!popupTimer)
             popupTimer = Resources.Load<FloatingTimeBuild> ("Timer/TimerParent"); 
     }
 
     public static void CreateFloatingText(Transform location,float timeToBuild){
         FloatingTimeBuild instance = Instantiate (popupTimer);
         instance.timeToBuild = timeToBuild;
 
         instance.transform.SetParent (canvas.transform,false);
         instance.transform.position = new Vector3(location.position.x + 40,location.position.y + 50,location.position.z + 80);
     }
 }

Wich "location" is the transform in the world space where I want to instantiate my timer and time to build to run the corroutine.We have to call "Initialize" function in a game manager. Also,I had to make some few adjustments on the coordinates to fit my purpose.

Thank you all.

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

Answer by hexagonius · Jun 12, 2017 at 06:19 PM

  1. You can have multiple canvases, no need to fit all into one. They can be different too.

  2. You can attach a script into the loading bar which positions the bar every frame to where it should be, coverting it's world to it's canvas space. I think there's even a position value for a recttransform that does that, position3d or something.

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 zMalik · Jun 13, 2017 at 12:18 AM 0
Share

Hello,thank you very much for the answer @hexagonius .Altough,I spent many hours trying to solve the problem I just can't get it to work.Can you please give me some more information?

So,I tried to create another canvas and set it's Render to "World Space".Then I rotated the Canvas gameobject in 90º on X axis(Because my game is top-down view),but I can't get the loading UI to instantiate in the correct position.It instantiates,but in some random position far away from where I want.Can you give me some help please? Here's my code:

 public class FloatingTimeBuildingController : $$anonymous$$onoBehaviour {
     private static FloatingTimeBuild popupTimer;
     private static GameObject canvas;
 
     public static void Initialize(){
         canvas = GameObject.Find ("Canvas2");
         if(!popupTimer)
             popupTimer = Resources.Load<FloatingTimeBuild> ("Timer/TimerParent"); 
     }
 
     public static void CreateFloatingText(Transform location){
              //Location is the transform of the tile the building is going to be built.
         FloatingTimeBuild instance = Instantiate (popupTimer);
         Vector2 screenPosition = Camera.main.WorldToScreenPoint(new Vector3(location.position.x,location.position.y,location.position.z));
 
         instance.transform.SetParent (canvas.transform,false);
         instance.transform.position = screenPosition;
     }
 }

avatar image
0

Answer by Davinder_Singh · Jun 13, 2017 at 03:58 AM

@zMalik,,, you can try setting the position of your loading bar using Vector3 via a script. If that doesn't work, you can also try to make the loading bar the child of an empty game object whose position is fixed on screen and hence the position of loading bar will also be fixed. If it still doesn't works, please tell me.

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

71 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

Related Questions

Re-center UI Image after zooming out 0 Answers

ui button in world space not working 0 Answers

Getting A Web Cam to Play on UI Texture Image 5 Answers

Content size fitter default image background 0 Answers

Movie Texture not working properly on UI Raw Image 5.4 beta 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