public int triangleIndex ;

描述

命中的三角形的索引。

仅当命中的碰撞体是 MeshCollider 时,三角形索引才有效。

// This script draws a debug line around mesh triangles
// as you move the mouse over them.
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { Camera cam;

void Start() { cam = GetComponent<Camera>(); }

void Update() { RaycastHit hit; if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit)) return;

MeshCollider meshCollider = hit.collider as MeshCollider; if (meshCollider == null || meshCollider.sharedMesh == null) return;

Mesh mesh = meshCollider.sharedMesh; Vector3[] vertices = mesh.vertices; int[] triangles = mesh.triangles; Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]]; Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]]; Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]]; Transform hitTransform = hit.collider.transform; p0 = hitTransform.TransformPoint(p0); p1 = hitTransform.TransformPoint(p1); p2 = hitTransform.TransformPoint(p2); Debug.DrawLine(p0, p1); Debug.DrawLine(p1, p2); Debug.DrawLine(p2, p0); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961