我有以下脚本,我无法转换为新的统一类。这个脚本写于6 - 7年前,现在无法使用。
它引发了Particle[]
课程的错误,因为它现在是ParticleSystem.Particle[]
'
我只是设计师,无法将此行转换为新系统。任何帮助将不胜感激。
using UnityEngine;
public class LightningBolt : MonoBehaviour
{
public Transform target;
public int zigs = 100;
public float speed = 1f;
public float scale = 1f;
public Light startLight;
public Light endLight;
Perlin noise;
float oneOverZigs;
private Particle[] particles;
void Start()
{
oneOverZigs = 1f / (float)zigs;
particleEmitter.emit = false;
particleEmitter.Emit(zigs);
particles = particleEmitter.particles;
}
void Update ()
{
if (noise == null)
noise = new Perlin();
float timex = Time.time * speed * 0.1365143f;
float timey = Time.time * speed * 1.21688f;
float timez = Time.time * speed * 2.5564f;
for (int i=0; i < particles.Length; i++)
{
Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
noise.Noise(timey + position.x, timey + position.y, timey + position.z),
noise.Noise(timez + position.x, timez + position.y, timez + position.z));
position += (offset * scale * ((float)i * oneOverZigs));
particles[i].position = position;
particles[i].color = Color.white;
particles[i].energy = 1f;
}
particleEmitter.particles = particles;
if (particleEmitter.particleCount >= 2)
{
if (startLight)
startLight.transform.position = particles[0].position;
if (endLight)
endLight.transform.position = particles[particles.Length - 1].position;
}
}
}
答案 0 :(得分:2)
替换
particles[i].energy = 1f;
与
particles[i].remainingLifetime = 1f;
答案 1 :(得分:0)
由于此代码最初是由Unity提供的,因此我认为应该会出现一个可用的版本。
这是我转换后的脚本,似乎可以正常工作。
using UnityEngine;
using System.Collections;
using LibNoise.Generator;
[RequireComponent(typeof(ParticleSystem))]
public class LightningBolt : MonoBehaviour
{
public Transform target;
public Vector3 targetOffset;
public int zigs = 100;
public float speed = 1f;
public Vector3 speedMultiplier = new Vector3(0.1365143f, 1.21688f, 2.5564f);
public float scale = 1f;
public float lifetime = 0;
private Perlin noise;
private float oneOverZigs;
ParticleSystem m_System;
ParticleSystem.Particle[] m_Particles;
private void LateUpdate()
{
if (lifetime > 0)
{
lifetime -= Time.deltaTime;
}
if (target == null || lifetime < 0)
{
Destroy(gameObject);
return;
}
InitializeIfNeeded();
// GetParticles is allocation free because we reuse the m_Particles buffer between updates
int numParticlesAlive = m_System.GetParticles(m_Particles);
float timex = Time.time * speed * speedMultiplier.x;
float timey = Time.time * speed * speedMultiplier.y;
float timez = Time.time * speed * speedMultiplier.z;
// Change only the particles that are alive
for (int i = 0; i < numParticlesAlive; i++)
{
Vector3 position = Vector3.Lerp(transform.position, target.TransformPoint(targetOffset), oneOverZigs * (float)i);
Vector3 offset = new Vector3((float)noise.GetValue(timex + position.x, timex + position.y, timex + position.z),
(float)noise.GetValue(timey + position.x, timey + position.y, timey + position.z),
(float)noise.GetValue(timez + position.x, timez + position.y, timez + position.z));
position += (offset * scale * ((float)i * oneOverZigs));
m_Particles[i].position = position;
m_Particles[i].color = new Color(1, 1, 1, Mathf.Clamp01(lifetime * 10));
m_Particles[i].remainingLifetime = 1f;
}
// Apply the particle changes to the Particle System
m_System.SetParticles(m_Particles, numParticlesAlive);
}
void InitializeIfNeeded()
{
if (noise == null)
{
noise = new Perlin();
oneOverZigs = 1f / (float)zigs;
}
if (m_System == null)
{
m_System = GetComponent<ParticleSystem>();
m_System.Emit(zigs);
}
if (m_Particles == null || m_Particles.Length < m_System.main.maxParticles)
{
m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
}
}
}