Skip to content

Instantly share code, notes, and snippets.

@Tunied
Created April 13, 2022 04:21
Show Gist options
  • Save Tunied/f2f11e1c4792b0e4c97b46772898148d to your computer and use it in GitHub Desktop.
Save Tunied/f2f11e1c4792b0e4c97b46772898148d to your computer and use it in GitHub Desktop.
生成一个QuadMesh并保存
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
namespace Code.EditorTools.CreateMesh
{
public class Mono_CreateQuadAssets : MonoBehaviour
{
public Transform left_bottom; //0
public Transform left_top; //1
public Transform right_bottom; //2
public Transform right_top; //3
[Button("生成", ButtonSizes.Large)]
private void CreateQuad()
{
var mesh = new Mesh();
var vertices = new[] {left_bottom.position, left_top.position, right_bottom.position, right_top.position};
var tris = new[]
{
// lower left triangle
0, 1, 3,
// upper right triangle
0, 3, 2
};
var normals = new[]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
var uv = new Vector2[]
{
new(0, 0),
new(1, 0),
new(0, 1),
new(1, 1)
};
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.uv = uv;
mesh.normals = normals;
var path = EditorUtility.SaveFilePanel("Save Mesh Asset", "Assets/", "Mesh_Quad", "asset");
if (string.IsNullOrEmpty(path)) return;
path = FileUtil.GetProjectRelativePath(path);
AssetDatabase.CreateAsset(mesh, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment