Monday, January 29, 2018

Adding and controlling 'Animator Controller' at runtime using script in Unity


Time to read: 5 min

Goal: To use particular animation with any game object at runtime.

Unique thing used: RuntimeAnimatorController class (specially designed by Unity for runtime use)



Steps: 
1. Create 'Animator Controller' in the 'Project view'.
2. Use drag and drop method or 'Load resources' method to take it into code. (Remember to type cast it into 'RuntimeAnimatorController' class when using load resources method)
3. Create 'animation' of your need.
4. Drag the animation into 'Animator Window' and make arrangements as shown below:


5. You have to create one 'default state' which does nothing, and set it as 'default state' because when you load the animator at runtime it will by default plays the current default state from the animator window. And if our animation is the default state then it will play that animation which we dont want, coz we want control on it.

6. We are going to control the animation using trigger. So create the new parameter in 'Animator Window' as 'fade' of type trigger.

7. Triggers are basically used to tell the animator which state transition should be played.

8. To assign the trigger variable as a 'condition' click on the 'transition arrow' which is starting from 'Base' to 'fadeOut' state.

9. You will see the properties as shown in below into the 'inspector window':


10. Click on the plus sign in below 'conditions' section. And select the 'fade' variable. Now when we set the trigger to 'fade' from script using 'SetTrigger()', then this transition will happen and 'fadeOut' state will be played. When the animation is played then the control will go back to 'Base' state automatically coz we have made the transition arrow from 'fadeOut' to 'Base'. (Remember to uncheck the 'Loop Time' variable from the 'fadeOut' animations property so that it will not play in loop)

11. Below is the script:



If you have any doubt please drop a comment.

Thank you,
Happy coding :)


Friday, March 24, 2017

Mac unity android sdk path setting
=========================
If you have Android Studio installed.
The path for SDK is “/Users/username/Library/Android/sdk”.

But sometimes the “Library” folder is hidden.
Or follow the steps below
  • Open Terminal found in Finder > Applications > Utilities
  • In Terminal, paste the following: defaults write com.apple.finder AppleShowAllFiles YES
  • Press return
  • Hold the ‘Option/alt’ key, then right click on the Finder icon in the dock and click Relaunch.

Once you done this. Open unity->Unity->Preferences->External tools

In that find SDK path option, click browse, then drag the “sdk” folder which you just opened above in Finder…

Done…

If its still giving error then consider below case:

Sometimes Unity won’t recognise Android Studio’s sdk tools and gives error and ask you to again set the path.

To solve this, download separate sdk tools from Google.

Once you download the “tools” folder.
Then copy it and replace it in “/Users/username/Library/Android/sdk”.

And try again…

I know this is not written well…but if you are struggling a lot ..if will just give you an correct hint. I struggled a lot for this issue…so wanted to help you all.

Thank you…