Morph 3D Tutorial Part 3: Adding MCSMaleLite to scene and adding / changing clothes at runtime

This tutrial’s got outdated with M3D 1.6 release. Might still be useful but not entirely accurate.

In this part of the tutorial we’ll instantiate the MCS model at runtime and add some clothes on him. But before that let’s see how we can show/hide clothes on an existing Morph 3D model at runtime.

We’ll use the scene we created for Tutorial Part 2. Open the scene and select Canvas from hierarchy view. Add two buttons to UI by selecting Create -> UI -> Button. Name them DressedButton and NakedButton. Edit their text as Dressed and Naked. Anchor them to top left adn position them to 120, -65, 0 and 120, -100, 0 respectively.

Open the UIController script and modify it so it looks something like this.

What we basically do is setting and keeping a list of visible clothing and make them visible/invisible in the loop. We keep it in the clothingList field because GetVisibleClothing only gets the visible clothings. When we make them invisible we can’t use this method to get the list. (I couldn’t figure out a way to get the invisible or full clothing list, if anyone knows a way please leave a comment.) Since eyes are considered clothing by Morph 3D we need to check if the clothing is G2FSimplifiedEyes before making it invisible, unless you want to see a freaky naked man.

Go back to unity and add new methods MakeModelDressed and MakeModelNaked to buttons’ On Click events. (Hit + at the bottom, drag Canvas object to slot and select UIController -> MakeModelDressed() and same for the naked one)

If you hit play you’ll see that you can dress and undress the model at runtime. You’ll notice if you change the weight blenshape when naked and hit Dressed button bone positions look awkward. This is probably because the clothing blendshapes doesn’t update when invisible. You can add a few lines of code at the end of dressed code that sets one of the blendshapes to 0 and back to current value to workaround that issue (If anyone knows a method to trigger that please leave a comment).

This approach requires all of the clothings attached to the model beforehand. This could cause some performance issues and it also is not convenient by any means since you would need to attach every possible clothing to every model with a probability of changing clothes (player, companions etc.)

There is one other issue. If you are planning to have a character creation feature and leave the choice of sex to the player you won’t know which model to use at runtime either. You can always have both male and female model and do the hide/show thing like the clothing. But it gets even more complicated when coupled with all the clothings on those two models.

I think you’ll agree with me whan I say we need to be able to instantiate models and clothes at runtime.

Save your scene if you haven’t done so yet and open a new scene then save it too. Like in the Tutorial Part 2 add a plane, a canvas, MultipurposeCaneraRig and ThirdPersonController prefab to the scene. Delete MainCamera and the Ethan stuff under ThirdPersonController. Remember to tag ThirdPersonController as player and MultipurposeCameraRig as Main Camera. You should have a hierarchy view looking like this.

0014

We need an object in the scene to hold the character creation script. We could use the player game object but we’ll create an empty game object in the hierarchy view and name it GameController. Now create a C# script under Assets/Scripts (create the scripts folder) named “GameController” and drag it on to GameController game object in hierarchy view.

Now we are ready to start coding. The rest of the stuff we’ll add at runtime.

Start by adding imports.

Then add these variables.

The ones we marked as [SerializeField] needs to be assigned before we run the game. Go back to unity editor and click on the GameController in the hierarchy view. You should see the GameController (Script) section in Inspector. Drag ThirdPersonController from hierarchy view to Player Controller slot. Drag MCSMaleLite prefab from project view (MORPH3D->Content_>StarterPacks->Male->MCSMaleLite folder) to Moel Prefab slot. Drag UrbanMetro prefab from project view (MORPH3D->Content_>StarterPacks->Male->hair->BritHair folder) to Clothing Content Pack slot. Drag BritHair prefab from project view (MORPH3D->Content_>StarterPacks->Male->outfits->UrbanMetro folder) to Hair Content Pack slot.

GameController (Script) should look something like this:

0015

Add methods for model creation and hair and call them from Start method.

I will not go into detail much but one of the important things here is setting the player controller’s animator’s avatar as MCSMaleLite model’s avatar. If we skipped this our model wouldn’t animate. You could set this in the scene by changing from the Unity UI but consider that you are creating the character from a saved character creation data. And that you must instantiate a male or female model accordingly. You wouldn’t know beforehand which avatar to set as player controller’s.

If you hit play now you’ll see that the Male model will be created and hair added. (You can run around naked for a while, you deserved it. Hit play button again to stop when you’re done.)

To demonstrate adding and removing clothes we’ll add two buttons to the scene much like the ones at the beginning of this tutorial part.

For dressing up add this function to GameController script. Remember to bind the Dressed button to this function.

As you see adding clothing is very similar to adding hair. The last line makes manager aware of newly added clothing. If you ommit this line you can’t access currently loaded clothing by manager later (naked function). I haven’t tried but I suspect blendshapes might not propogate to clothing either.

I think you could add a game object for clothings to the scene. Add a script to it with an array to hold all the content pack prefab refernces. Drag the prefabs to the arrays slots and make the game object itself a prefab. This could be used as a clothing library. The same goes for hair.

The last part is the function to remove clothings. (remember to bind this function to the second button)

As you can see we loop through the clothing items and destroy them. I used destroy because I couldn’t find any manager method to remove clothing (leave a comment if you are aware of a better way please). Since eyes are considered clothing by Morph3D and we want to keep them we check if it’s the eyes before destroying.

If you run the game now you can dress/undress the model by clicking the buttons.

And for convenience here is the complete GameController script.

Links to the other parts of the tutorial:

[Total: 2   Average: 2/5]