Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为粒子系统在Unity中设置If-Else语句循环_C#_Unity3d_Scripting - Fatal编程技术网

C# 为粒子系统在Unity中设置If-Else语句循环

C# 为粒子系统在Unity中设置If-Else语句循环,c#,unity3d,scripting,C#,Unity3d,Scripting,我两天前才开始使用Unity。我已经在Unity中创建了4个粒子系统,我正在尝试在我创建的行星上设置动画,但我想根据外部源的.txt文件使每个粒子系统处于活动状态10-20秒。有人知道怎么做吗 有点像这样 if (line == 'Anger') #play said animation else if (line == 'Excitement') #play other animation else #play last animation 我是否需要创建每个粒

我两天前才开始使用Unity。我已经在Unity中创建了4个粒子系统,我正在尝试在我创建的行星上设置动画,但我想根据外部源的.txt文件使每个粒子系统处于活动状态10-20秒。有人知道怎么做吗

有点像这样

if (line == 'Anger')
     #play said animation
else if (line == 'Excitement')
     #play other animation
else
     #play last animation
我是否需要创建每个粒子系统都需要读取的脚本?

这并不是那么容易。 最好的办法是为每个粒子系统设置一个不同的对象。他们最初都是残疾人。然后,如果要播放特定的粒子样式,请启用具有所需粒子系统的对象

gameObject.SetActive(true);
如果只想在指定的时间段内保持对象的活动状态,请使用协同路由,在经过一段时间后,禁用对象以将其关闭。使用“世界空间粒子”(world space particles)可以防止在出于美学目的禁用对象时所有粒子都消失

或者,跳过协同路线并在inspector中手动设置粒子系统持续时间,或者通过getcomponent()对其进行更改

快速示例:

// Assign the game objects manually from the Unity interface via drag and drop. That is why public GameObject[].

Public GameObject[] ParticleSystems;
Private String[] YourEmotions;
float activationDuration = 15f;

// Your code to retrieve the txt value and assign to 'YourEmotions' array, or use YourEmotions = {"Angry", "Sad", "Happy"};. Have the order match the order of the equivalent game object. Then when you need the particle system, active it by calling StartParticularParticleSystem and passing in the string identifier of the one you want to use.
// ...

Public Void StartParticularParticleSystem(string Emotion)
{

    for(int i = 0; i < ParticleSystems.Count; i++)
    {
       if(Emotion == YourEmotions[i])
       {
          StartCoroutine(MyCoroutine(ParticleSystems[i]));
       }
    }

}

IEnumerator MyCoroutine (GameObject ObjectToActivate)
{
    ObjectToActivate.SetActive(True);    
    yield return new WaitForSeconds(activationDuration);
    ObjectToActivate.SetActive(False);
}
//通过拖放从Unity界面手动分配游戏对象。这就是为什么公共游戏对象[]。
公共游戏对象[]粒子系统;
私人情感;
浮子激活时间=15f;
//您的代码用于检索txt值并分配给“YourMoothers”数组,或者使用YourMoothers={“愤怒”、“悲伤”、“快乐”};。使顺序与等效游戏对象的顺序匹配。然后,当需要粒子系统时,通过调用StartParticularParticleSystem并传入要使用的粒子系统的字符串标识符来激活它。
// ...
Public Void startParticularParticle系统(字符串)
{
for(int i=0;i
我相信还有其他方法可以做到这一点。不过,我不认为有什么是非常简单的。

你应该看看Unity的内置功能