mesh线条的运用

本文介绍了一个Unity中用于创建动态矩形网格的基础组件。该组件允许通过调整宽度、长度和对齐方式来生成不同尺寸的矩形网格,并支持运行时修改网格参数以实现动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[ExecuteInEditMode]
public class structureMeshBase : MonoBehaviour
{

    protected MeshFilter meshFilter;
    public Transform cacheTransform;
    protected Vector3[] vertices;
    protected int[] triangles;
    protected Vector2[] uvs;

    void Awake()
    {
        Init();
        InitMesh();
        transform.rotation = Quaternion.Euler(90, 0, 0);
    }

    protected virtual void Init()
    {
        cacheTransform = this.transform;
        meshFilter = GetComponent<MeshFilter>();
        meshFilter.sharedMesh = new Mesh();
    }
    protected virtual void InitMesh()
    {

    }
    protected virtual void UpdateShape()
    {

    }
    protected virtual void UpdateMesh()
    {
        if (meshFilter.sharedMesh)
        {
            meshFilter.sharedMesh.vertices = vertices;
            meshFilter.sharedMesh.triangles = triangles;
            meshFilter.sharedMesh.uv = uvs;
        }

    }
    void OnValidate()
    {
        InitMesh();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;

public class structureRectMesh : structureMeshBase
{

    public Vector3 m_offset;
    public Vector3 offset
    {
        get { return m_offset; }
        set
        {
            m_offset = value;
            UpdateShape();
        }
    }

    public float m_Width;
    public float width
    {
        get { return m_Width; }
        set
        {
            m_Width = value;
            UpdateShape();
        }
    }

    public float m_Length;

    public float length
    {
        get { return m_Length; }
        set
        {
            m_Length = value;
            UpdateShape();
        }
    }


    public enum PivotAlign
    {
        Left,
        Center,
        Right,
    }
    public PivotAlign widthAlign = PivotAlign.Center;
    public PivotAlign lengthAlign = PivotAlign.Center;

    protected override void InitMesh()
    {
        if (cacheTransform == null || meshFilter == null)
        {
            Init();
        }
        triangles = new int[] { 0, 2, 3, 0, 3, 1 };
        uvs = new Vector2[4];
        uvs[0].x = 0;
        uvs[0].y = 0;
        uvs[1].x = 1;
        uvs[1].y = 0;
        uvs[2].x = 0;
        uvs[2].y = 1;
        uvs[3].x = 1;
        uvs[3].y = 1;
        UpdateShape();
    }
    protected override void UpdateShape()
    {
        Vector3 localPot = offset;
        float w2 = m_Width * 0.5f;
        float l2 = m_Length * 0.5f;

        vertices = new Vector3[4];
        float x0, z0, x1, z1, x2, z2, x3, z3;
        x0 = z0 = x1 = z1 = x2 = z2 = x3 = z3 = 0;

        switch (widthAlign)
        {
            case PivotAlign.Left:
                x0 = 0f;
                x1 = m_Width;
                x2 = 0f;
                x3 = m_Width;
                break;
            case PivotAlign.Center:
                x0 = -w2;
                x1 = w2;
                x2 = -w2;
                x3 = w2;

                break;
            case PivotAlign.Right:
                x0 = -m_Width;
                x1 = 0f;
                x2 = -m_Width;
                x3 = 0f;
                break;
        }
        switch (lengthAlign)
        {
            case PivotAlign.Left:
                z0 = 0f;
                z1 = 0f; ;
                z2 = m_Length;
                z3 = m_Length;
                break;
            case PivotAlign.Center:
                z0 = -l2;
                z1 = -l2;
                z2 = l2;
                z3 = l2;

                break;
            case PivotAlign.Right:
                z0 = -m_Length;
                z1 = -m_Length;
                z2 = 0f;
                z3 = 0f;
                break;
        }
        vertices[0].x = localPot.x + x0;
        vertices[0].y = localPot.y;
        vertices[0].z = localPot.z + z0;

        vertices[1].x = localPot.x + x1;
        vertices[1].y = localPot.y;
        vertices[1].z = localPot.z + z1;

        vertices[2].x = localPot.x + x2;
        vertices[2].y = localPot.y;
        vertices[2].z = localPot.z + z2;

        vertices[3].x = localPot.x + x3;
        vertices[3].y = localPot.y;
        vertices[3].z = localPot.z + z3;
        UpdateMesh();
    }
    public Tweener DoLength(float endValue, float duration, float delay)
    {
        return DOTween.To(() => m_Length, x => length = x, endValue, duration).SetDelay(delay);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值