예제 #1
0
    void CreateCacheImage()
    {
        MegaCacheOBJ mod = (MegaCacheOBJ)target;

        if (mod.cacheimage)
        {
            mod.DestroyImage();
        }

        MegaCacheImage img = CreateInstance <MegaCacheImage>();

        img.maxv  = 0;
        img.maxsm = 0;

        for (int i = 0; i < mod.meshes.Count; i++)
        {
            if (mod.meshes[i].vertexCount > img.maxv)
            {
                img.maxv = mod.meshes[i].vertexCount;
            }

            int sub = mod.meshes[i].subMeshCount;
            if (sub > img.maxsm)
            {
                img.maxsm = sub;
            }
        }

        img.smfc = new int[img.maxsm];

        for (int i = 0; i < mod.meshes.Count; i++)
        {
            for (int s = 0; s < mod.meshes[i].subMeshCount; s++)
            {
                int len = mod.meshes[i].GetTriangles(s).Length;

                if (len > img.smfc[s])
                {
                    img.smfc[s] = len;
                }
            }
        }

        for (int i = 0; i < mod.meshes.Count; i++)
        {
            Mesh ms = mod.meshes[i];

            MegaCacheImageFrame frame = MegaCacheImage.CreateImageFrame(ms);
            img.frames.Add(frame);
        }

        img.CalcMemory();
        mod.cacheimage = img;
    }
