Firstborn/Assets/MeshBaker/Examples/CharacterCustomization/MB_SwapShirts.cs
Schaken-Mods 959e80cf72 assets upload
assets upload description.
2023-03-28 12:16:30 -05:00

79 lines
2.4 KiB
C#

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class MB_SwapShirts : MonoBehaviour {
public MB3_MeshBaker meshBaker;
public Renderer[] clothingAndBodyPartsBareTorso;
public Renderer[] clothingAndBodyPartsBareTorsoDamagedArm;
public Renderer[] clothingAndBodyPartsHoodie;
void Start(){
//initial bake
GameObject[] objs = new GameObject[ clothingAndBodyPartsBareTorso.Length ];
for (int i = 0; i < clothingAndBodyPartsBareTorso.Length; i++) {
objs[i] = clothingAndBodyPartsBareTorso[i].gameObject;
}
meshBaker.ClearMesh ();
meshBaker.AddDeleteGameObjects (objs,null,true);
meshBaker.Apply ();
}
// Update is called once per frame
void OnGUI () {
if (GUILayout.Button("Wear Hoodie"))
{
ChangeOutfit(clothingAndBodyPartsHoodie);
}
if (GUILayout.Button("Bare Torso"))
{
ChangeOutfit(clothingAndBodyPartsBareTorso);
}
if (GUILayout.Button("Damaged Arm"))
{
ChangeOutfit(clothingAndBodyPartsBareTorsoDamagedArm);
}
}
void ChangeOutfit(Renderer[] outfit)
{
//collect the meshes we will be removing
List<GameObject> objectsWeAreRemoving = new List<GameObject>();
foreach (GameObject item in meshBaker.meshCombiner.GetObjectsInCombined())
{
Renderer r = item.GetComponent<Renderer>();
bool foundInOutfit = false;
for (int i = 0; i < outfit.Length; i++)
{
if (r == outfit[i])
{
foundInOutfit = true;
break;
}
}
if (!foundInOutfit)
{
objectsWeAreRemoving.Add(r.gameObject);
Debug.Log("Removing " + r.gameObject);
}
}
//Now collect the meshes we will be adding
List<GameObject> objectsWeAreAdding = new List<GameObject>();
for (int i = 0; i < outfit.Length; i++)
{
if (!meshBaker.meshCombiner.GetObjectsInCombined().Contains(outfit[i].gameObject))
{
objectsWeAreAdding.Add(outfit[i].gameObject);
Debug.Log("Adding " + outfit[i].gameObject);
}
}
meshBaker.AddDeleteGameObjects(objectsWeAreAdding.ToArray(), objectsWeAreRemoving.ToArray(),true);
meshBaker.Apply();
}
}