About the automatic generation of animation, rain pine Momo has a relatively complete article, links in this click to open the link
Generating Animationclip, controller, and setting state are all references to the code he provides, and here I would like to add an anchor point to the 2d texture.
The main look at the code here, it is worth mentioning that the set custom anchor point must be set to the Spritealignment 9 (customer), otherwise the set of pivot will not work.
[MenuItem ("Jygame/buildclip")] static void Buildclip () {//animationxml XML = Resourcemanager.get
Resourcemanager.init ();
list<string> actionlist = new list<string> ();
foreach (Var d in Targetdir.getdirectories ()) {string animationname = D.name;
Debug.Log ("Building" + animationname); if (resourcemanager.get<jygame.animationnode> (animationname) = = null) {debug.logwarning
("error, cannot find the anchor description in XML!" + Animationname);
Continue ///Adjust anchor foreach (Var f in new DirectoryInfo (D.fullname + "\\rawdata"). GetFiles ("*.png")) {string[] paras = f.name.replace (". png", "").
Split ('-');
string animation = Paras[0];
String action = Paras[1]; int count = Int.
Parse (paras[2]);
Animationimage img = null;
Try{img = resourcemanager.get<jygame.animationnode> (animation).
Getgroup (Action). Images[count];
catch {Debug.Log ("The description of the file is not found, delete" + f.fullname);
File.delete (F.fullname);
Continue
String path = Datapathtoassetpath (F.fullname);
Textureimporter textureimporter = Assetimporter.getatpath (path) as Textureimporter;
Textureimportersettings tis = new Textureimportersettings ();
Textureimporter.readtexturesettings (tis); Tis.spritealignment = 9;
Customer Tis.spritepixelsperunit = 1;
tis.mipmapenabled = false;
Tis.spritepivot = new Vector2 (float) img.anchorx/img.w, (float) (img.h-img.anchory)/img.h);
Textureimporter.settexturesettings (tis);
if (!actionlist.contains (action)) {Actionlist.add (action);
} assetdatabase.importasset (path);
} list<animationclip> clips = new list<animationclip> (); Create clip foreach (var action in actionlist) {fileinfo[] images = new DirectoryInfo (D.fullname + "\\rawdata"). GetFiles (String.
Format ("*-{0}-*.png", Action));
Animationclip clip = new Animationclip ();
Animationutility.setanimationtype (clip, modelimporteranimationtype.generic);
editorcurvebinding curvebinding = new editorcurvebinding ();
Curvebinding.type = typeof (Spriterenderer);
Curvebinding.path = "";
Curvebinding.propertyname = "M_sprite"; objectreferencekeyframe[] keyframes = new objectreferencekeyframe[images.
Length];
float frametime = 1/10f; if (action = = "Stand") {
Frametime = 1/4f; for (int i = 0; i < images. Length; i++) {Sprite Sprite = resources.loadassetatpath<sprite> (Datapathtoassetpath (IMA Ges[i].
FullName));
Keyframes[i] = new Objectreferencekeyframe ();
Keyframes[i].time = Frametime * i;
Keyframes[i].value = Sprite;
} clip.framerate = 10;
if (action = = "Stand") Clip.framerate = 4; If (Action!= "attack") {//Set idle file for looping animation Serializedobject Seriali
Zedclip = new Serializedobject (clip); Animationclipsettings clipsettings = new Animationclipsettings (Serializedclip.findproperty ("m_
Animationclipsettings "));
Clipsettings.looptime = true;
Serializedclip.applymodifiedproperties ();} animationutility.setobjectreferencecurve (clip, curvebinding, keyframes);
Assetdatabase.createasset (clip, Target_dir + animationname + "\" + Action + ". Anim"); Clips.
ADD (clip);
Assetdatabase.saveassets (); }//Create controller Animatorcontroller Animatorcontroller = animatorcontroller.creat
Eanimatorcontrolleratpath (Target_dir + animationname + "\\controller.controller");
Animatorcontrollerlayer layer = animatorcontroller.getlayer (0);
Unityeditorinternal.statemachine sm = layer.statemachine;
Animatorcontroller.addparameter ("Attack", Animatorcontrollerparametertype.trigger);
Animatorcontroller.addparameter ("Move", Animatorcontrollerparametertype.trigger);
Animatorcontroller.addparameter ("Stand", Animatorcontrollerparametertype.trigger);
state standstate = null; state attackstate = Null
state movestate = null; foreach (Animationclip newclip in clips) {state state = Sm.
Addstate (Newclip.name); State.
Setanimationclip (newclip, layer);
if (Newclip.name = = "Stand") {sm.defaultstate = state;
Standstate = State;
}else if (Newclip.name = = "Move") {movestate = state;
}else if (Newclip.name = = "Attack") {attackstate = state; } Transition trans = Sm.
Addanystatetransition (state); Trans.
Removecondition (0); } if (attackstate!= null && standstate!=null) {var trans = Sm.
Addtransition (Attackstate, standstate); var condition = trans.
Addcondition (); Condition.mode = TransitionconditioNmode.exittime;
} assetdatabase.saveassets (); Build prefab Add a preview of the sprite FileInfo tagimage = new DirectoryInfo (d.fullname + "\\rawdata").
GetFiles ("*.png") [0];
Gameobject go = new Gameobject ();
Go.name = Animationname; Spriterenderer Spriterender = go.
Addcomponent<spriterenderer> ();
Spriterender.sprite = resources.loadassetatpath<sprite> (Datapathtoassetpath (tagImage.FullName)); Animator Animator = go.
Addcomponent<animator> ();
Animator.runtimeanimatorcontroller = Animatorcontroller;
Prefabutility.createprefab (Target_dir + animationname + "/sprite.prefab", go);
Destroyimmediate (GO); } public static string Datapathtoassetpath (string path) {if (Application.platform = = RUNTIMEPLATF Orm. Windowseditor) return path. Substring (path.
IndexOf ("assets\\")); else return path. SubsTring (path.
IndexOf ("assets/"));
Class Animationclipsettings {Serializedproperty m_property;
Private Serializedproperty Get (string property) {return m_property.findpropertyrelative;}
Public animationclipsettings (Serializedproperty prop) {m_property = prop;}
Public float StartTime {get {get {] ("M_starttime"). Floatvalue} set {get ("M_starttime"). Floatvalue = value;} Public float StopTime {get {get {] ("M_stoptime"). Floatvalue} set {get ("M_stoptime"). Floatvalue = value;} Public float Orientationoffsety {get {get {] ("m_orientationoffsety"). Floatvalue} set {get ("M_orientati Onoffsety "). Floatvalue = value;
The public float level {"M_level"). Floatvalue} set {get ("M_level"). Floatvalue = value;}} Public float Cycleoffset {get {get {] ("M_cycleoffset"). Floatvalue} set {get ("M_cycleoffset"). Floatvalue = Value } public bool Looptime {get {"M_looptime"]. Boolvalue} set {get ("M_looptime"). Boolvalue = value;}
public bool Loopblend {get {get {] ("M_loopblend"). Boolvalue} set {get ("M_loopblend"). Boolvalue = value;} public bool Loopblendorientation {get {get] ("M_loopblendorientation"). Boolvalue;} set {get ("M_loopblendorien Tation "). Boolvalue = value; } public bool Loopblendpositiony {get {* get (' m_loopblendpositiony '). Boolvalue;} set {get ("M_LOOPBLENDP Ositiony "). Boolvalue = value; } public bool Loopblendpositionxz {get {* get (' m_loopblendpositionxz '). Boolvalue;} set {get ("M_loopblen Dpositionxz "). Boolvalue = value; } public bool Keeporiginalorientation {get {* get (' m_keeporiginalorientation '). Boolvalue;} set {get ("m_ Keeporiginalorientation "). Boolvalue = value; } public bool Keeporiginalpositiony {get {* get (' m_keeporiginalpositiony '). Boolvalue;} set {get ("M_keep Originalpositiony "). Boolvalue= value; } public bool Keeporiginalpositionxz {get {* get (' m_keeporiginalpositionxz '). Boolvalue;} set {get ("M_ke Eporiginalpositionxz "). Boolvalue = value; } public bool Heightfromfeet {get {get {] ("M_heightfromfeet"). Boolvalue} set {get ("M_heightfromfeet"). b Oolvalue = value;
} public bool Mirror {get {"M_mirror '). Boolvalue;} set {get (" M_mirror "). Boolvalue = value;} }