コード例 #1
0
        private void AddMeshColliderToDynamicColliders(GrabbitSettings settings, MeshCollider col, Mesh mesh,
                                                       GrabbitCollidersWatcher watcher)
        {
            var existingColliders = col.gameObject.GetComponents <MeshCollider>();

            //check for existing colliders to not have to duplicate the ones of other handlers, if subobjects are involved
            var existing = existingColliders.FirstOrDefault(_ => _.sharedMesh == mesh);

            var mc = existing ? existing : col.gameObject.AddComponent <MeshCollider>();

            if (!existing)
            {
                if (!settings.useLowQualityConvexCollidersOnSelection)
                {
                    mc.cookingOptions &= ~MeshColliderCookingOptions.UseFastMidphase;
                }
                mc.sharedMesh = mesh;
                mc.convex     = true;
            }

            AddedDynamicConvexColliders.Add(mc);
            watcher.AddedDynamicColliders.Add(mc);
        }
コード例 #2
0
        private void RegisterMeshes(Dictionary <GameObject, GrabbitCollidersWatcher> preExistingWatchers,
                                    Dictionary <GameObject, GrabbitCollidersWatcher> addedWatchers)
        {
            var bounds = new Bounds();

            var meshes = GetComponentsInChildren <MeshFilter>();
            int i      = 0;

            foreach (var mesh in meshes)
            {
                if (!mesh.sharedMesh || mesh.sharedMesh.triangles.Length <= 1)
                {
                    continue;
                }

                GrabbitCollidersWatcher addedColliders = mesh.gameObject.GetComponent <GrabbitCollidersWatcher>();

                bool hasRegisteredPreExisting = preExistingWatchers.ContainsKey(mesh.gameObject);
                bool hasAdded  = addedWatchers.ContainsKey(mesh.gameObject);
                bool justAdded = false;

                if (!hasRegisteredPreExisting && !hasAdded && !addedColliders)
                {
                    addedColliders = mesh.gameObject.AddComponent <GrabbitCollidersWatcher>();
                    AddedCollidersList.Add(addedColliders);
                    justAdded = true;
                }
                else
                {
                    if (hasAdded)
                    {
                        addedColliders = addedWatchers[mesh.gameObject];
                    }
                    else if (hasRegisteredPreExisting)
                    {
                        addedColliders = preExistingWatchers[mesh.gameObject];
                    }
                }

                if (hasRegisteredPreExisting)
                {
                    foreach (var meshCollider in addedColliders.AddedStaticColliders)
                    {
                        if (i == 0)
                        {
                            bounds = meshCollider.bounds;
                        }
                        else
                        {
                            bounds.Encapsulate(meshCollider.bounds);
                        }
                    }
                }
                else
                {
                    if (justAdded)
                    {
                        var col = mesh.gameObject.AddComponent <MeshCollider>();
                        col.sharedMesh = mesh.sharedMesh;

                        addedColliders.AddedStaticColliders.Add(col);
                        AddedStaticConvexColliders.Add(col);
                        if (i == 0)
                        {
                            bounds = col.bounds;
                        }
                        else
                        {
                            bounds.Encapsulate(col.bounds);
                        }
                    }
                    else
                    {
                        //then the watcher exists, but we haven't registered it just yet.
                        PreExistingColliders.AddRange(addedColliders.ExistingCollider);
                        AddedStaticConvexColliders.AddRange(addedColliders.AddedStaticColliders);

                        foreach (var collider in addedColliders.AddedStaticColliders)
                        {
                            if (i == 0)
                            {
                                bounds = collider.bounds;
                            }
                            else
                            {
                                bounds.Encapsulate(collider.bounds);
                            }
                        }

                        AddedDynamicConvexColliders.AddRange(addedColliders.AddedDynamicColliders);

                        preExistingWatchers.Add(mesh.gameObject, addedColliders);
                    }
                }

                i++;
            }

            Bounds = bounds;
        }