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 /
avatar image
0
Question by Thephil2988 · Feb 28, 2018 at 01:04 PM · animationbuttonboxhotkeyspressed

Playing a button pressed animation on a box

I have a boxin my world (made in onGUI) and was wondering if it was possible to do a have a simular animation as the button press be played on the box?

I have a spell that is cast when the key 1 is pressed. I have a box displaying the name and hotkey and also if the player is in range or not. I want to have an animation that shows when the spell button is being pressed. Is this possible or is it just a stupid idea?

Comment
Add comment · Show 8
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 Harinezumi · Feb 28, 2018 at 01:23 PM 2
Share

I would recommend not using OnGUI() anymore, it is deprecated. The newer Unity UI system replaced it and has a lot better functionality. In Unity UI you model a button with a GameObject with a Button script on it, which automatically handles being pressed.

avatar image Thephil2988 Harinezumi · Feb 28, 2018 at 04:46 PM 0
Share

I have heard people saying this, but i feel like the new system is extremely bad at different sizes of screens. If i place a button in the bottom of the screen on my computer it is not even in the screen on my laptop.

avatar image Harinezumi Thephil2988 · Feb 28, 2018 at 04:53 PM 0
Share

Well, designing the UI for various resolutions is difficult, but once you understand anchoring and the CanvasScaler component, it becomes a lot easier. And with OnGUI() you still have the same problem, but there you have to change code to move the UI, and calculate with resolution and ratios...

Check out this Unity manual about how to design for various resolutions, it is not perfect, but it explains the basics well.

Show more comments
avatar image Thephil2988 Harinezumi · Feb 28, 2018 at 04:51 PM 0
Share

And also i find it hard to eg. have a text be displayed over an enemy or the player.

avatar image Harinezumi Thephil2988 · Feb 28, 2018 at 05:01 PM 0
Share

That's actually really easy, just add this script to the UI element you want to move:

 public class AnchorIn3D : $$anonymous$$onoBehaviour {
 
     [SerializeField] private RectTransform ownRectTransform = null;
     [SerializeField] private Transform target = null;
     public void SetTarget (Transform value) { target = value; }
 
     [SerializeField] private Vector3 offset = Vector3.zero;
     public void SetOffset (Vector3 value) { offset = value; }
 
     private void Awake () {
         if (ownRectTransform == null) { ownRectTransform = GetComponent<RectTransform>(); }
     }
 
     private void Update () {
         if (ownRectTransform != null && target != null) {
             ownRectTransform.position = Camera.main.WorldSpaceToScreen(target.position + offset);
         }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rerenzel · Feb 28, 2018 at 05:02 PM

yes it is if(Input.GetButtonDown(yourButton) { // this is where your animation starts }

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

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

UI Button Pressed color motion don't reset to Normal color 1 Answer

Animation bug when click and hold on Button edge 1 Answer

UI button pressed animation not playing directly after entering 2 Answers

How to make a button that opens and closes a tab? 1 Answer

(Unity 4.6) Button animations 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