CombinedMeshModification.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. using LunarCatsStudio.SuperCombiner;
  4. namespace LunarCatsStudio.SuperCombiner
  5. {
  6. /// <summary>
  7. /// Attach this script to each combined Gameobject that you wish to remove part during runtime.
  8. /// This only works for a combined GameObject with "combine mesh" parameter set to true.
  9. /// You can remove parts of the combined mesh using the "RemoveFromCombined" API. Use the instanceID of the object you wish to
  10. /// remove. In order know the correct instanceID, check in the "combinedResults" file under "mesh Results" -> "Instance Ids".
  11. /// </summary>
  12. public class CombinedMeshModification : MonoBehaviour
  13. {
  14. // The combined result
  15. [Tooltip("Reference to the _combinedResult file")]
  16. public CombinedResult _combinedResult;
  17. // The MeshFilter to which the combinedMesh is set
  18. [Tooltip("Reference to the MeshFilter in which the combined mesh is attached to")]
  19. public MeshFilter _meshFilter;
  20. // A new instance of combined result is created at runtime to keep original intact
  21. private CombinedResult _currentCombinedResult;
  22. // Use this for initialization
  23. void Awake()
  24. {
  25. // Instanciate a copy of the _combinedResult
  26. _currentCombinedResult = GameObject.Instantiate(_combinedResult) as CombinedResult;
  27. }
  28. /// <summary>
  29. /// Remove a GameObject from the combined mesh
  30. /// </summary>
  31. /// <param name="gameObject"></param>
  32. public void RemoveFromCombined(GameObject gameObject)
  33. {
  34. RemoveFromCombined (gameObject.GetInstanceID ());
  35. }
  36. /// <summary>
  37. /// Remove a GameObject from the combined mesh
  38. /// </summary>
  39. /// <param name="instanceID"></param>
  40. public void RemoveFromCombined(int instanceID)
  41. {
  42. // Check if _meshFilter is set
  43. if (_meshFilter == null)
  44. {
  45. Logger.Instance.AddLog("SuperCombiner", "MeshFilter is not set, please assign MeshFilter parameter before trying to remove a part of it's mesh", Logger.LogLevel.LOG_WARNING);
  46. return;
  47. }
  48. bool success = false;
  49. foreach (MeshCombined meshResult in _currentCombinedResult._meshResults)
  50. {
  51. if (meshResult.instanceIds.Contains(instanceID))
  52. {
  53. Logger.Instance.AddLog("SuperCombiner", "Removing object '" + instanceID + "' from combined mesh");
  54. _meshFilter.mesh = meshResult.RemoveMesh(instanceID, _meshFilter.mesh);
  55. success = true;
  56. }
  57. }
  58. if (!success)
  59. {
  60. Logger.Instance.AddLog("SuperCombiner", "Could not remove object '" + instanceID + "' because it was not found", Logger.LogLevel.LOG_WARNING);
  61. }
  62. }
  63. }
  64. }