You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity中基于外部TXT文件实现粒子系统的条件循环控制?

搞定Unity粒子系统的TXT控制方案

嘿,恭喜你刚接触Unity两天就整出4个粒子系统,这进度相当可以!先给你个定心丸:完全不用给每个粒子系统单独写脚本,一个统一的控制脚本就能搞定所有逻辑,既简洁又好维护。下面一步步教你实现:

第一步:准备工作

  • 把所有粒子系统的Play On Awake选项取消勾选(在Inspector面板里找),避免一开始就自动播放。
  • 新建一个StreamingAssets文件夹(如果没有的话),把你的控制TXT文件放进去(比如命名为emotions.txt),这样打包后也能正常读取。

第二步:编写控制脚本

创建一个C#脚本(比如叫ParticleSystemController),挂在任意空物体或者你的行星对象上,然后把下面的代码粘进去,注释都写得很清楚,新手也能看懂:

using UnityEngine;
using System.IO;
using System.Collections;

public class ParticleSystemController : MonoBehaviour
{
    // 在Inspector里把对应的粒子系统拖到这些变量里
    public ParticleSystem angerParticles;
    public ParticleSystem excitementParticles;
    public ParticleSystem defaultParticles;
    // 如果第四个粒子系统有单独用途,可以再加一个public ParticleSystem xxxParticles;

    // TXT文件的路径(自动指向StreamingAssets文件夹)
    private string txtFilePath;

    void Start()
    {
        // 初始状态下禁用所有粒子系统
        DisableAllParticles();

        // 设置TXT文件的完整路径
        txtFilePath = Path.Combine(Application.streamingAssetsPath, "emotions.txt");
        
        // 开始读取并处理TXT内容
        StartCoroutine(ProcessTxtContent());
    }

    // 通用方法:禁用所有粒子系统
    void DisableAllParticles()
    {
        if (angerParticles != null)
        {
            angerParticles.Stop();
            angerParticles.gameObject.SetActive(false);
        }
        if (excitementParticles != null)
        {
            excitementParticles.Stop();
            excitementParticles.gameObject.SetActive(false);
        }
        if (defaultParticles != null)
        {
            defaultParticles.Stop();
            defaultParticles.gameObject.SetActive(false);
        }
        // 第四个粒子系统同理,复制上面的逻辑就行
    }

    // 协程:逐行处理TXT内容,控制粒子播放时长
    IEnumerator ProcessTxtContent()
    {
        // 先检查文件是否存在
        if (!File.Exists(txtFilePath))
        {
            Debug.LogError("找不到TXT文件!请检查文件是否放在StreamingAssets文件夹里,文件名是否正确");
            yield break;
        }

        // 读取TXT的所有行
        string[] emotionLines = File.ReadAllLines(txtFilePath);

        // 逐行处理每个情绪指令
        foreach (string line in emotionLines)
        {
            // 去掉行首尾的空格和换行符,避免匹配出错
            string currentEmotion = line.Trim();

            // 先关掉所有正在播放的粒子
            DisableAllParticles();

            // 随机生成10-20秒的播放时长(21是因为Random.Range的上限是开区间)
            float playTime = Random.Range(10f, 21f);

            // 根据情绪激活对应粒子系统
            switch (currentEmotion)
            {
                case "Anger":
                    ActivateAndPlay(angerParticles);
                    break;
                case "Excitement":
                    ActivateAndPlay(excitementParticles);
                    break;
                default:
                    // 匹配不到的情绪就播放默认粒子
                    ActivateAndPlay(defaultParticles);
                    break;
            }

            // 等待指定时长后,再处理下一行指令
            yield return new WaitForSeconds(playTime);
        }

        // 所有指令处理完后,关掉所有粒子
        DisableAllParticles();
    }

    // 通用方法:激活并播放粒子系统
    void ActivateAndPlay(ParticleSystem particleSys)
    {
        if (particleSys != null)
        {
            particleSys.gameObject.SetActive(true);
            particleSys.Play();
        }
    }
}

第三步:配置脚本

  • 在Unity编辑器里,选中挂了这个脚本的物体,把你创建的四个粒子系统分别拖到脚本对应的public变量上(如果第四个是备用的,记得在脚本里加对应的变量并拖入)。
  • 给emotions.txt写测试内容,比如:
    Anger
    Excitement
    Calm
    Anger
    

一些小提示

  • 如果需要循环播放TXT里的指令,可以在ProcessTxtContent协程的末尾加一句StartCoroutine(ProcessTxtContent());,这样就会无限循环下去。
  • 如果粒子系统播放完后有残留效果,可以在DisableAllParticles里调用particleSys.Clear();来清除粒子。
  • 测试时如果遇到文件找不到的问题,记得检查StreamingAssets文件夹的拼写(大小写要对),以及TXT文件名是否和脚本里的一致。

内容的提问来源于stack exchange,提问作者Quinton Jackson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:13:56