Unity 用粒子排列成图片

本文介绍如何使用Unity和UnityDOTS结合图片像素信息创建动态粒子系统,形成太极剑阵效果。通过获取PNG图片的像素坐标,调整粒子系统的位置,使粒子能够精确地分布在指定位置上。

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

思路: 获取图片的像素位置,然后再将粒子一个个的移动到该位置上,参考自用Unity DOTS制作4万飞剑的太极剑阵

1.首先来一张PNG图片
请添加图片描述
2.将图片拖入Unity中,尺寸越大,像素点越高,粒子也就增多
在这里插入图片描述
3.添加GetPixel代码,作用是获取图片像素的坐标位置,来源这里

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Mathematics;
public class GetPixel : MonoBehaviour
{
    public Sprite sprite;
    //像素点相对位置
    public List<Vector3> posList;
    public List<Color> poxelColor;

    [Header("Drawing")]
    //剑阵密度
    public int drawDensity = 5;
    //剑阵离散程度
    public int disperseMin;
    public static GetPixel Instance;
    //图片宽高
    private int width;
    private int height;
    void Start()
    {
        Instance = this;
        width = sprite.texture.width;
        height = sprite.texture.height;
        Debug.Log("图片宽度" + width + "图片高度" + height);
        StartCoroutine(ie_GetPixelPos());
    }
    IEnumerator ie_GetPixelPos()
    {
        //yield return new WaitForSeconds(5);
        int halfHeight= height / 2;
        int halfWidth = width / 2;
        Vector3 tempPos = new Vector3();
        for (int i = 0; i < height; i += drawDensity)
        {
            for (int j = 0; j < width; j += drawDensity)
            {
                //获取每个位置像素点的颜色
                Color32 c = sprite.texture.GetPixel(j, i);
                tempPos.y = (j - halfHeight) * disperseMin;
                // Debug.Log("RGBA:" + c);
                //如果对应位置颜色不为透明,则记录坐标到List中
                if (c.a != 0)
                {
                    tempPos.x = (i - halfWidth) * disperseMin;
                    poxelColor.Add(c);
                    posList.Add(tempPos);
                }
            }
            yield return null;
        }
        //总像素长度
        Debug.Log("位置List长度: "+posList.Count);
    }
}

在这里插入图片描述
4.创建粒子系统并添加代码TestParticleMovement,获取像素点位置,并将粒子移动到改位置,粒子数量要与像素点位置的数量一致。
在这里插入图片描述

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestParticleMovement : MonoBehaviour
{
    private ParticleSystem particles;
    public int partCount;
    public int speed=2;
    ParticleSystem.Particle[] particleList;
    void Start()
    {
        particles = GetComponent<ParticleSystem>();
        Invoke("Init", 3);
    }

    void Init()
    {
        particleList = new ParticleSystem.Particle[particles.particleCount];
        StartCoroutine(ie_Move());
    }
    IEnumerator ie_Move()
    {
        while (true)
        {
            partCount = particles.GetParticles(particleList);
            int count = 0;
            for (int i = 0; i < partCount; i++)
            {
                float a = Vector3.Distance(GetPixel.Instance.posList[i], particleList[i].position);
                if (a == 0)
                {
                    count++;
                    continue;
                }
                else if (a < 0.5f)
                {
                    particleList[i].velocity = Vector3.zero;
                    particleList[i].position = GetPixel.Instance.posList[i];
                }
                else
                {
                    particleList[i].velocity += (GetPixel.Instance.posList[i] - particleList[i].position) * Time.deltaTime * speed;
                }
            }
            particles.SetParticles(particleList, partCount);
            if (count == partCount)
            {
               break;
            }
            yield return null;
        }

        Debug.Log("协程结束");
	}
}

效果图
请添加图片描述

完毕。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值