Unity Character Dressup
Character Dressup is a game in the old terrier, in order to beautify the game, for a better game experience, a lot of games can realize the character dressup, but also to the characters change weapons, but also wings and so on. In short, can be in the player in the experience to bring a good effect.
First look at the effect diagram.
This changes the image of a character a little bit. Add some fun to the game. The production of the character's skin
First look at the production of character skin, in fact, here art has to make a few sets of models, we give the first set of character model to add the skeleton, the back of the skin are not added on the skeleton. Only add skinnedmeshrenderer to it, and only adds a set of Skinnedmeshrenderer, can not add duplicates.
Here you can go to batch build clothes prefab. The best createprefabinternal () function is a custom-made prefab function.
[MenuItem ("gameobject/batch generation role clothes Prefab", false, 0)] public static void Generatebodyclothprefab () {if (index
>= 1) {Resetcreatenormalprefabbyfbxmane ();
Return
} index++;
gameobject[] Objs = selection.gameobjects; for (int index1 = 0; index1 < Objs. Length;
index1++) {Gameobject obj = objs[index1]; if (obj. getcomponent<skinnedmeshrenderer> () = null) {skinnedmeshrenderer[] skins = obj.
Getcomponentsinchildren<skinnedmeshrenderer> (); if (skins! = null && skins. Length > 0) {for (int i = 0; i < skins. Length;
i++) {createsinglebodyskinprefab (skins[i].gameobject);
}}} and else {createsinglebodyskinprefab (obj);
}
}
}public static void Createsinglebodyskinprefab (Gameobject skin) {if (skin = = null) return; Skinbonesrecord Bonerecord = skin.
Getcomponent<skinbonesrecord> (); if (Bonerecord = = null) Bonerecord = skin.
Addcomponent<skinbonesrecord> ();
Bonerecord.recordbonenames (); Skinnedmeshrenderer mesh = skin.
Getcomponent<skinnedmeshrenderer> ();
Mesh.receiveshadows = false;
Mesh.uselightprobes = false;
Mesh.shadowcastingmode = UNITYENGINE.RENDERING.SHADOWCASTINGMODE.OFF;
Editorutility.setdirty (skin);
String Prefabpath = Tempprefabfolderpath + skin.name + "Skin.prefab";
Createprefabinternal (skin, Prefabpath); }
Load Clothes Model
The prefab of the clothes was made, and we loaded the assetbundle into the game. Here is the ID of the Showclothstyle to load it in, if it is 0, then unload the current clothes, or go in to dress the function.
public void Showequipsfashioncloth (int showclothstyle)
{
if ( LoadConfigManager.ModelShowContainer.dic.ContainsKey (Showclothstyle) = = False)
{
#if unity_editor
Debug.logformat ("There is no fashion {0}", Showclothstyle);
#endif
return;
}
#region Clothing model generates
if (Showclothstyle! = 0)
{
//replace
if (showclothstyle! = _lastclothstyle && Showclothstyle! = 0)
{ Charactergene.changebodycloth (Showclothstyle, false);}
}
else
{
//Remove Equipment
if (_lastclothstyle! = 0)
{
charactergene.removebodycloth ();
}
}
#endregion
_lastclothstyle = Showclothstyle;
}
The following function saves the ID number of the old garment and then updates the number to the new ID number.
public void Changebodycloth (int modelid, bool Isngui = False, system.action doneact = null)
{
int oldclothid = 0;< C2/>if (Equipsmodelcache.containskey ("Body"))
Oldclothid = equipsmodelcache["Body"];
var model = LoadConfigManager.ModelShowContainer.GetModelShow (modelid);
if (model = = null)
{
return;
}
equipsmodelcache["Body" = ModelID;
GameManager.assetBundleNew.StartCoroutine (_changebodycloth (ModelID, Isngui,oldclothid, doneact));
}
Because you want to load in Assetbundle, you need to use the co-function.
Private IEnumerator _changebodycloth (int clothid, bool Isngui, int oldclothid, system.action doneact) {if (c Haracterroot = = null) {DEBUG.LOG ("!!!
Character Root is not exist ");
Yield break;
} bool ismaprelevant = true;
if (Isngui) ismaprelevant = true;
Gameobject newskinobj = GameManager.objectCacheMgr.SpawnByModelStyle (Cachetype.resname_model, Clothid, true); if (newskinobj = = null) {var abinfo = GameManager.assetBundleNew.CreateModelAB (Clothid, False, True,
Ismaprelevant, Isngui);
if (Abinfo = = null) {yield break; } while (!abinfo.
IsDone) yield return null;
Newskinobj = Abinfo.objinst as Gameobject; if (ismaprelevant)//Read AssetBundles.AssetBundleManagerNew.AddToMapRelDyUnloadAbInfos from the cache pool (Abinfo, Ismaprel
Evant); else//PutCache inside AssetBundles.ABLoadInfo.Release (abinfo);
if (newskinobj = = null) {yield break; }} if (Destroy) {GameManager.objectCacheMgr.CacheExsistGOToCachePool (5, Clothid, "", NEWSK
Inobj, False, false);
Yield break;
} if (!equipsmodelcache.containskey ("Body") | | equipsmodelcache["BODY"]! = Clothid) {#if unity_editor
Debug.logwarning (Clothid + "---download Clothid is not cur clothid----" + "body--" + equipsmodelcache["body"]);
#endif GameManager.objectCacheMgr.CacheExsistGOToCachePool (5, Clothid, "", Newskinobj, False, false);
Yield break;
}//share a set of total bones, but need to update information, need public download if (skins = = null) {Debug.logerror ("no Skin");
Yield break; } skinnedmeshrenderer Newskin = Newskinobj.
Getcomponent<skinnedmeshrenderer> (); Skinbonesrecord Skinbonesrecord = newskinobj. GEtcomponent<skinbonesrecord> ();
if (Newskin = = null) {Debug.logerror ("new Skin obj no Skinnedmeshrender" + newskinobj.name);
Gameobject.destroy (Newskinobj);
Yield break; } if (Skinbonesrecord = = null) {Debug.logerror ("new Skin obj no Skinbonesrecord" + newskinob
J.name);
Gameobject.destroy (Newskinobj);
Yield break;
} list<transform> bones = new List<transform> (40); for (int i = 0, i < SkinbonesRecord.BoneNames.Count; i++) {for (int j = 0; J < Bonetrans. Length;
J + +) {if (bonenames[j] = = Skinbonesrecord.bonenames[i]) { Bones.
ADD (Bonetrans[j]);
Break
}}} if (normalroot = = null) {Gameobject.destroy (newskinobj);
Yield break; } if (nEwskinmesh = = null) {Newskinmesh = Newskin; Newskin.bones = Bones.
ToArray ();
Newskinobj.transform.SetParent (Normalroot.transform);
Newskinobj.transform.localPosition = Vector3.zero;
Newskinobj.transform.localEulerAngles = new Vector3 (-90, 0, 0);
Newskinobj.transform.localScale = Vector3.one;
BaseSkinMesh.gameObject.SetActive (FALSE); Newskinobj.
SetActive (TRUE); } else {newskin.bones = bones.
ToArray ();
Newskinobj.transform.SetParent (Normalroot.transform);
Newskinobj.transform.localPosition = Vector3.zero;
Newskinobj.transform.localEulerAngles = new Vector3 (-90, 0, 0);
Newskinobj.transform.localScale = Vector3.one;
BaseSkinMesh.gameObject.SetActive (FALSE); Newskinobj.
SetActive (TRUE); if (Oldclothid! = 0) GameManager.objectCacheMgr.CacheExsistGOToCachePool (5, Oldclothid, "", Newskinmesh.gameobject, False, false);
else Gameobject.destroyimmediate (newskinmesh.gameobject);
Newskinmesh = Newskin;
} if (Isngui) {Nguitools.setlayer (newskinmesh.gameobject, (int) Scenelayer.ngui); } else {util.setobjectlayerwithtagfilter (Charobtype, Newskinmesh.gameobject, Const.scenetags_
Modeleffect);
} changeskinshader ();
if (doneact! = null) doneact (); }