基于贴花的弹孔实现

作者:追风剑情 发布于:2025-4-30 11:48 分类:Unity3d

一、准备一张弹孔贴图

bullet_hole_small.png

二、修改弹孔贴图属性

1111111.png

三、测试脚本

将脚本挂在模型上。脚本中用到了 Texture2D 的扩展方法,参见 Texture2DExtension

using UnityEngine;
/// <summary>
/// 铁箱子
/// </summary>
public class TestIronBox : MonoBehaviour
{
    //贴花图片
    public Texture2D decal;
    private Material material;
    //备份下原始贴图
    private Texture2D originTexture2D;
    //新贴图
    private Texture2D mainTexture;

    private void Awake()
    {
        material = GetComponent<MeshRenderer>().material;
        originTexture2D = material.mainTexture as Texture2D;
        mainTexture = originTexture2D.Clone();
        material.mainTexture = mainTexture;
    }

    private void OnDestroy()
    {
        material.mainTexture = originTexture2D;
    }

    // 测试:绘制弹孔
    public void DrawBulletHole(RaycastHit hit)
    {
        mainTexture.DrawDecal(hit, decal);
    }
}


四、测试效果

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号