[Unity Combat] Detailed dress up System (III)

Source: Internet
Author: User

Before reading this article, I strongly recommend that you look at the first two articles of this series, and then continue after some understanding of the system!


In the previous article, this was done after running:


Our target hangs on various types of mesh, and each mesh has a skinned mesh renderer component, which will undoubtedly increase the computational volume, according to the official demo guidelines, we should merge Mesh, This means that target has only one skinned Mesh renderer component for optimization purposes!


I have made some changes to the code of the previous article, mainly to add 6 new code, and do not need to comment on the code (not deleted), easy to understand.

In fact, the code with the previous article is not very different. The previous article was to replace the mesh for a single spot under target, binding the skeleton. And this article is first to target all the parts of the mesh, material, skeleton all stored well, and once again to assign to target the only skinned mesh Renderer.


The code is as follows:

Using unityengine;using system.collections;using system.collections.generic;public class avatarsys2:monobehaviour{P    Ublic static AVATARSYS2 instance;    Source Model private Transform source;    Target skeleton private Transform target; The information of the source model is stored, respectively, the part name, the part number, the Skinnedmeshrenderer component of the part//according to the site name, part number to obtain the desired part of the private dictionary<string, Dictionary <string, skinnedmeshrenderer>> sourcedata = new dictionary<string, dictionary<string, SkinnedMeshRen    Derer>> (); New Skinnedmeshrenderer Targetsmr;//target has only one skinnedmeshrenderer component list<combineinstance> CombineInstan CES = new list<combineinstance> ();//store all mesh list<material> on target materials = new list<material> () ;//Store all material list<transform> bones = new list<transform> () on target, and//store all bone////on target to hold the information of the skeleton Do not name the part, the Skinnedmeshrenderer component////is not numbered because target has only a set of//private dictionary<string, skinnedmeshrenderer> tar GETSMR =//NEW dictionary<string, skinnedmeshrenderer> ();    The skeleton information of the target skeleton is stored for mesh binding skeleton private transform[] hips; Site information, the target skeleton is initialized to the desired part of the model, note that the naming is a public string[named according to the location on the source model,] Avatarstr = new string[,]{{"Face", "1"},{"Hair", "1"}, {"P    Ants "," 1 "},{" Shoes "," 1 "},{" Top "," 1 "}};        Use the this for initialization void Start () {instance = this; Instantiatesource ();//Instantiate the Source Model Instantiatetarget ();//Instantiate the target skeleton SaveData ();//The information that stores the source model and the target skeleton Initavatar ();    Initialize the target skeleton to model target.animation.PlayQueued ("Walk");        } void Instantiatesource () {Gameobject sourceobj = instantiate (Resources.load ("Source")) as Gameobject;        Source = Sourceobj.transform;    Sourceobj.setactive (FALSE);        } void Instantiatetarget () {Gameobject targetobj = instantiate (Resources.load ("Target")) as Gameobject;        target = Targetobj.transform; Hips = target.        Getcomponentsinchildren<transform> (); New TARGETSMR = Target.gameObject.addcomponent<skinnedmeshrenderer> (); } void SaveData () {if (source = = null) | |            (target = = null))        Return skinnedmeshrenderer[] Parts = source. Getcomponentsinchildren<skinnedmeshrenderer> (true);//true means that the component of the hidden part is also given a foreach (Skinnedmeshrenderer parts in P            Arts) {string[] PartName = Part.name.Split ('-');                if (!sourcedata.containskey (partname[0]))//each with a new type of site {////each with a new type of part creates an empty gameobject under the skeleton                Gameobject partobj = new Gameobject ();                Partobj.name = partname[0];                Partobj.transform.parent = target;                The site type is only recorded once Sourcedata.add (Partname[0], new dictionary<string, skinnedmeshrenderer> ()); TARGETSMR only records a set of parts of the information, such as trousers have two sets of models, only a set of////because target is used to display, all parts of a set can be//targetsmr.add (PA Rtname[0], Partobj.            Addcomponent<skinnedmeshrenderer> ());     }       SourceData Record all site information sourcedata[partname[0]].        ADD (Partname[1], part);        }} public void Changemesh (string part, string num)//incoming site name, number {///because SourceData records all site information, you can find the new part you want.        Skinnedmeshrenderer SMR = Sourcedata[part][num];        list<transform> bones = new list<transform> ();  Depending on the skeleton information of the new part, locate the corresponding position on the target's skeleton//such as hair mesh, in source it is bound to the head, then we will find the head in target also this position foreach (Transform bone                In Smr.bones) {foreach (Transform hip in hips) {if (Hip.name = = bone.name) {Bones.                    Add (hip);                Break }}}//targetsmr[part].materials = smr.materials;//replacement material//targetsmr[part].sharedmesh = SM r.sharedmesh;//Replace mesh//targetsmr[part].bones = bones.        ToArray ();//bind mesh to the appropriate skeleton//new combineinstance ci = new combineinstance ();        Ci.mesh = Smr.sharedmesh; CoMbineinstances.add (CI); Materials.    AddRange (smr.materials);        }//new public void Initavatar () {Empty ();        int length = avatarstr.getlength (0);        for (int i = 0; i < length; i++) {Changemesh (avatarstr[i, 0], avatarstr[i, 1]);    } Combine ();        }//new void Empty () {combineinstances.clear (); Materials.        Clear (); Bones.    Clear ();        }//new void Combine () {Targetsmr.sharedmesh = new Mesh ();        TargetSmr.sharedMesh.CombineMeshes (Combineinstances.toarray (), false,false); targetsmr.materials = materials.        ToArray (); Targetsmr.bones = Bones.    ToArray (); }}

After running:





What do you think? It's cool!

[Unity Combat] Detailed dress up System (III)

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.