public static ColliderPart Create(Transform parent, string name = "Collider") { #if UNITY_EDITOR GameObject go = new GameObject(name); UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Created Collider Part GameObject"); UnityEditor.Undo.SetTransformParent(go.transform, parent, "Parent New Collider Part GameObject"); BuildRPart part = go.GetComponent <BuildRPart>(); if (part == null) { part = UnityEditor.Undo.AddComponent <BuildRPart>(go); } part.parent = parent.GetComponent <BuildRPart>(); ColliderPart output = UnityEditor.Undo.AddComponent <ColliderPart>(go); return(output); #else GameObject go = new GameObject(name); go.transform.parent = parent; BuildRPart part = go.GetComponent <BuildRPart>(); if (part == null) { part = go.AddComponent <BuildRPart>(); } part.parent = parent.GetComponent <BuildRPart>(); ColliderPart output = go.AddComponent <ColliderPart>(); return(output); #endif }
public void GenerateFromDynamicMesh(BuildRMesh mesh) { if (_mesh == null) { _mesh = new Mesh(); } if (_meshCollider == null) { #if UNITY_EDITOR _meshCollider = UnityEditor.Undo.AddComponent <MeshCollider>(gameObject); #else _meshCollider = gameObject.AddComponent <MeshCollider>(); #endif } // Debug.Log("GenerateFromDynamicMesh "+ _mesh.vertexCount+" "+_mesh.triangles.Length); mesh.Build(_mesh); _meshCollider.sharedMesh = _mesh; if (mesh.hasOverflowed) { if (_sibling == null) { _sibling = Create(transform.parent, name); } _sibling.GenerateFromDynamicMesh(mesh.overflow); } else { if (_sibling != null) { _sibling.DestroyVisual(); } _sibling = null; } }
public void Clear() { if (_mesh == null) { _mesh = new Mesh(); } _mesh.Clear(); if (_sibling != null) { _sibling.DestroyVisual(); } if (_colliderPart == null) { _colliderPart = ColliderPart.Create(transform.parent); } _colliderPart.Clear(); }
public void GenerateFromDynamicMesh(BuildRMesh overflow = null) { Debug.Log("VisualPart.cs GenerateFromDynamicMesh :" + overflow); HUtils.log(); if (_dynamicMesh == null) { _dynamicMesh = new BuildRMesh(DYNAMIC_MESH_NAME); } if (_mesh == null) { _mesh = new Mesh(); } if (_filter == null) { #if UNITY_EDITOR _filter = UnityEditor.Undo.AddComponent <MeshFilter>(gameObject); #else _filter = gameObject.AddComponent <MeshFilter>(); #endif } if (_renderer == null) { #if UNITY_EDITOR _renderer = UnityEditor.Undo.AddComponent <MeshRenderer>(gameObject); #else _renderer = gameObject.AddComponent <MeshRenderer>(); #endif } if (overflow != null) { _dynamicMesh = overflow; } _dynamicMesh.Build(_mesh); _filter.sharedMesh = _mesh; _renderer.sharedMaterials = _dynamicMesh.materials.ToArray(); if (_dynamicMesh.hasOverflowed) { if (_sibling == null) { _sibling = Create(transform.parent, name); } _sibling.GenerateFromDynamicMesh(_dynamicMesh.overflow); } else { if (_sibling != null) { _sibling.DestroyVisual(); } _sibling = null; } if (_colliderPart == null) { _colliderPart = ColliderPart.Create(transform.parent); } if (_colliderMesh == null) { _colliderMesh = new BuildRCollider(DYNAMIC_COLLIDER_NAME); } _colliderPart.GenerateFromColliderMesh(_colliderMesh); }