Skip to content

Instantly share code, notes, and snippets.

@andybak
Created June 4, 2019 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/660f765a1eaf8b00dd819920da0c2613 to your computer and use it in GitHub Desktop.
Save andybak/660f765a1eaf8b00dd819920da0c2613 to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.VersionControl;
public static class MeshSaverEditor {
[MenuItem("Assets/Extrude Image")]
public static void ExtrudeImage () {
var tex = Selection.activeObject as Texture2D;
var sprite = Sprite.Create(
tex,
new Rect(0.0f, 0.0f, tex.width, tex.height),
new Vector2(0.5f, 0.5f),
100.0f,
0,
SpriteMeshType.Tight
);
var vertices = sprite.vertices;
var tris = sprite.triangles;
var uv = sprite.uv;
var go = new GameObject(tex.name);
var mf = go.AddComponent<MeshFilter>();
var mr = go.AddComponent<MeshRenderer>();
var mesh = new Mesh();
mesh.name = tex.name;
mesh.vertices = vertices.Select(x => (Vector3)x).ToArray();
mesh.uv = uv;
mesh.triangles = tris.Select(x => (int)x).ToArray();
var shader = Shader.Find("Standard");
var material = new Material(shader);
material.name = tex.name;
material.mainTexture = tex;
mr.material = material;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();
mf.sharedMesh = mesh;
}
[MenuItem("Assets/Extrude Image", true)]
static bool ValidateExtrudeImage (MenuCommand menuCommand)
{
var path = AssetDatabase.GetAssetPath(Selection.activeObject);
return path.Contains(".png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment