unity中实现圆柱,cube等以外的立方体 棱锥

这篇博客介绍如何在Unity中实现除了圆柱和立方体之外的几何形状,如棱锥。通过提供的C#代码示例,展示了如何利用MeshFilter和MeshRenderer组件创建具有特定长度、宽度和高度的3D三角形,并在Unity编辑器中进行预览和调整。

想做来在3D项目中做标记 直接上代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class DrawTriangle : MonoBehaviour
{
public float length = 1;
public float width = 1;
public float height = 1;

private MeshFilter meshFilter;
void Start()
{
    meshFilter = GetComponent<MeshFilter>();
    meshFilter.mesh = CreateTriangle(length, width, height);
    if (this.gameObject.AddComponent<MeshCollider>() == null)
        this.gameObject.AddComponent<MeshCollider>();
    // this.GetComponent<MeshRenderer>().enabled = false;
    this.transform.DOMoveY(this.transform.position.y - 0.3f, 0.5f, false).SetLoops(int.MaxValue, LoopType.Yoyo);
}

private Mesh CreateTriangle(float length, float width, float height)
{
    int verticesCount = 4 * 6;//3* 4;
    Vector3[] vertices = new Vector3[verticesCount];

    vertices[0] = Vector3.zero;                 //前面左下角的点
    vertices[1] = new Vector3(0, height, 0);    //前面左上角的点
    vertices[2] = new Vector3(length, height / 2, width / 2);   // 前面右下角的点    
    vertices[3] = new Vector3(length, height / 2, width / 2);//前面右上角的点

    vertices[4] = new Vector3(length, height / 2, width/2);        //后面右下角的点
    vertices[5] = new Vector3(length, height/2, width/2);    //后面右上角的点
    vertices[6] = new Vector3(0, 0, width);             //后面左下角的点
    vertices[7] = new Vector3(0, height, width);        //后面左上角的点

    vertices[8] = vertices[6];                              //左
    vertices[9] = vertices[7];
    vertices[10] = vertices[0];
    vertices[11] = vertices[1];

    vertices[12] = vertices[2];                              //右
    vertices[13] = vertices[3];
    vertices[14] = vertices[6];
    vertices[15] = vertices[7];

    vertices[16] = vertices[1];                              //上
    vertices[17] = vertices[7];
    vertices[18] = vertices[3];
    vertices[19] = vertices[7];

    vertices[20] = vertices[2];                              //下
    vertices[21] = vertices[6];
    vertices[22] = vertices[0];
    vertices[23] = vertices[6];


    int triangleCount = 6 * 2 * 3;
    int[] triangles = new int[triangleCount];
    for (int i = 0, v = 0; i < triangleCount; i += 6, v += 4)
    {
        triangles[i] = v;
        triangles[i + 1] = v + 1;
        triangles[i + 2] = v + 2;
        triangles[i + 3] = v + 3;
        triangles[i + 4] = v + 2;
        triangles[i + 5] = v + 1;
    }
    Mesh mesh = new Mesh();
    mesh.vertices = vertices;
    mesh.triangles = triangles;
    return mesh;
}

#if UNITY_EDITOR
//用于在编辑状态设置大小
void OnDrawGizmos()
{
meshFilter = GetComponent();
meshFilter.mesh = CreateTriangle(length, width, height);
}
#endif
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值