How to add a Animator Controller to a character at runtime? - animation

I have an Animator controller at "Assets/Resources/System/PLController". I have to add it at runtime using a script. How to accomplish this. Using Unity 5 (5.3.0f4)
PLController = (Animator Controller)

First attach an Animator component to that GameObject like
Declare an Animator variable.
Assign variable through GetComponent
Assign RuntimeAnimatorController to its runtimeAnimatorController attribute.
Like,
Animator PLAnimator;
void Start ()
{
PLAnimator = GetComponent<Animator> ();
PLAnimator.runtimeAnimatorController = Resources.Load ("Assets/Resources/System/PLController") as RuntimeAnimatorController;
}

Related

Different animation in 2 directions

I’m really just a beginner & I have no idea how to use the animation in Unity properly. I’m working on a 2D platformer game. I managed to make the animation work, but the point is that my character should looks different from each side (so I can’t just flip the sprite). What’s the simplest way to for example, when pressing right he will use animation 1 and when pressing left he will use animation 2? Thanks in advance & sorry about my stupidity;)
Did you try using Animator Controller.
[RequireComponent(typeof(Animator))]
public class PlayerAnimation: MonoBehaviour
{
[SerializeField]private Animator _animator;
private const string DirectionParam = "Direction";
private void Update()
{
//On Right button clicked
if (Input.GetKeyDown(KeyCode.RightArrow))
{
//Play Right animation
_animator.SetInteger(DirectionParam, 1);
}else if (Input.GetKeyDown(KeyCode.RightArrow))
{
_animator.SetInteger(DirectionParam, -1);
}
else
{
_animator.SetInteger(DirectionParam, 0);
}
}
}
Drag this script on you player. This will also Add unity Animator Component on your game object.
Create Animator Object and place it on the animator component.
open Animator Editor from Window>Animation>Animator
Then create an int parameter in Animation Controller Window and name it "Direction".
Create two animation clips one for left move animation and another for right move animation
Drag these clips to the animator Window. Right click and create two transition and add condition with Direction parameter. You should create 3 animation clips and set that to idle and transition to idle when the direction value is 0.
Enjoy!

Unity 5.3 dropdown menu

I am trying to set the text of my dropdown menu options by scripting but I'm geting this error
NullReferenceException: Object reference not set to an instance of an object
My code:
function Start () {
var dropdown = GameObject.Find("Dropdown");
dropdown.options.Clear();
dropdown.options.Add(new dropdown.OptionData("tile1"));
}
This is my first project in Unity.
It's possible that Dropdown does not exist yet in the scene when Start() is called. Perhaps consider adding a public field instead of using GameObject.Find():
public GameObject dropdown;
void Start()
{
//... your code here
}
Then in the scene, drag your "Dropdown" object onto the object this script is on.

How to change content of unity text?

I create an application on unity3d. I use Unity 5.0.2f1 (64-bit). I create a new project and add an empty gameobject. I click gameobject and add a gui text from inspector. My gui text is below :
I want to change the contenf of this text at each frame. I use below code but it doesn't change it.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
private int count = 0;
public GUIText guiText;
// Use this for initialization
void Start () {
guiText.text = "Counter";
}
}
What should I do? I see asddffbhfbehfbhj every time.
Attach this script to your gameobject(the same game object that guitext is attached to)then assign the guitext component of your game object to your script via inspector(drag you guitext component and drop it in guitext variable that you see within your script component in inspector)

Monotouch: How to change default animation of view change with NavigationController.PushViewController

I'm using NavigationController for navigation in my app. When the PushViewController starts(someUIViewController,true), it change view with default animation(move from right to left for child controller). How can i change this animation ? Now i'm using this:
this.NavigationController.PushViewController(someUIViewController,true);
UIView.BeginAnimations(null);
UIView.SetAnimationDuration(0.4);
UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, NavigationController.View,true);
UIView.CommitAnimations();
But I'm limited to four UIViewAnimationTransition types.I found what I needed(the appearance of the view from the bottom to up):
this.NavigationController.PushViewController(someUIViewController,true);
var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.y");
theAnimation.Duration = 0.3f;
theAnimation.From = NSNumber.FromFloat(this.View.Frame.Height);
theAnimation.To = NSNumber.FromFloat(0f);
NavigationController.View.Layer.AddAnimation(theAnimation, "animate");
NavigationController.View.Layer.AnimationForKey("animate");
But when CABasicAnimation starts, default animation (move from left to right to parent controller) starts too.The result is a wrong combination.How can I run only one(translation on y) or make custom animation?
I believe you need to change the call to PushViewController to not animate to avoid the default animation kicking in.
this.NavigationController.PushViewController(someUIViewController,true);
Should be
this.NavigationController.PushViewController(someUIViewController,false);
Was same problem. There is no normal solution for this. PushViewController is limited by the standard animation(type of UIViewAnimationTransition). If you want to change them, try this:
NavigationController.PushViewController(screen, false);
var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.x");
theAnimation.Duration = 0.6f;
theAnimation.From = NSNumber.FromFloat(-NavigationController.View.Frame.Width);
theAnimation.To = NSNumber.FromFloat(0f);
NavigationController.View.Layer.AddAnimation(theAnimation, "animate");
NavigationController.View.Layer.AnimationForKey("animate");
But it is not perfect, try and u see why. Also u can use PresentViewController. It have some other standart animations in UIModalTransitionStyle:
this.NavigationController.PresentViewController(new UINavigationController(screen){
ModalTransitionStyle= UIModalTransitionStyle.CoverVertical,
}, true,null);
//Allows a UINavigationController to push using a custom animation transition
public static void PushControllerWithTransition(this UINavigationController
target, UIViewController controllerToPush,
UIViewAnimationOptions transition)
{
UIView.Transition(target.View, 0.75d, transition, delegate() {
target.PushViewController(controllerToPush, false);
}, null);
}
//Allows a UINavigationController to pop a using a custom animation
public static void PopControllerWithTransition(this UINavigationController
target, UIViewAnimationOptions transition)
{
UIView.Transition(target.View, 0.75d, transition, delegate() {
target.PopViewControllerAnimated(false);
}, null);
}
// With these extensions in scope, moving between controllers with a flip animation is now as trivial as this:
//Pushing someController to the top of the stack
NavigationController.PushControllerWithTransition(someController,
UIViewAnimationOptions.TransitionFlipFromLeft);
//Popping the current controller off the top of the stack
NavigationController.PopControllerWithTransition(
UIViewAnimationOptions.TransitionFlipFromRight);

Group of buttons?

I have a question - is there any way to define a few buttons as a 1 group, and make 1 animation that will start for this group, instead of making an separate animation start for each of them?
Put them all in a ViewGroup and animate that. All children will be part of the animation. A ViewGroup is the base class for any layout class, that means you can use a LinearLayout, RelativeLayout or anything else that you find suitable.
You can try to set to all buttons same onTouchListener, like:
button1.setOnTouchListener(this);
button2.setOnTouchListener(this);
button3.setOnTouchListener(this);
Of course your app needs to implement onTouchListener, and then you can write your onTouchListener:
public void onClick(View v) {
// write your code what you want your buttons to do
// You can also write some different codes for buttons by using id
if(v.getId() == button1.getId())
{
//do something when button1 clicked...
}
}

Resources