Esempio n. 1
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            SerializedProperty mesh = serializedObject.FindProperty("m_Mesh");

            if (mesh != null)
            {
                EditorGUILayout.PropertyField(mesh);
            }

            Mesh m = (Mesh)mesh.objectReferenceValue;

            if (m != null)
            {
                string        dontcare = null;
                z_ModelSource source   = z_EditorUtility.GetMeshGUID(m, ref dontcare);

                if (source == z_ModelSource.Scene &&
                    !(z_ReflectionUtil.IsProBuilderObject(((MeshFilter)serializedObject.targetObject).gameObject)))
                {
                    if (GUILayout.Button(new GUIContent("Save to Asset", "Save this instance mesh to an Asset so that you can use it as a prefab.")))
                    {
                        z_EditorUtility.SaveMeshAsset(m);
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
Esempio n. 2
0
        /**
         *	Save any modifications to the z_EditableObject.  If the mesh is a scene mesh or imported mesh, it
         *	will be saved to a new asset.  If the mesh was originally an asset mesh, the asset is overwritten.
         *  \return true if save was successful, false if user-canceled or otherwise failed.
         */
        public static bool SaveMeshAsset(Mesh mesh, MeshFilter meshFilter = null, SkinnedMeshRenderer skinnedMeshRenderer = null)
        {
            string save_path = DO_NOT_SAVE;

            string        guid   = null;
            z_ModelSource source = GetMeshGUID(mesh, ref guid);

            switch (source)
            {
            case z_ModelSource.Asset:

                int saveChanges = EditorUtility.DisplayDialogComplex(
                    "Save Changes",
                    "Save changes to edited mesh?",
                    "Save",                                                     // DIALOG_OK
                    "Cancel",                                                   // DIALOG_CANCEL
                    "Save As");                                                 // DIALOG_ALT

                if (saveChanges == DIALOG_OK)
                {
                    save_path = AssetDatabase.GetAssetPath(mesh);
                }
                else if (saveChanges == DIALOG_ALT)
                {
                    save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to");
                }
                else
                {
                    return(false);
                }

                break;

            case z_ModelSource.Imported:
            case z_ModelSource.Scene:
            default:
                // @todo make sure path is in Assets/
                save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to");
                break;
            }

            if (!save_path.Equals(DO_NOT_SAVE) && !string.IsNullOrEmpty(save_path))
            {
                Object existing = AssetDatabase.LoadMainAssetAtPath(save_path);

                if (existing != null && existing is Mesh)
                {
                    // save over an existing mesh asset
                    z_MeshUtility.Copy((Mesh)existing, mesh);
                    GameObject.DestroyImmediate(mesh);
                }
                else
                {
                    AssetDatabase.CreateAsset(mesh, save_path);
                }

                AssetDatabase.Refresh();

                if (meshFilter != null)
                {
                    meshFilter.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(save_path, typeof(Mesh));
                }
                else if (skinnedMeshRenderer != null)
                {
                    skinnedMeshRenderer.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(save_path, typeof(Mesh));
                }

                return(true);
            }

            // Save was canceled
            return(false);
        }