Adding Animation to Inputfield and save the transform












0















I wanted to add a simple search animation to my input field in my unity UI.
search-InputField



This is my InputField and the idea is, when I select it, it should expand slowly, and when I deselect it, it should shrink back to its normal form.



RectTransfrom Component



This is the rect Transform Component of this inputfield. I added to the input field and Event Trigger Component and an Animator. I created two Animations called SearchAnimation and DeselectAnimation and added them to my AnimationController called 'SearchController'.
This is how I designed my SearchController:
AnimationController
I set the Transitions between the defaultState and SearchAnimation to be listening to the SelectBool and DeselectBool (the name already describes its purpose).



Then I added the following script to my input Field so that those two booleans will be set accordingly to the event trigger:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnClickScript : MonoBehaviour {

Animator anim;

void Start()
{
anim = GetComponent<Animator>();
}


public void OnSelect()
{
anim.SetBool("SelectBool", true);
anim.SetBool("DeselectBool", false);
GetComponent<RectTransform>().sizeDelta = new Vector2(450, 50);
GetComponent<RectTransform>().localPosition.Set(-275, 0, 0);

}

public void OnDeselect()
{
anim.SetBool("DeselectBool", true);
anim.SetBool("SelectBool", false);
GetComponent<RectTransform>().sizeDelta = new Vector2(200, 50);
GetComponent<RectTransform>().localPosition.Set(-130, 0, 0);
}

}


But after the animation is played the inputfield is set back to its inital size and location. How do I fix this?










