Example #1
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null) {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent<SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck()) {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null) {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon")) {
                Rect r = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh")) {
                CreateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Create Mesh from Sprite")) {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider")) {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent<PolygonCollider2D>();
                if (polygonCollider == null) {
                    polygonCollider = spriteRenderer.gameObject.AddComponent<PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Create and Edit a Custom Polygon", EditorStyles.boldLabel);
            handleScale = EditorGUILayout.FloatField("Handle Size", handleScale);
            handleColor = EditorGUILayout.ColorField("Handle Color", handleColor);
            polyColor = EditorGUILayout.ColorField("Poly Color", polyColor);

            EditorGUILayout.Separator();

            GUILayout.Label("Ctrl + Click to Add Point, Shift + Click to Add Mid Point, Alt + Click to Remove Points", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create and Edit Polygon")) {
                CreatePolygon();
                EditorUtility.SetDirty(this);
            }

            EditorUtility.SetSelectedRenderState(spriteRenderer, EditorSelectedRenderState.Hidden);

            if (GUILayout.Button("Update Custom Mesh")) {
                if (spriteRenderer != null) UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Save Custom Mesh")) {
                if (spriteRenderer != null) SaveMesh();
            }

            if (GUI.changed) {
                UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Subdivide Mesh", EditorStyles.boldLabel);

            string[] subdivide = { "0", "1", "2", "3" };

            divisions = EditorGUILayout.Popup("Subdivisions", divisions, subdivide);

            if (GUILayout.Button("Subdivide Mesh")) {
                if (spriteRenderer != null && mesh != null) SubdivideMesh(divisions);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Load Custom Mesh to Edit", EditorStyles.boldLabel);

            GUILayout.Label("Adding or Deleting points Re-Triangulates Mesh", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            customLoadMesh = (Mesh)EditorGUILayout.ObjectField(customLoadMesh, typeof(Mesh), true);

            if (GUILayout.Button("Load Custom Mesh")) {
                if (spriteRenderer != null && customLoadMesh != null) {
                    LoadMesh(customLoadMesh);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Combine Meshes", EditorStyles.boldLabel);

            GUILayout.Label("Avoid Combining Complex Meshes, results will vary", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            combineMesh = (Mesh)EditorGUILayout.ObjectField(combineMesh, typeof(Mesh), true);

            if (GUILayout.Button("Combine Meshes")) {
                if (spriteRenderer != null) CombineMesh();
            }
        }
    }
Example #2
0
    public void OnGUI() {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null) {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent<SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck()) {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null) {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon")) {
                Rect r = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh")) {
                CreateMesh();
            }

            if (GUILayout.Button("Create Mesh from Sprite")) {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider")) {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent<PolygonCollider2D>();
                if (polygonCollider == null) {
                    polygonCollider = spriteRenderer.gameObject.AddComponent<PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer = spriteRenderer;
                polygonMesh.CreatePolygonMesh();

            }
        }
    }
Example #3
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null)
        {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent <SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck())
        {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null)
        {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon"))
            {
                Rect      r   = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap   bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh"))
            {
                CreateMesh();
            }

            if (GUILayout.Button("Create Mesh from Sprite"))
            {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider"))
            {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent <PolygonCollider2D>();
                if (polygonCollider == null)
                {
                    polygonCollider = spriteRenderer.gameObject.AddComponent <PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer  = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
            }
        }
    }
Example #4
0
    public void OnGUI()
    {
        GUILayout.Label("Sprite", EditorStyles.boldLabel);

        EditorGUI.BeginChangeCheck();
        spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField(spriteRenderer, typeof(SpriteRenderer), true);
        if (Selection.activeGameObject != null)
        {
            GameObject o = Selection.activeGameObject;
            spriteRenderer = o.GetComponent <SpriteRenderer>();
        }
        if (EditorGUI.EndChangeCheck())
        {
            polygon = new Vector2[0];
        }

        if (spriteRenderer != null)
        {
            EditorGUILayout.Separator();

            GUILayout.Label("Simplify", EditorStyles.boldLabel);

            simplify = EditorGUILayout.FloatField("Vertex Dist.", simplify);

            if (GUILayout.Button("Generate Polygon"))
            {
                Rect      r   = spriteRenderer.sprite.rect;
                Texture2D tex = spriteRenderer.sprite.texture;
                IBitmap   bmp = ArrayBitmap.CreateFromTexture(tex, new Rect(r.x, r.y, r.width, r.height));
                polygon = BitmapHelper.CreateFromBitmap(bmp);
                polygon = SimplifyTools.DouglasPeuckerSimplify(new Vertices(polygon), simplify).ToArray();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Vertices: " + polygon.Length);

            EditorGUILayout.Separator();

            if (polygon.Length > 0 && GUILayout.Button("Create Mesh"))
            {
                CreateMesh();
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Create Mesh from Sprite"))
            {
                SpriteMesh spriteMesh = new SpriteMesh();
                spriteMesh.spriteRenderer = spriteRenderer;
                spriteMesh.CreateSpriteMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create Mesh from Polygon2D Collider"))
            {
                PolygonCollider2D polygonCollider = spriteRenderer.GetComponent <PolygonCollider2D>();
                if (polygonCollider == null)
                {
                    polygonCollider = spriteRenderer.gameObject.AddComponent <PolygonCollider2D>();
                }

                PolygonMesh polygonMesh = new PolygonMesh();
                polygonMesh.polygonCollider = polygonCollider;
                polygonMesh.spriteRenderer  = spriteRenderer;
                polygonMesh.CreatePolygonMesh();
                EditorUtility.SetDirty(this);
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Create and Edit a Custom Polygon", EditorStyles.boldLabel);
            handleScale = EditorGUILayout.FloatField("Handle Size", handleScale);
            handleColor = EditorGUILayout.ColorField("Handle Color", handleColor);
            polyColor   = EditorGUILayout.ColorField("Poly Color", polyColor);

            EditorGUILayout.Separator();

            GUILayout.Label("Ctrl + Click to Add Point, Shift + Click to Add Mid Point, Alt + Click to Remove Points", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();

            if (GUILayout.Button("Create and Edit Polygon"))
            {
                CreatePolygon();
                EditorUtility.SetDirty(this);
            }

            EditorUtility.SetSelectedWireframeHidden(spriteRenderer, true);

            if (GUILayout.Button("Update Custom Mesh"))
            {
                if (spriteRenderer != null)
                {
                    UpdateMesh();
                }
                EditorUtility.SetDirty(this);
            }

            if (GUILayout.Button("Save Custom Mesh"))
            {
                if (spriteRenderer != null)
                {
                    SaveMesh();
                }
            }

            if (GUI.changed)
            {
                UpdateMesh();
                EditorUtility.SetDirty(this);
            }

            GUILayout.Label("Subdivide Mesh", EditorStyles.boldLabel);

            string[] subdivide = { "0", "1", "2", "3" };

            divisions = EditorGUILayout.Popup("Subdivisions", divisions, subdivide);

            if (GUILayout.Button("Subdivide Mesh"))
            {
                if (spriteRenderer != null && mesh != null)
                {
                    SubdivideMesh(divisions);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Load Custom Mesh to Edit", EditorStyles.boldLabel);

            GUILayout.Label("Adding or Deleting points Re-Triangulates Mesh", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            customLoadMesh = (Mesh)EditorGUILayout.ObjectField(customLoadMesh, typeof(Mesh), true);

            if (GUILayout.Button("Load Custom Mesh"))
            {
                if (spriteRenderer != null && customLoadMesh != null)
                {
                    LoadMesh(customLoadMesh);
                }
            }

            EditorGUILayout.Separator();

            GUILayout.Label("Combine Meshes", EditorStyles.boldLabel);

            GUILayout.Label("Avoid Combining Complex Meshes, results will vary", EditorStyles.whiteLabel);

            EditorGUILayout.Separator();
            combineMesh = (Mesh)EditorGUILayout.ObjectField(combineMesh, typeof(Mesh), true);

            if (GUILayout.Button("Combine Meshes"))
            {
                if (spriteRenderer != null)
                {
                    CombineMesh();
                }
            }
        }
    }