Ejemplo n.º 1
0
        public override void UpdateView(Event e, Rect editorViewRect)
        {
            //Update base derived class
            base.UpdateView(e, editorViewRect);

            if (isLoaded)
            {
                //Draw inspector
                if (curWindow.textureAtlas != null && curWindow.textureAtlas.selectedTextureQuad != null)
                {
                    //Deselect GUI elements when we are focusing on a new quad
                    if (lastSelectedQuad != curWindow.textureAtlas.selectedTextureQuad)
                    {
                        lastSelectedQuad = curWindow.textureAtlas.selectedTextureQuad;
                        GUI.FocusControl(null);
                    }

                    GUILayout.BeginArea(editorViewRect, EditorStyles.helpBox);
                    GUILayout.BeginVertical(GUILayout.ExpandWidth(true));

                    GUILayout.Label("Quad Name");
                    curWindow.textureAtlas.selectedTextureQuad.name = EditorGUILayout.TextField(curWindow.textureAtlas.selectedTextureQuad.name);

                    GUILayout.Space(MA_TextureAtlasserProUtils.VIEW_OFFSET / 2);

                    //Textures
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Textures", GUILayout.ExpandWidth(true));
                    if (GUILayout.Button(MA_TextureAtlasserProGuiLoader.editGC, EditorStyles.miniButton, GUILayout.Width(36), GUILayout.Height(15)))
                    {
                        isEditing = !isEditing;
                    }
                    GUILayout.EndHorizontal();
                    if (curWindow.textureAtlas.textureGroupRegistration == null || curWindow.textureAtlas.textureGroupRegistration.Count == 0)
                    {
                        if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
                        {
                            MA_TextureAtlasserProUtils.CreateTextureGroup(curWindow.textureAtlas, "New TextureGroup");
                        }
                    }
                    for (int i = 0; i < curWindow.textureAtlas.textureGroupRegistration.Count; i++)
                    {
                        if (isEditing)
                        {
                            curWindow.textureAtlas.textureGroupRegistration[i].name = curWindow.textureAtlas.selectedTextureQuad.textureGroups[i].name = EditorGUILayout.TextField(curWindow.textureAtlas.textureGroupRegistration[i].name);
                        }
                        else
                        {
                            GUILayout.Label(curWindow.textureAtlas.textureGroupRegistration[i].name);
                        }
                        GUILayout.BeginHorizontal();
                        curWindow.textureAtlas.selectedTextureQuad.textureGroups[i].texture = (Texture)EditorGUILayout.ObjectField(curWindow.textureAtlas.selectedTextureQuad.textureGroups[i].texture, typeof(Texture), false);
                        if (isEditing && GUILayout.Button("-", EditorStyles.miniButtonLeft, GUILayout.ExpandWidth(false)))
                        {
                            MA_TextureAtlasserProUtils.RemoveTextureGroup(curWindow.textureAtlas, i);
                        }
                        if (isEditing && GUILayout.Button("+", EditorStyles.miniButtonRight, GUILayout.ExpandWidth(false)))
                        {
                            MA_TextureAtlasserProUtils.CreateTextureGroup(curWindow.textureAtlas, "New TextureGroup");
                        }
                        GUILayout.EndHorizontal();
                    }

                    GUILayout.Space(MA_TextureAtlasserProUtils.VIEW_OFFSET / 2);

                    //Models
                    GUILayout.Label("Models");

                    SerializedObject serializedObject = new SerializedObject(curWindow.textureAtlas.selectedTextureQuad);
                    serializedObject.Update();

                    if (curWindow.textureAtlas.selectedTextureQuad.modelGroups != null)
                    {
                        SerializedProperty modelGroupsSP = serializedObject.FindProperty("modelGroups");

                        for (int i = 0; i < curWindow.textureAtlas.selectedTextureQuad.modelGroups.Count; i++)
                        {
                            using (new GUILayout.VerticalScope(EditorStyles.helpBox))
                            {
                                using (new GUILayout.HorizontalScope())
                                {
                                    curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].name = EditorGUILayout.TextField(curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].name);
                                    if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
                                    {
                                        curWindow.textureAtlas.selectedTextureQuad.modelGroups.RemoveAt(i);
                                        break;
                                    }
                                }

                                SerializedProperty meshesSP = modelGroupsSP.GetArrayElementAtIndex(i).FindPropertyRelative("meshes");
                                EditorGUILayout.PropertyField(meshesSP, false, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(editorViewRect.width * 0.5f));

                                if (meshesSP.isExpanded)
                                {
                                    for (int j = 0; j < curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].meshes.Count; j++)
                                    {
                                        using (new GUILayout.HorizontalScope())
                                        {
                                            curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].meshes[j] = (Mesh)EditorGUILayout.ObjectField(curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].meshes[j], typeof(Mesh), false);
                                            if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
                                            {
                                                curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].meshes.RemoveAt(j);
                                                break;
                                            }
                                        }
                                    }
                                }

                                if (GUILayout.Button("+", EditorStyles.miniButton))
                                {
                                    curWindow.textureAtlas.selectedTextureQuad.modelGroups[i].meshes.Add(null);
                                }
                            }
                        }

                        if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
                        {
                            curWindow.textureAtlas.selectedTextureQuad.modelGroups.Add(new MA_ModelGroup()
                            {
                                name = MA_StringUtils.RandomAlphabetString(6)
                            });
                        }
                    }
                    else
                    {
                        curWindow.textureAtlas.selectedTextureQuad.modelGroups = new List <MA_ModelGroup>();
                    }

                    serializedObject.ApplyModifiedProperties();

                    GUILayout.Space(MA_TextureAtlasserProUtils.VIEW_OFFSET / 2);

                    //x, y, w, h.
                    GUILayout.FlexibleSpace();
                    if (!MA_TextureAtlasserProUtils.IsPowerOfTwo((int)curWindow.textureAtlas.selectedTextureQuad.guiRect.width) || !MA_TextureAtlasserProUtils.IsPowerOfTwo((int)curWindow.textureAtlas.selectedTextureQuad.guiRect.height))
                    {
                        labelStyle.normal.textColor = Color.red;
                    }
                    else
                    {
                        labelStyle.normal.textColor = GUI.skin.label.normal.textColor;
                    }

                    GUILayout.Label("x " + curWindow.textureAtlas.selectedTextureQuad.guiRect.x.ToString() + ", y " + curWindow.textureAtlas.selectedTextureQuad.guiRect.y.ToString());
                    GUILayout.Label("w " + curWindow.textureAtlas.selectedTextureQuad.guiRect.width.ToString() + ", h " + curWindow.textureAtlas.selectedTextureQuad.guiRect.height.ToString(), labelStyle);

                    GUILayout.EndVertical();
                    GUILayout.EndArea();
                }
            }

            if (curWindow.textureAtlas != null && curWindow.textureAtlas.selectedTextureQuad != null)
            {
                ProcessEvents(e, editorViewRect);
            }
        }
