Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 paulaceccon · Jan 21, 2013 at 11:10 AM · animationguiguitextureresize

Scale Animation

Hi, I'm wondering how I could do a scale animation. I would like to do something similar to what we can do with Lerp. With this method, we can do an animation in which, e.g., the object starts at a position1 and ends at a position2, traversing this path at a constant speed and in a certain time.

I would like to do something like this, but with the size. I have a GUITexture that starts with an specific size1 and I would like that it ends with a size2. So, I would like to be able to simulate the GUITexture getting close to the screen, or getting away from the screen (without change the camera position).

So far, I have this, but I don't have much idea about how I could do that...

 Texture animationTexture;
 GUITexture animationGUI;
 GameObject animationGO;
 
 void Start ()
 {
     animationGO = new GameObject();
     animationGO.transform.position = Vector3.zero;
     animationGO.transform.localScale = Vector3.zero;
     animationGUI = animationGO.AddComponent<GUITexture>();
     animationGUI.name = "Animation";
 }
 
 //Just to set the animation texture and its properties.
 void setAnimationTexture (string animationResourcePath, Vector2 position, Vector2 size)
 {
     animationTexture = Resources.Load(animationResourcePath) as Texture;
     animationGUI.enabled = true;
     animationGUI.texture = animationTexture;
     animationGUI.pixelInset = new Rect(position.x, position.y, size.x, size.y);
 }
 
 IEnumerator scaleObject(Vector2 finalSize)
 {
     float progress = 0.0f;
     Vector2 position = new Vector2 (animationGUI.pixelInset.x, animationGUI.pixelInset.y);
     Vector2 size = new Vector2 (animationGUI.pixelInset.width, animationGUI.pixelInset.height);
 
     while (progress < 1.0f)
     {
             progress += Time.deltaTime/10;
   
             animationGUI.pixelInset = new Rect(position.x, position.y, size.x, size.y);
             
             yield return new WaitForEndOfFrame();
     }
  }

Thanks in advance.

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 paulaceccon · Jan 25, 2013 at 07:29 PM

Well, I could do this with this script:

 public delegate Vector2 PositionFunc(Vector2 position, Vector2 sizeIncrement, int step);
     
     public Vector2 scaleBottomRight(Vector2 position, Vector2 sizeIncrement, int step)
     {
         return new Vector2(position.x-(sizeIncrement.x * (step + 1)), 0f);
     }
     
     public Vector2 scaleLeftCenter(Vector2 position, Vector2 sizeIncrement, int step)
     {
         return new Vector2(0, position.y - (sizeIncrement.y * (step + 1))/2);
     }
     
     public Vector2 scaleMiddleCenter(Vector2 position, Vector2 sizeIncrement, int step)
     {
         return new Vector2(position.x - (sizeIncrement.x * (step + 1))/2, position.y - (sizeIncrement.y * (step + 1))/2);
     }
     
     public IEnumerator animateObject(float scale, float timeout, Vector2 position, PositionFunc positionFunc)
     {
         GameObject animationGO = GameObject.Find("Animation");
         if (animationGO)
         {
             GUITexture animationGUI = animationGO.GetComponent<GUITexture>();
             if (animationGUI)
             {
                 float wait = 0.1f;
                 int steps = (int) ((timeout-1f)/wait);
                 Vector2 oldPosition = new Vector2 (animationGUI.pixelInset.x, animationGUI.pixelInset.y);
                 Vector2 oldSize = new Vector2 (animationGUI.pixelInset.width, animationGUI.pixelInset.height);
                 Vector2 finalSize = oldSize * scale;
                 Vector2 sizeIncrement = (finalSize-oldSize) / steps;
                 Vector2 currentSize = oldSize;
                 Vector2 currentPosition = oldPosition;
                 
                 for (int i = 0; i < steps; i++)
                 {             
                     currentSize += sizeIncrement;
                     currentPosition = positionFunc(position, sizeIncrement, i);
                     
                     animationGUI.pixelInset = new Rect(currentPosition.x, currentPosition.y, currentSize.x, currentSize.y);
                     
                     yield return new WaitForSeconds(wait);
                 }
                 
                 animationGUI.enabled = false;
             }
         }
     }
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to size the guitexture which with animation on it in different resolutions? 1 Answer

Animate GUI Elements 1 Answer

Draw GUI texture from it's center point. 2 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

GUI button resizing makes button disappear? 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