MB_SwapShirts.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class MB_SwapShirts : MonoBehaviour {
  6. public MB3_MeshBaker meshBaker;
  7. public Renderer[] clothingAndBodyPartsBareTorso;
  8. public Renderer[] clothingAndBodyPartsBareTorsoDamagedArm;
  9. public Renderer[] clothingAndBodyPartsHoodie;
  10. void Start(){
  11. //initial bake
  12. GameObject[] objs = new GameObject[ clothingAndBodyPartsBareTorso.Length ];
  13. for (int i = 0; i < clothingAndBodyPartsBareTorso.Length; i++) {
  14. objs[i] = clothingAndBodyPartsBareTorso[i].gameObject;
  15. }
  16. meshBaker.ClearMesh ();
  17. meshBaker.AddDeleteGameObjects (objs,null,true);
  18. meshBaker.Apply ();
  19. }
  20. // Update is called once per frame
  21. void OnGUI () {
  22. if (GUILayout.Button("Wear Hoodie"))
  23. {
  24. ChangeOutfit(clothingAndBodyPartsHoodie);
  25. }
  26. if (GUILayout.Button("Bare Torso"))
  27. {
  28. ChangeOutfit(clothingAndBodyPartsBareTorso);
  29. }
  30. if (GUILayout.Button("Damaged Arm"))
  31. {
  32. ChangeOutfit(clothingAndBodyPartsBareTorsoDamagedArm);
  33. }
  34. }
  35. void ChangeOutfit(Renderer[] outfit)
  36. {
  37. //collect the meshes we will be removing
  38. List<GameObject> objectsWeAreRemoving = new List<GameObject>();
  39. foreach (GameObject item in meshBaker.meshCombiner.GetObjectsInCombined())
  40. {
  41. Renderer r = item.GetComponent<Renderer>();
  42. bool foundInOutfit = false;
  43. for (int i = 0; i < outfit.Length; i++)
  44. {
  45. if (r == outfit[i])
  46. {
  47. foundInOutfit = true;
  48. break;
  49. }
  50. }
  51. if (!foundInOutfit)
  52. {
  53. objectsWeAreRemoving.Add(r.gameObject);
  54. Debug.Log("Removing " + r.gameObject);
  55. }
  56. }
  57. //Now collect the meshes we will be adding
  58. List<GameObject> objectsWeAreAdding = new List<GameObject>();
  59. for (int i = 0; i < outfit.Length; i++)
  60. {
  61. if (!meshBaker.meshCombiner.GetObjectsInCombined().Contains(outfit[i].gameObject))
  62. {
  63. objectsWeAreAdding.Add(outfit[i].gameObject);
  64. Debug.Log("Adding " + outfit[i].gameObject);
  65. }
  66. }
  67. meshBaker.AddDeleteGameObjects(objectsWeAreAdding.ToArray(), objectsWeAreRemoving.ToArray(),true);
  68. meshBaker.Apply();
  69. }
  70. }