share|improve this question



























    0















    I wanted to add a simple search animation to my input field in my unity UI.
    search-InputField



    This is my InputField and the idea is, when I select it, it should expand slowly, and when I deselect it, it should shrink back to its normal form.



    RectTransfrom Component



    This is the rect Transform Component of this inputfield. I added to the input field and Event Trigger Component and an Animator. I created two Animations called SearchAnimation and DeselectAnimation and added them to my AnimationController called 'SearchController'.
    This is how I designed my SearchController:
    AnimationController
    I set the Transitions between the defaultState and SearchAnimation to be listening to the SelectBool and DeselectBool (the name already describes its purpose).



    Then I added the following script to my input Field so that those two booleans will be set accordingly to the event trigger:



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class OnClickScript : MonoBehaviour {

    Animator anim;

    void Start()
    {
    anim = GetComponent<Animator>();
    }


    public void OnSelect()
    {
    anim.SetBool("SelectBool", true);
    anim.SetBool("DeselectBool", false);
    GetComponent<RectTransform>().sizeDelta = new Vector2(450, 50);
    GetComponent<RectTransform>().localPosition.Set(-275, 0, 0);

    }

    public void OnDeselect()
    {
    anim.SetBool("DeselectBool", true);
    anim.SetBool("SelectBool", false);
    GetComponent<RectTransform>().sizeDelta = new Vector2(200, 50);
    GetComponent<RectTransform>().localPosition.Set(-130, 0, 0);
    }

    }


    But after the animation is played the inputfield is set back to its inital size and location. How do I fix this?










    share|improve this question

























      0












      0








      0








      I wanted to add a simple search animation to my input field in my unity UI.
      search-InputField



      This is my InputField and the idea is, when I select it, it should expand slowly, and when I deselect it, it should shrink back to its normal form.



      RectTransfrom Component



      This is the rect Transform Component of this inputfield. I added to the input field and Event Trigger Component and an Animator. I created two Animations called SearchAnimation and DeselectAnimation and added them to my AnimationController called 'SearchController'.
      This is how I designed my SearchController:
      AnimationController
      I set the Transitions between the defaultState and SearchAnimation to be listening to the SelectBool and DeselectBool (the name already describes its purpose).



      Then I added the following script to my input Field so that those two booleans will be set accordingly to the event trigger:



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEngine.UI;

      public class OnClickScript : MonoBehaviour {

      Animator anim;

      void Start()
      {
      anim = GetComponent<Animator>();
      }


      public void OnSelect()
      {
      anim.SetBool("SelectBool", true);
      anim.SetBool("DeselectBool", false);
      GetComponent<RectTransform>().sizeDelta = new Vector2(450, 50);
      GetComponent<RectTransform>().localPosition.Set(-275, 0, 0);

      }

      public void OnDeselect()
      {
      anim.SetBool("DeselectBool", true);
      anim.SetBool("SelectBool", false);
      GetComponent<RectTransform>().sizeDelta = new Vector2(200, 50);
      GetComponent<RectTransform>().localPosition.Set(-130, 0, 0);
      }

      }


      But after the animation is played the inputfield is set back to its inital size and location. How do I fix this?










      share|improve this question














      I wanted to add a simple search animation to my input field in my unity UI.
      search-InputField



      This is my InputField and the idea is, when I select it, it should expand slowly, and when I deselect it, it should shrink back to its normal form.



      RectTransfrom Component



      This is the rect Transform Component of this inputfield. I added to the input field and Event Trigger Component and an Animator. I created two Animations called SearchAnimation and DeselectAnimation and added them to my AnimationController called 'SearchController'.
      This is how I designed my SearchController:
      AnimationController
      I set the Transitions between the defaultState and SearchAnimation to be listening to the SelectBool and DeselectBool (the name already describes its purpose).



      Then I added the following script to my input Field so that those two booleans will be set accordingly to the event trigger:



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEngine.UI;

      public class OnClickScript : MonoBehaviour {

      Animator anim;

      void Start()
      {
      anim = GetComponent<Animator>();
      }


      public void OnSelect()
      {
      anim.SetBool("SelectBool", true);
      anim.SetBool("DeselectBool", false);
      GetComponent<RectTransform>().sizeDelta = new Vector2(450, 50);
      GetComponent<RectTransform>().localPosition.Set(-275, 0, 0);

      }

      public void OnDeselect()
      {
      anim.SetBool("DeselectBool", true);
      anim.SetBool("SelectBool", false);
      GetComponent<RectTransform>().sizeDelta = new Vector2(200, 50);
      GetComponent<RectTransform>().localPosition.Set(-130, 0, 0);
      }

      }


      But after the animation is played the inputfield is set back to its inital size and location. How do I fix this?







      unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 12:31









      ScorixScorix

      18311




      18311
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Easy way:



          Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.



          I put my test project here.



          Selected State Animation Example



          With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.



          Hard way



          Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.



          Suggestions:



          Using Pivot:



          In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.



          enter image description here



          Using trigger:



          Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.



          enter image description here






          share|improve this answer


























          • didn't think of giving the selected state another animtation, that was simple

            – Scorix
            Nov 26 '18 at 18:31











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467464%2fadding-animation-to-inputfield-and-save-the-transform%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Easy way:



          Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.



          I put my test project here.



          Selected State Animation Example



          With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.



          Hard way



          Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.



          Suggestions:



          Using Pivot:



          In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.



          enter image description here



          Using trigger:



          Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.



          enter image description here






          share|improve this answer


























          • didn't think of giving the selected state another animtation, that was simple

            – Scorix
            Nov 26 '18 at 18:31
















          1














          Easy way:



          Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.



          I put my test project here.



          Selected State Animation Example



          With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.



          Hard way



          Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.



          Suggestions:



          Using Pivot:



          In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.



          enter image description here



          Using trigger:



          Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.



          enter image description here






          share|improve this answer


























          • didn't think of giving the selected state another animtation, that was simple

            – Scorix
            Nov 26 '18 at 18:31














          1












          1








          1







          Easy way:



          Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.



          I put my test project here.



          Selected State Animation Example



          With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.



          Hard way



          Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.



          Suggestions:



          Using Pivot:



          In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.



          enter image description here



          Using trigger:



          Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.



          enter image description here






          share|improve this answer















          Easy way:



          Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.



          I put my test project here.



          Selected State Animation Example



          With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.



          Hard way



          Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.



          Suggestions:



          Using Pivot:



          In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.



          enter image description here



          Using trigger:



          Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 0:49

























          answered Nov 25 '18 at 23:42









          ming060ming060

          15225




          15225













          • didn't think of giving the selected state another animtation, that was simple

            – Scorix
            Nov 26 '18 at 18:31



















          • didn't think of giving the selected state another animtation, that was simple

            – Scorix
            Nov 26 '18 at 18:31

















          didn't think of giving the selected state another animtation, that was simple

          – Scorix
          Nov 26 '18 at 18:31





          didn't think of giving the selected state another animtation, that was simple

          – Scorix
          Nov 26 '18 at 18:31




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467464%2fadding-animation-to-inputfield-and-save-the-transform%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin