Unity implements a facelift system

Source: Internet
Author: User


public class Avatarsys:monobehaviour
{

Private Gameobject girlsource;//Resource model
Private Transform Girlsourcetrans;
Private Gameobject girltarget;//Skeleton Object
Private dictionary<string, dictionary<string, skinnedmeshrenderer>> girldata = new Dictionary<string, Dictionary<string, skinnedmeshrenderer>> ();
Transform[] girlhips;//little girl skeleton Information
Private dictionary<string, skinnedmeshrenderer> GIRLSMR = new dictionary<string, skinnedmeshrenderer> (); /Dress up SKM information on bones
Private string[,] girlstr = new string[,] {{"Eyes", "1"}, {"Hair", "1"}, {"Top", "1"}, {"Pants", "1"}, {"Shoes", "1"}, {"Face", "1"}};

private void Start ()
{
Instantiatesource ();
Instantiatetarget ();
SaveData ();
Initavatart ();
}

void Instantiatesource ()
{
Gameobject Go = Instantiate (Resources.load ("Femalemodel")) as Gameobject;
Girlsourcetrans = Go.transform;
Go.setactive (FALSE);
}

void Instantiatetarget ()
{
Girltarget = Instantiate (Resources.load ("Femaletarget")) as Gameobject;
Girlhips = girltarget.getcomponentsinchildren<transform> ();
}

void SaveData ()
{
if (Girlsourcetrans = = null)
Return
Traverse all sub-objects with Skinedmeshrenderer
skinnedmeshrenderer[] parts = girlsourcetrans.getcomponentsinchildren<skinnedmeshrenderer> ();
foreach (var part in parts)
{
string[] names = Part.name.Split ('-');
if (!girldata.containskey (Names[0]))
{
Generates a corresponding part, and generates only one
Gameobject Partgo = new Gameobject ();
Partgo.name = Names[0];
PartGo.transform.parent = Girltarget.transform;
Girlsmr.add (Names[0], partgo.addcomponent<skinnedmeshrenderer> ());//The SKM information stored on the bone target body
Girldata.add (Names[0], new dictionary<string, skinnedmeshrenderer> ());
}
Girldata[names[0]]. ADD (Names[1], part);//store so the SKM information into the data inside
}
}

void Changemesh (string part, String num)
{//Incoming part, number, pick up the corresponding SKM from data
To follow the change of the area
Skinnedmeshrenderer SKM = Girldata[part][num];
list<transform> bones = new list<transform> ();
foreach (Var trans in skm.bones)
{
foreach (var bone in girlhips)
{
if (Bone.name = = trans.name)
{
Bones. Add (bone);
Break
}
}
}
The realization of Dressup
Girlsmr[part].bones = Bones. ToArray ();
Girlsmr[part].materials = skm.materials;
Girlsmr[part].sharedmesh = Skm.sharedmesh;
}

void Initavatart ()
{
Initialize the skeleton and let him have mesh material skeleton information
int length = girlstr.getlength (0);//Get row count
for (int i = 0; i < length; i++)
{
Changemesh (girlstr[i, 0], girlstr[i, 1]);//Let character wear
}
}
}

Unity implements a facelift system

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.