Ejemplo n.º 2
0
        private static string[] ExportAtlasUnityMeshPrefab(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string materialPath = null, string savePath = EXPORT_ASSET_PATH)
        {
            if (atlas == null || atlas.textureQuads == null)
            {
                return(null);
            }

            List <string> assetPaths = new List <string>();

            //Directories.
            string savePathPrefab = savePath + atlas.name + "/";
            string savePathMeshes = savePathPrefab + "Meshes/";

            CreateFolder(savePathPrefab);
            CreateFolder(savePathMeshes);

            foreach (MA_TextureAtlasserProQuad quad in atlas.textureQuads)
            {
                foreach (MA_ModelGroup mg in quad.modelGroups)
                {
                    //Validate name.
                    if (string.IsNullOrEmpty(mg.name) || string.IsNullOrWhiteSpace(mg.name))
                    {
                        mg.name = MA_StringUtils.RandomAlphabetString(6);
                        Debug.LogWarning("No valid model name assigned!");
                    }

                    //Create new prefab asset.
                    string     newPrefabPath = MA_PrefabUtils.CreatePrefab(mg.name, savePathPrefab);
                    GameObject newPrefab     = AssetDatabase.LoadAssetAtPath <GameObject>(newPrefabPath);

                    foreach (Mesh m in mg.meshes)
                    {
                        if (m != null)
                        {
                            //Validate name.
                            if (string.IsNullOrEmpty(m.name) || string.IsNullOrWhiteSpace(m.name))
                            {
                                m.name = MA_StringUtils.RandomAlphabetString(6);
                                Debug.LogWarning("No valid mesh name assigned!");
                            }

                            //Create new mesh.
                            //Duplicate it from the current one.
                            Mesh newMesh = MA_MeshUtils.MA_DuplicateMesh(m);
                            //Remap UV's.
                            newMesh = MA_MeshUtils.MA_UVReMap(newMesh, atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap);
                            //Set name.
                            newMesh.name = string.Format("{0}_{1}", mg.name, m.name);
                            //Save mesh.
                            string savedMeshPath = MA_MeshUtils.MA_SaveMeshAsset(newMesh, savePathMeshes);

                            //Load mesh.
                            Mesh savedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(savedMeshPath);
                            //Load material.
                            Material savedMaterial = AssetDatabase.LoadAssetAtPath <Material>(materialPath);

                            //Create gameObject.
                            GameObject newGameObject = new GameObject(m.name);
                            //Add mesh filter.
                            MeshFilter mf = newGameObject.AddComponent <MeshFilter>();
                            mf.mesh = savedMesh;
                            //Add mesh renderer.
                            MeshRenderer mr = newGameObject.AddComponent <MeshRenderer>();
                            mr.material = savedMaterial;

                            //Add to parent gameObject (prefab).
                            MA_PrefabUtils.AddChild(newPrefab, newGameObject);
                            Object.DestroyImmediate(newGameObject);
                        }
                    }

                    assetPaths.Add(newPrefabPath);
                }
            }

            return(assetPaths.ToArray());
        }