예제 #2
0
    public override void OnInspectorGUI()
    {
        bool undo = true;

        MegaCacheOBJ mod = (MegaCacheOBJ)target;

        serializedObject.Update();

#if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019 && !UNITY_2020
        EditorGUIUtility.LookLikeControls();
#endif

        EditorGUILayout.BeginVertical("box");
        mod.showdataimport = EditorGUILayout.Foldout(mod.showdataimport, "Data Import");

        if (mod.showdataimport)
        {
            EditorGUILayout.PropertyField(_prop_firstframe, new GUIContent("First"));
            EditorGUILayout.PropertyField(_prop_lastframe, new GUIContent("Last"));
            EditorGUILayout.PropertyField(_prop_skip, new GUIContent("Skip"));

            //int val = 0;
            //mod.decformat = EditorGUILayout.IntSlider("Format name" + mod.namesplit + val.ToString("D" + mod.decformat) + ".obj", mod.decformat, 1, 6);
            //mod.namesplit = EditorGUILayout.TextField("Name Split Char", mod.namesplit);
            EditorGUILayout.PropertyField(_prop_scale, new GUIContent("Import Scale"));
            EditorGUILayout.PropertyField(_prop_adjustcords, new GUIContent("Adjust Coords"));
            EditorGUILayout.PropertyField(_prop_buildtangents, new GUIContent("Build Tangents"));
            EditorGUILayout.PropertyField(_prop_updatecollider, new GUIContent("Update Collider"));
            EditorGUILayout.PropertyField(_prop_loadmtls, new GUIContent("Load Materials"));
            EditorGUILayout.PropertyField(_prop_recalcnormals, new GUIContent("Recalculate Normals"));

            if (GUILayout.Button("Load Frames"))
            {
                string file = EditorUtility.OpenFilePanel("OBJ File", mod.lastpath, "obj");

                if (file != null && file.Length > 1)
                {
                    mod.lastpath = file;
                    LoadOBJ(mod, file, mod.firstframe, mod.lastframe, mod.skip);
                }
            }

            if (mod.meshes.Count > 0)
            {
                if (GUILayout.Button("Clear Stored Meshes"))
                {
                    mod.DestroyMeshes();
                }
            }
        }
        EditorGUILayout.EndVertical();

        mod.showdata = EditorGUILayout.Foldout(mod.showdata, "Data");

        if (mod.showdata)
        {
            MegaCacheData src = (MegaCacheData)EditorGUILayout.EnumPopup("Data Source", mod.datasource);

            if (src != mod.datasource)
            {
                mod.ChangeSource(src);
            }

            switch (mod.datasource)
            {
            case MegaCacheData.Mesh:
                if (mod.meshes.Count > 0)
                {
                    EditorGUILayout.BeginVertical("box");
                    EditorGUILayout.PropertyField(_prop_saveuvs, new GUIContent("Save Uvs"));
                    EditorGUILayout.PropertyField(_prop_savenormals, new GUIContent("Save Normals"));
                    EditorGUILayout.PropertyField(_prop_savetangents, new GUIContent("Save Tangents"));
                    EditorGUILayout.PropertyField(_prop_optimize, new GUIContent("Optimize Data"));

                    if (GUILayout.Button("Save MegaCache File"))
                    {
                        string file = EditorUtility.SaveFilePanel("MegaCache File", mod.lastpath, mod.name, "mgc");

                        if (file != null && file.Length > 1)
                        {
                            mod.CloseCache();
                            CreateCacheFile(file);
                            if (mod.cachefile.Length == 0)
                            {
                                mod.cachefile = file;
                            }
                        }
                    }

                    if (GUILayout.Button("Create Image"))
                    {
                        CreateCacheImage();
                    }

                    EditorGUILayout.EndVertical();
                }
                break;

            case MegaCacheData.File:
                EditorGUILayout.BeginVertical("box");
                EditorGUILayout.TextArea("Cache File: " + mod.cachefile);

                if (GUILayout.Button("Select MegaCache File"))
                {
                    string file = EditorUtility.OpenFilePanel("MegaCache File", mod.lastpath, "mgc");

                    if (file != null && file.Length > 1)
                    {
                        mod.CloseCache();
                        mod.cachefile = file;
                        mod.update    = true;
                        mod.OpenCache(mod.cachefile);
                    }
                }

                EditorGUILayout.PropertyField(_prop_runtimefolder, new GUIContent("Runtime Folder"));

                if (mod.cachefile.Length > 0)
                {
                    if (GUILayout.Button("Create Image From Cache"))
                    {
                        bool doit = true;
                        if (mod.cacheimage)
                        {
                            if (!EditorUtility.DisplayDialog("Add to or Replace", "Image already loaded do you want to Replace?", "Yes", "No"))
                            {
                                doit = false;
                            }
                        }

                        if (doit)
                        {
                            mod.CreateImageFromCacheFile();
                        }
                    }
                }

                EditorGUILayout.EndVertical();
                break;

            case MegaCacheData.Image:
                if (mod.cacheimage)
                {
                    EditorGUILayout.BeginVertical("box");
#if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
                    mod.cacheimage.threadupdate = EditorGUILayout.Toggle("Preload", mod.cacheimage.threadupdate);
#endif
                    if (GUILayout.Button("Delete Image"))
                    {
                        mod.DestroyImage();                                     // = null;
                    }
                    EditorGUILayout.EndVertical();
                }
                break;
            }

            string info = "";

            info += "Frame Verts: " + mod.framevertcount + "\nFrame Tris: " + (mod.frametricount / 3);

            if (mod.datasource == MegaCacheData.Image)
            {
                if (mod.cacheimage)
                {
                    info += "\nMemory: " + mod.cacheimage.memoryuse / (1024 * 1024) + "MB";
                }
                else
                {
                    info += "\nNo Image File";
                }
            }

            EditorGUILayout.HelpBox(info, MessageType.None);
        }

        mod.showanimation = EditorGUILayout.Foldout(mod.showanimation, "Animation");

        if (mod.showanimation)
        {
            EditorGUILayout.BeginVertical("box");

            int fc = 0;
            switch (mod.datasource)
            {
            case MegaCacheData.Mesh: fc = mod.meshes.Count - 1; break;

            case MegaCacheData.File: fc = mod.framecount - 1; break;

            case MegaCacheData.Image:
                if (mod.cacheimage && mod.cacheimage.frames != null)
                {
                    fc = mod.cacheimage.frames.Count - 1;
                }
                break;
            }

            if (fc > 0)
            {
                EditorGUILayout.IntSlider(_prop_frame, 0, fc);
            }

            mod.animate = EditorGUILayout.BeginToggleGroup("Animate", mod.animate);
            bool timechange = GUI.changed;
            EditorGUILayout.PropertyField(_prop_time, new GUIContent("Time"));
            if (GUI.changed && !timechange)
            {
                undo = false;
            }

            EditorGUILayout.PropertyField(_prop_fps, new GUIContent("Fps"));
            EditorGUILayout.PropertyField(_prop_speed, new GUIContent("Speed"));
            EditorGUILayout.PropertyField(_prop_loopmode, new GUIContent("Loop Mode"));

            EditorGUILayout.EndToggleGroup();
            EditorGUILayout.EndVertical();
        }

        mod.showextras = EditorGUILayout.Foldout(mod.showextras, "Extra Options");

        if (mod.showextras)
        {
            mod.shownormals = EditorGUILayout.BeginToggleGroup("Show Normals", mod.shownormals);
            mod.normallen   = EditorGUILayout.FloatField("Normal Length", mod.normallen);
            EditorGUILayout.EndToggleGroup();
        }

        if (GUI.changed)
        {
            if (undo)
            {
                serializedObject.ApplyModifiedProperties();
            }
            else
            {
#if UNITY_5_3_OR_NEWER || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
                serializedObject.ApplyModifiedPropertiesWithoutUndo();
#else
                serializedObject.ApplyModifiedProperties();
#endif
            }
            EditorUtility.SetDirty(target);
        }
    }