Position of bars using Gradient GUI Bars asset from asset store
Hi, i am currently using this Gradient GUI Bars on my Unity project: (https://www.assetstore.unity3d.com/en/#!/content/19972)
They're really helpful, but there's a problem with them: Since the position of the bar is done using an attribute called "public Vector2 Position;" (from the GUIBarScript script that is attached to all my bars i use in the game), when my game is on fullscreen mode, the bars are at a different position from when my game is not fullscreen. Then, I have tried to make the class not use the Position attribute at all(i have changed the whole GUIBarScript class to use transform.localposition instead of this "Position" attribute). Instead, use the transform.localposition. Still, it seems like the Vector2 Position works differently from a normal localposition. My question is: How can I make the GUIBarScript do not use Position and use the object's current position instead? And how different is this Vector2 Position compared to the natural transform.position from the gameObject on Unity.
Sorry for posting this question here on the forum, but the creator of the asset doesn't read his email, i guess so I can't seem to have an option to contact him
It seems like the creation of a Rect uses position coordinates that are different from GameObject's transform coordinates and that is the problem. So, how do i create a rect using a gameobjects position? $$anonymous$$aybe that will solve my problem
Answer by TBruce · Jul 23, 2016 at 10:40 PM
The problem is that this asset was made with an older version of Unity (4.5.4) which does not have support anchoring objects to the screen. The asset needs to be updated to use the new Unity UI.
I have attached a Unity package. There is one scene that has a UI prefab. I took the original GUIBarScript.cs and modified it to use the UI instead of the GUI. The new script is called UIBarScript.cs.
The UIBarScript script to is attached a canvas called GradientBar. To change the anchor select the Background UI GameObject.
It uses the artwork and font from the GradientGUIBars\Bars\DriveStyle folder. If you wish to use different artwork then you will need to import it (images will need to be sprites not textures).
Good luck and enjoy.
Thank you so much, @$$anonymous$$avina ! This looks like the correct solution! I will work right away to modify my project and check the results. Just something i wanted to ask: If i wanted to put the rect on a position that's not x=0 and y=0, what should i do? I have tried to modify the UpdateBar() function: when you create int x = 0;and int y = 0;, ins$$anonymous$$d of 0 i put the value of public attributes i defined, but it seems to me the bar isn't moving to any position. Is there any way to use those coordinates i have when i use a normal gameObject(you know, the ones that are in the gameObject's transform component)?
The proper way to use this is to first set your anchor of the Background GameObject (see Anchor presets and Anchor and position fields in the Inspector on this page).
Then set the position of the Background GameObject to the desired location using either the RectTransform PosX and PosY or in the scene view using the position tool. Watching the Game View you can see where the UI bar will stay no matter what resolution.
Note: Per above, there is no need to modify the code. Just set the anchor and position visually the way you want. Also the GameObject GradientBar in the scene is a Canvas and must remain at the root in the scene.
Here is a short video showing how to set the position with anchors.
Note: When setting your anchor, it is easier if you hold the ALT key at the same time.
Thank you for the help @$$anonymous$$avina and Sorry for the work, but how do i scale the bar in this solution? I have noticed that the GradientBar is a component i can't scale and the background, mask, value bar... they are several different components that if i scale them individually, the bar stops working(like, the value bar keeps on 10% even though the value isn't)
@$$anonymous$$avina, ins$$anonymous$$d of position, i used alpha. Now, the only problem I'm facing is that i want to draw the bar right beside(on the right of) a certain GameObject. On fullscreen and on no fullscreen, the bar needs to be right beside the gameObject. Is there any way to do that on Unity? I have tried to use this user's answer to my other topic: http://answers.unity3d.com/questions/1219518/drawing-a-rectangle-on-gameobjects-position.html but still didn't work.
I do not know what you mean when you say ins$$anonymous$$d of position, i used alpha.
alpha relates to the color of an object not the position.
What is this GameObject that you need to place the bar next to? If it is a sprite that is easy.
Here is a little demo showing you how.
@$$anonymous$$avina Thank you so much for all your answers, $$anonymous$$avina. You really saved me and now everything is working with my Bar! Thanks to your last answer, the bar is now on the same height as a gameobject and on the correct position on all screen sizes. Thank you so much! ^^
Oh, by the way, "ins$$anonymous$$d of position i used alpha" means that i wanted all the components from a screen to disappear in order to another group of components from another screen appear. Normally, i would move all the objects to a position the user can't see, but now i'm using alpha with the GuiBar, because it's better than moving it to another position.
Thank you ^^
Your answer
Follow this Question
Related Questions
How to include Standard Assets in a custom asset? (best practices?) 0 Answers
i cant down load any new assets I have purchased from untiy asset store 0 Answers
Aset store download issue 0 Answers
If I will buy asset is this one time purchase or monthly subscribe? 1 Answer
What should I do? 2 Answers