示例#1
0
        private static void GenerateAsset(GameObject selectedObject, Mesh srcMesh)
        {
            if (!AssetDatabase.IsValidFolder("Assets/Physics Hulls"))
            {
                AssetDatabase.CreateFolder("Assets", "Physics Hulls");
            }

            string path = "Assets/Physics Hulls/";

            // Find suitable asset names
            string paintAssetName, hullAssetName;

            CreateAssetPaths(path, selectedObject.name, out paintAssetName, out hullAssetName);

            // Painting asset
            PaintingData painting = ScriptableObject.CreateInstance <PaintingData>();

            painting.sourceMesh = srcMesh;
            AssetDatabase.CreateAsset(painting, paintAssetName);

            // Mesh asset
            HullData hulls = ScriptableObject.CreateInstance <HullData>();

            AssetDatabase.CreateAsset(hulls, hullAssetName);

            // Connect the painting data to the hull data

            painting.hullData = hulls;

            // Get the hull painter (or create one if it doesn't exist)

            HullPainter selectedPainter = selectedObject.GetComponent <HullPainter>();

            if (selectedPainter == null)
            {
                selectedPainter = selectedObject.AddComponent <HullPainter>();
            }

            // Point the painter at the asset data

            selectedPainter.paintingData = painting;
            selectedPainter.hullData     = hulls;

            // Start with a single empty hull
            selectedPainter.paintingData.AddHull(HullType.ConvexHull, null, false);

            EditorUtility.SetDirty(painting);
            EditorUtility.SetDirty(hulls);

            // Ping the painting asset in the ui (can only ping one object at once, so do the more important one)
            EditorGUIUtility.PingObject(painting);

            EditorWindow.GetWindow(typeof(HullPainterWindow));
        }
示例#2
0
        public static void DrawGenerateOrReconnectGui(GameObject selectedObject, Mesh srcMesh)
        {
            if (GUILayout.Button("Generate Asset"))
            {
                GenerateAsset(selectedObject, srcMesh);
            }

            GUILayout.Label("Or reconnect to existing asset:");

            PaintingData newPaintingData = (PaintingData)EditorGUILayout.ObjectField(null, typeof(PaintingData), false);

            if (newPaintingData != null)
            {
                Reconnect(selectedObject, newPaintingData);
            }
        }
示例#3
0
        public static void Reconnect(GameObject selectedObject, PaintingData newPaintingData)
        {
            Debug.Log("Reconnect " + selectedObject.name + " to " + newPaintingData.name);

            // Get the hull painter (or create one if it doesn't exist)

            HullPainter hullPainter = selectedObject.GetComponent <HullPainter>();

            if (hullPainter == null)
            {
                hullPainter = selectedObject.AddComponent <HullPainter>();
            }

            // Point the hull painter at the assets

            hullPainter.paintingData = newPaintingData;
            hullPainter.hullData     = newPaintingData.hullData;

            EditorWindow.GetWindow(typeof(HullPainterWindow)).Repaint();
        }
示例#4
0
        private void GenerateColliders()
        {
            HullPainter currentHullPainter = sceneManipulator.GetCurrentHullPainter();

            if (currentHullPainter == null)
            {
                return;
            }

            Undo.RegisterCompleteObjectUndo(currentHullPainter.gameObject, "Generate Colliders");

            // Fetch the data assets

            PaintingData paintingData = currentHullPainter.paintingData;
            HullData     hullData     = currentHullPainter.hullData;

            string hullAssetPath = AssetDatabase.GetAssetPath(hullData);

            // Create / update the hull meshes

            foreach (Hull hull in paintingData.hulls)
            {
                paintingData.GenerateCollisionMesh(hull, sceneManipulator.GetTargetVertices(), sceneManipulator.GetTargetTriangles());
            }

            // Sync the in-memory hull meshes with the asset meshes in hullAssetPath

            List <Mesh> existingMeshes = GetAllMeshesInAsset(hullAssetPath);

            foreach (Mesh existing in existingMeshes)
            {
                if (!paintingData.ContainsMesh(existing))
                {
                    GameObject.DestroyImmediate(existing, true);
                }
            }

            foreach (Hull hull in paintingData.hulls)
            {
                if (hull.collisionMesh != null)
                {
                    if (!existingMeshes.Contains(hull.collisionMesh))
                    {
                        AssetDatabase.AddObjectToAsset(hull.collisionMesh, hullAssetPath);
                    }
                }
                if (hull.faceCollisionMesh != null)
                {
                    if (existingMeshes.Contains(hull.faceCollisionMesh))
                    {
                        AssetDatabase.AddObjectToAsset(hull.faceCollisionMesh, hullAssetPath);
                    }
                }
            }

            EditorUtility.SetDirty(hullData);

            AssetDatabase.SaveAssets();

            // Add collider components to the target object

            currentHullPainter.CreateColliderComponents();
        }