MB_SkinnedMeshSceneController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class MB_SkinnedMeshSceneController : MonoBehaviour {
  5. public GameObject swordPrefab;
  6. public GameObject hatPrefab;
  7. public GameObject glassesPrefab;
  8. public GameObject workerPrefab;
  9. public GameObject targetCharacter;
  10. public MB3_MeshBaker skinnedMeshBaker;
  11. GameObject swordInstance;
  12. GameObject glassesInstance;
  13. GameObject hatInstance;
  14. void Start () {
  15. //To demonstrate lets add a character to the combined mesh
  16. GameObject worker1 = (GameObject) Instantiate(workerPrefab);
  17. worker1.transform.position = new Vector3(1.31f, 0.985f, -0.25f);
  18. Animation anim = worker1.GetComponent<Animation>();
  19. anim.wrapMode = WrapMode.Loop;
  20. //IMPORTANT set the culling type to something other than renderer. Animations may not play
  21. //if animation.cullingType is left on BasedOnRenderers. This appears to be a bug in Unity
  22. //the animation gets confused about the bounds if the skinnedMeshRenderer is changed
  23. anim.cullingType = AnimationCullingType.AlwaysAnimate; //IMPORTANT
  24. anim.Play("run");
  25. //create an array with everything we want to add
  26. //It is important to add the gameObject with the Renderer/mesh attached
  27. GameObject[] objsToAdd = new GameObject[1] {worker1.GetComponentInChildren<SkinnedMeshRenderer>().gameObject};
  28. //add the objects. This will disable the renderers on the source objects
  29. skinnedMeshBaker.AddDeleteGameObjects(objsToAdd, null, true);
  30. //apply the changes to the mesh
  31. skinnedMeshBaker.Apply();
  32. }
  33. void OnGUI () {
  34. if (GUILayout.Button ("Add/Remove Sword")) {
  35. if (swordInstance == null){
  36. Transform hand = SearchHierarchyForBone(targetCharacter.transform,"RightHandAttachPoint");
  37. swordInstance = (GameObject) Instantiate(swordPrefab);
  38. swordInstance.transform.parent = hand;
  39. swordInstance.transform.localPosition = Vector3.zero;
  40. swordInstance.transform.localRotation = Quaternion.identity;
  41. swordInstance.transform.localScale = Vector3.one;
  42. GameObject[] objsToAdd = new GameObject[1] {swordInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  43. skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
  44. skinnedMeshBaker.Apply();
  45. } else if (skinnedMeshBaker.CombinedMeshContains(swordInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
  46. GameObject[] objsToDelete = new GameObject[1] {swordInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  47. skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
  48. skinnedMeshBaker.Apply();
  49. Destroy(swordInstance);
  50. swordInstance = null;
  51. }
  52. }
  53. if (GUILayout.Button ("Add/Remove Hat")) {
  54. if (hatInstance == null){
  55. Transform hand = SearchHierarchyForBone(targetCharacter.transform,"HeadAttachPoint");
  56. hatInstance = (GameObject) Instantiate(hatPrefab);
  57. hatInstance.transform.parent = hand;
  58. hatInstance.transform.localPosition = Vector3.zero;
  59. hatInstance.transform.localRotation = Quaternion.identity;
  60. hatInstance.transform.localScale = Vector3.one;
  61. GameObject[] objsToAdd = new GameObject[1] {hatInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  62. skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
  63. skinnedMeshBaker.Apply();
  64. } else if (skinnedMeshBaker.CombinedMeshContains(hatInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
  65. GameObject[] objsToDelete = new GameObject[1] {hatInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  66. skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
  67. skinnedMeshBaker.Apply();
  68. Destroy(hatInstance);
  69. hatInstance = null;
  70. }
  71. }
  72. if (GUILayout.Button ("Add/Remove Glasses")) {
  73. if (glassesInstance == null){
  74. Transform hand = SearchHierarchyForBone(targetCharacter.transform,"NoseAttachPoint");
  75. glassesInstance = (GameObject) Instantiate(glassesPrefab);
  76. glassesInstance.transform.parent = hand;
  77. glassesInstance.transform.localPosition = Vector3.zero;
  78. glassesInstance.transform.localRotation = Quaternion.identity;
  79. glassesInstance.transform.localScale = Vector3.one;
  80. GameObject[] objsToAdd = new GameObject[1] {glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  81. skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
  82. skinnedMeshBaker.Apply();
  83. } else if (skinnedMeshBaker.CombinedMeshContains(glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
  84. GameObject[] objsToDelete = new GameObject[1] {glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject};
  85. skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
  86. skinnedMeshBaker.Apply();
  87. Destroy(glassesInstance);
  88. glassesInstance = null;
  89. }
  90. }
  91. }
  92. public Transform SearchHierarchyForBone(Transform current, string name)
  93. {
  94. if (current.name.Equals( name ))
  95. return current;
  96. for (int i = 0; i < current.childCount; ++i)
  97. {
  98. Transform found = SearchHierarchyForBone(current.GetChild(i), name);
  99. if (found != null)
  100. return found;
  101. }
  102. return null;
  103. }
  104. }