以下.cs需要放置d在名称为 Editor 的文件夹下
EditorFlipMeshNormals.cs
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
/// <summary>
/// 在 Hierarchy 视图对象的快捷菜单中增加 Flip Mesh Normals(反转网格法线)项
/// </summary>
public class EditorFlipMeshNormals : Editor {
/// <summary> 翻转网格的法线 </summary>
private static void FlipMeshNormals (Mesh mesh) {
int[] triangles = mesh.triangles;
for (int i = 0; i < triangles.Length; i += 3) {
// 交换三角形的首尾索引
int t = triangles[i];
triangles[i] = triangles[i + 2];
triangles[i + 2] = t;
}
mesh.triangles = triangles;
}
/// <summary> 翻转多个游戏对象网格的法线 </summary>
private static void FlipMeshNormals (GameObject[] gameObjects) {
for (int i = 0, len = gameObjects.Length; i < len; i++) {
GameObject go = gameObjects[i];
Mesh mesh = null;
SkinnedMeshRenderer skinnedMeshRenderer = go.GetComponent<SkinnedMeshRenderer>();
if (skinnedMeshRenderer) {
mesh = skinnedMeshRenderer.sharedMesh;
} else {
MeshFilter meshFilter = go.GetComponent<MeshFilter>();
if (meshFilter) {
mesh = meshFilter.sharedMesh;
}
}
if (mesh) {
FlipMeshNormals(mesh);
}
}
}
/// <summary> 验证所选择的游戏对象有网格时菜单才可用(不计算子级) </summary>
[MenuItem("GameObject/Flip Mesh Normals", true)]
private static bool ValidateFlipMeshNormalsOnGameObject () {
bool isHasMesh = false;
GameObject[] gameObjects = Selection.gameObjects;
for (int i = 0, len = gameObjects.Length; i < len; i++) {
GameObject go = gameObjects[i];
SkinnedMeshRenderer skinnedMeshRenderer = go.GetComponent<SkinnedMeshRenderer>();
if (skinnedMeshRenderer) {
isHasMesh = true;
break;
}
MeshFilter meshFilter = go.GetComponent<MeshFilter>();
if (meshFilter) {
isHasMesh = true;
break;
}
}
return isHasMesh;
}
[MenuItem("GameObject/Flip Mesh Normals", false, 11)]
private static void FlipMeshNormalsOnGameObject () {
FlipMeshNormals(Selection.gameObjects);
}
}
#endif