Unity automatically creates Animatorcontroller

Source: Internet
Author: User

This is the most correct use of Mecanim I know now. First on the code:

Using unityengine;using unityeditor;using unityeditorinternal;using system;using system.collections;using System.collections.generic;public class getanimationclips:assetpostprocessor{void Onpostprocessmodel (GameObject go        ) {list<animationclip> clips = new list<animationclip> ();        Unityengine.object[] objects = Assetdatabase.loadallassetsatpath (Assetpath); Debug.Log (objects.        Length);            foreach (var obj in objects) {animationclip clip = obj as Animationclip; if (clip! = null && clip.name.IndexOf ("__preview__") = =-1) {clips.            ADD (clip);     }} createanimationcontroller (Assetpath, clips); } static void Createanimationcontroller (string path, list<animationclip> clips) {path = path. Replace ("\ \", "/"). Replace (".        Fbx ",". Fbx "); String Acpath = path.        Replace (". Fbx", ". Controller"); String Prefabpath = path.        Replace (". Fbx", ". Prefab"); Creating animationsController Animatorcontroller Animatorcontroller = Animatorcontroller.createanimatorcontrolleratpath (AcPath);        Animatorcontrollerlayer layer = animatorcontroller.getlayer (0);        Unityeditorinternal.statemachine sm = layer.statemachine;        Vector3 anystateposition = sm.anystateposition;        float offset_x = 220;        float offset_y = 60;        float item_per_line = 4;        float Originx = anystateposition.x-offset_x * (item_per_line/2.5f);        Float Originy = anystateposition.y + offset_y;        float x = Originx;        Float y = originy; foreach (Animationclip newclip in clips) {state state = Sm.            Addstate (NewClip.name.ToLower ()); State.            Setanimationclip (newclip, layer);            State.position = new Vector3 (x, y, 0);            x + = offset_x;                if (x >= Originx + offset_x * item_per_line) {x = Originx;            Y + = offset_y; }}//create prefab string name = path.Substring (path. LastIndexOf ("/") + 1).        Replace (". Fbx", "");        Prefabpath = "assets/resources/" + name + ". Prefab";        Gameobject go = Assetdatabase.loadassetatpath (path, typeof (Gameobject)) as Gameobject;        Go.name = name; Animator Animator = go.        Getcomponent<animator> (); if (animator = = null) {animator = go.        Addcomponent<animator> ();        } animator.applyrootmotion = false;        Animator.runtimeanimatorcontroller = Animatorcontroller;        Assetdatabase.createasset (go, Prefabpath);        Prefabutility.createprefab (Prefabpath, go);        Gameobject.destroyimmediate (go, true);    Assetdatabase.saveassets (); }}

What this code does is that when the model is loaded, it automatically loads its animations, and then generates the Animatorcontroller file (the file that controls the animated flow), generating the corresponding prefab (for direct instantiation of the code). There are several points of knowledge:

1, Assetdatabase.loadallassetsatpath through this function can load FBX file (including all animation information). This is the way to get animationclip based on the fbx file.

2, Assetpostprocessor This is the interface that the unity resource loads. Automatically generated animation controller code can not be placed here, you can expand the menu menus to complete the corresponding functions (such as traversing the selection folder of FBX files and related processing).

This is the most correct use of Mecanim in the MMO I know now:

1, do not manually edit the. controller file, whether it is loading animation or wiring, in the MMO animation volume is quite large, manual editing these are absolutely unacceptable.

2, Mecanim has certain advantages, such as redirection, now almost all MMO is used by the old animation system. But I think that my method can be compatible with the old animation system and the new animation system, or that can make the old animation system's project smooth transition to the new animation system

3, we maintain the way the animation may be, the art gives the model, if possible, it is best to split the good animation (seemingly Maya is supported, Blender is also supported, but 3dmax unknown, such as I use blender to export the torch light model into unity, Unity can directly recognize the segmented animation without having to manually cut the animation. If not, a configuration file (such as Excel) is required to maintain information about the start and end frames of the animation. When importing the model, you can add Animationclip to the code based on this configuration file.

4, the idea of automatically generating animatorcontroller can be applied to the old animation system, because its core content is how to get the Animationclip information through FBX. If the old animation system is to add Animationclip to Prefab's animation animated list.

5. How I use the New animation system:

Gameobject prefab = Resources.load ("Player") as Gameobject;        Gameobject go = Instantiate (prefab) as Gameobject;        Animator Animator = go. Getcomponent<animator> ();        Animator. Crossfade ("Idle", 0.25f, 0);

Controller inside is not wired, nor Param control, it is just a container. The code uses Crossfade directly to animate the switch. This has the added benefit that Assetbundle packaged prefab cannot see its data in Unity's animator page. Your own control of the animation switch can be done more freely.

Unity automatically creates Animatorcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.