Unity Dressup Implementation

Source: Internet
Author: User

About Unity's dressup, there are a few articles on the Internet, I also briefly described the implementation. But at that time it was just a rough experiment. Have a good comb of the code today.

First on the code (own game project, not the company, so rest assured that the project code, part of the reference to other functional files, but the core code has no effect, here the main look at the details and ideas)

Using unityengine;using system.collections;using system.collections.generic;public enum avatarpart{helmet, chest, Shoulders, gloves, boots,}//people dressup public class actoravatar:monobehaviour{//Dress Up Parts Information public class Avatarinf        o {public string partname;        Public Gameobject Defaultpart;    Public Gameobject Avatarpart;    } protected int _bodymodelid;         protected Gameobject _body;        Basic model animation protected dictionary<string, avatarinfo> _avatarinfo = new dictionary<string, avatarinfo> ();    Dressup Information private list<int> _avatarloadqueue = new list<int> (); void Start () {} void Update () {}//create model public void Loadmodel (int modelid) {_bodymodeli        D = modelid;            ResourceMgr.Instance.LoadModel (ModelID, (gameobject obj) = {_body = obj; Dressup Request if (_avatarloadqueue.count > 0) {foreach (var avatar in _avatarloadqueue) {                   Loadavatar (avatar);            } _avatarloadqueue.clear ();    }}, True);            }//To the character dressup public void Loadavatar (int avatarid) {//If the underlying model has not yet been loaded, wait for if (_body = = null) {            _avatarloadqueue.add (Avatarid);        Return        } avatardata adata = DataMgr.Instance.GetAvatarData (Avatarid);        ResourceMgr.Instance.LoadModel (Adata.model, (gameobject obj) = {Changeavatar (obj, adata.addpart);    });  }//Replacement part public void Changeavatar (Gameobject avatarmodel, String partname) {//unload the current part first Avatarinfo        CurrentInfo;                if (_avatarinfo.trygetvalue (PartName, out currentinfo)) {if (Currentinfo.avatarpart! = null) {                Destroy (Currentinfo.avatarpart);            Currentinfo.avatarpart = null;            } if (Currentinfo.defaultpart! = null) {currentInfo.defaultPart.SetActive (true);       } }//Avatarmodel is a resource, and there is no instantiation of if (Avatarmodel = = null) {return;        }//parts to be replaced Transform Avatarpart = GetPart (Avatarmodel.transform, partname); if (Avatarpart = = null) {Debug.logerror (string.            Format ("Avatar part not Found:", PartName));        Return        }//Hides the original part Transform bodypart = GetPart (_body.transform, partname);        if (bodypart! = null) {bodyPart.gameObject.SetActive (false);        }//Set to new object on body gameobject Newpart = Gameobject (PartName);        NewPart.transform.parent = _body.transform;        Skinnedmeshrenderer Newpartrender = newpart.addcomponent<skinnedmeshrenderer> ();        Skinnedmeshrenderer Avatarrender = avatarpart.getcomponent<skinnedmeshrenderer> ();        Refresh Bone model Data setbones (Newpart, Avatarpart.gameobject, _body);        Newpartrender.sharedmesh = Avatarrender.sharedmesh; Newpartrender.sharedmaterials = AvaTarrender.sharedmaterials;        Record Dressup Information Avatarinfo info = new Avatarinfo ();        Info.partname = PartName;        if (bodypart! = null) {Info.defaultpart = Bodypart.gameobject;        } else {info.defaultpart = null;        } Info.avatarpart = Newpart;    _avatarinfo[partname] = info;  }//recursive traverse child object public static Transform GetPart (Transform T, string searchname) {foreach (Transform C in t)                        {String partname = C.name.tolower ();            if (Partname.indexof (searchname)! =-1) {return C;                } else {Transform r = GetPart (c, searchname);                if (r! = null) {return r;    }}} return null;            } public static Transform Findchild (Transform T, string searchname) {foreach (Transform C in t) {            string partname = C.name;                if (PartName = = Searchname) {return C;                } else {Transform r = Findchild (c, searchname);                if (r! = null) {return r;    }}} return null; }//Refresh bone data to update the bodypart skeleton of the root object to Avatarpart public static void Setbones (Gameobject gobodypart, Gameobject Goavatar        Part, Gameobject root) {var bodyrender = gobodypart.getcomponent<skinnedmeshrenderer> ();        var avatarrender = goavatarpart.getcomponent<skinnedmeshrenderer> ();        var mybones = new Transform[avatarrender.bones.length]; for (var i = 0; i < avatarRender.bones.Length; i++) {Mybones[i] = Findchild (Root.transform, Avatarrender.bo        Nes[i].name);    } bodyrender.bones = Mybones; }}


1, Unity Dressup has three kinds of requirements:

Add a weapon to the mount to dress up, this just create the corresponding model, and set the transform.parent on it OK.

Replace the texture, this takes the corresponding material, and then set the texture on it.

The replacement of the model parts, this is handled here, but also the most complex dress up.


2, the most core part is the Changeavatar, it completes the model to dress the function. The replacement of the model parts is actually replacing the Sharedmesh and sharedmaterials in the Skinnedmeshrender.

(Here is a little bit of sharedmaterials sharedmaterial materials material The difference between these variables.) Sharedmaterials is a shared and referenced relationship, as long as you modify this, all models that use this material will be affected.    If it is in editor mode, it also modifies the properties of the actual material file.    Materials is a copy of the sharedmaterials, only the current model is used. Materia is the first object in the materials array, which is only for the convenience of writing. )

Just replacing the Sharedmesh is not enough, the model will turn into a lump of twist.  You should also modify the Bones property in Skinnedmeshrender, which records the skeleton information of the model (which is actually a lot of transform). The Setbones function completes the skeleton substitution operation. It finds all the bone names in the avatar part, and then finds the corresponding bone names in the current model and stores them. This array is the skeleton information for the new part.


3, a logical processing of details. The corresponding parts of the original model are retained, and the part is not destroyed, just hidden. When you unload your equipment, you only need to delete the parts and set the default part to visible.


4. Consider using Unity's combineinstance to merge the model, which has the advantage of improving operational performance. But only when the material is shared with one can it really optimize the effect. There's a meshbaker plugin that's cool. If you are going to fight for thousands of people, you have to consider this optimization.



Unity Dressup Implementation

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.