Пример #1
0
        // DO THE ACTUAL SAVE
        public static void saveParametricObject(AXParametricObject po, bool withInputSubparts, string filepathname)
        {
            //Debug.Log(filepathname);
            //EditorUtility.DisplayDialog("Archimatix Library", "Saving to Library: This may take a few moments.", "Ok");

            po.readIntoLibraryFromRelativeAXOBJPath = ArchimatixUtils.getRelativeFilePath(filepathname);

            Library.recentSaveFolder = System.IO.Path.GetDirectoryName(filepathname);



            // If this head po has a grouperKey, lose it!
            // This is because when it is later instantiated,
            // that grouper may not exist or may not be the desired place for this.

            po.grouperKey = null;



            // gather relations as you go...
            List <AXRelation> rels = new List <AXRelation>();

            // BUILD JSON STRING
            StringBuilder sb = new StringBuilder();

            sb.Append("{");
            sb.Append("\"parametric_objects\":[");
            sb.Append(JSONSerializersAX.ParametricObjectAsJSON(po));

            // gather rels
            foreach (AXParameter p in po.parameters)
            {
                foreach (AXRelation rr in p.relations)
                {
                    if (!rels.Contains(rr))
                    {
                        rels.Add(rr);
                    }
                }
            }



            // SUB NODES

            if (withInputSubparts)
            {
                // GATHER SUB_NODES
                //Debug.Log("Start gather");
                po.gatherSubnodes();
                //Debug.Log("End gather");

                foreach (AXParametricObject spo in po.subnodes)
                {
                    //Debug.Log("-- " + spo.Name);
                    sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo));

                    // gather rels
                    foreach (AXParameter p in spo.parameters)
                    {
                        foreach (AXRelation rr in p.relations)
                        {
                            if (!rels.Contains(rr))
                            {
                                rels.Add(rr);
                            }
                        }
                    }
                }
            }
            sb.Append("]");



            // add relations to json
            string thecomma;

            // RELATIONS
            if (rels != null && rels.Count > 0)
            {
                sb.Append(", \"relations\": [");                                // begin parametric_objects

                thecomma = "";
                foreach (AXRelation rr in rels)
                {
                    sb.Append(thecomma + rr.asJSON());
                    thecomma = ", ";
                }
                sb.Append("]");
            }



            sb.Append("}");



            // *** SAVE AS ASSET ***



            // Since we have added the newItem to the live library,
            // generating this new file will not force a recreation of the library from the raw JSON file.
            File.WriteAllText(filepathname, sb.ToString());



            // THUMBNAIL TO PNG



            if (po.is2D())
            {
                Library.cache2DThumbnail(po);
            }
            else
            {
                Thumbnail.BeginRender();
                Thumbnail.render(po, true);
                Thumbnail.EndRender();
            }
            //string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".png");
            string filename = System.IO.Path.GetFileNameWithoutExtension(filepathname);

            string prefix         = (po.is2D()) ? "zz-AX-2DLib-" : "zz-AX-3DLib-";
            string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".jpg");

            thumb_filename = thumb_filename.Replace(filename, prefix + filename);



            byte[] bytes = po.thumbnail.EncodeToJPG();
            //File.WriteAllBytes(libraryFolder + "data/"+ po.Name + ".png", bytes);
            File.WriteAllBytes(thumb_filename, bytes);

            //AssetDatabase.Refresh();

            string thumbnailRelativePath = ArchimatixUtils.getRelativeFilePath(thumb_filename);

            //Debug.Log(path);
            AssetDatabase.ImportAsset(thumbnailRelativePath);
            TextureImporter importer = AssetImporter.GetAtPath(thumbnailRelativePath) as TextureImporter;

            if (importer != null)
            {
                importer.textureType    = TextureImporterType.GUI;
                importer.maxTextureSize = 256;



                                #if UNITY_5_5_OR_NEWER
                importer.textureCompression = TextureImporterCompression.Uncompressed;
                                #else
                importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
                                #endif
            }

            AssetDatabase.WriteImportSettingsIfDirty(thumbnailRelativePath);


            // Save the recent path to prefs
            EditorPrefs.SetString("LastLibraryPath", System.IO.Path.GetDirectoryName(filepathname));


            EditorUtility.DisplayDialog("Archimatix Library", "ParametricObject saved to " + System.IO.Path.GetDirectoryName(thumbnailRelativePath) + " as " + filename, "Great, thanks!");

            Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(thumbnailRelativePath, typeof(Texture2D));



            LibraryItem newItem = new LibraryItem(po);
            newItem.icon = tex;
            ArchimatixEngine.library.addLibraryItemToList(newItem);
            ArchimatixEngine.library.sortLibraryItems();


            ArchimatixEngine.saveLibrary();



            AssetDatabase.Refresh();

            //Debug.Log("yo 2");
            //ArchimatixEngine.library.readLibraryFromFiles();
        }