| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AddCollider : MonoBehaviour {
- [ContextMenu("添加Collider")]
- public void Menu()
- {
- Debug.Log("11");
- AddColliderToMesh(this.transform);
- }
- [ContextMenu("移除Collider")]
- public void RemoveMenu()
- {
- Debug.Log("22");
- RemoveColliderToMesh(this.transform);
- }
- public void AddColliderToMesh(Transform tra)
- {
- if (tra.childCount >= 0)
- {
- for (int i = 0; i < tra.childCount; i++)
- {
- Transform temp = tra.GetChild(i);
- if (temp.GetComponent<MeshRenderer>() != null)
- {
- temp.gameObject.AddComponent<MeshCollider>();
- }
-
- AddColliderToMesh(temp);
-
- }
- }
- }
- public void RemoveColliderToMesh(Transform tra)
- {
- if (tra.childCount >= 0)
- {
- for (int i = 0; i < tra.childCount; i++)
- {
- Transform temp = tra.GetChild(i);
- if (temp.GetComponent<MeshRenderer>() != null && temp.GetComponent<MeshCollider>() != null)
- {
- DestroyImmediate(temp.GetComponent<MeshCollider>());
- }
- RemoveColliderToMesh(temp);
- }
- }
- }
- }
|