0

我相信每个人都知道这个脚本http://wiki.unity3d.com/index.php/Floating_Origin,它可以轻松解决浮动原点的问题。

问题是脚本已经过时并且没有移动由视觉效果图创建的粒子效果。

我试图重写它,但我似乎无法创建一个数组来存储所有粒子,就像前一个一样,因此我无法从那里继续。

这是我的代码:

// Based on the Unity Wiki FloatingOrigin script by Peter Stirling
// URL: http://wiki.unity3d.com/index.php/Floating_Origin

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.VFX;
using UnityEngine.Experimental.VFX;

public class FloatingOrigin : MonoBehaviour
{
    [Tooltip("Point of reference from which to check the distance to origin.")]
    public Transform ReferenceObject = null;

    [Tooltip("Distance from the origin the reference object must be in order to trigger an origin shift.")]
    public float Threshold = 5000f;

    [Header("Options")]
    [Tooltip("When true, origin shifts are considered only from the horizontal distance to orign.")]
    public bool Use2DDistance = false;

    [Tooltip("When true, updates ALL open scenes. When false, updates only the active scene.")]
    public bool UpdateAllScenes = true;

    [Tooltip("Should ParticleSystems be moved with an origin shift.")]
    public bool UpdateParticles = true;

    [Tooltip("Should TrailRenderers be moved with an origin shift.")]
    public bool UpdateTrailRenderers = true;

    [Tooltip("Should LineRenderers be moved with an origin shift.")]
    public bool UpdateLineRenderers = true;

    private ParticleSystem.Particle[] parts = null;

    VisualEffect[] visualEffect = null;

    void LateUpdate()
    {
        if (ReferenceObject == null)
            return;

        Vector3 referencePosition = ReferenceObject.position;

        if (Use2DDistance)
            referencePosition.y = 0f;

        if (referencePosition.magnitude > Threshold)
        {
            MoveRootTransforms(referencePosition);

            if (UpdateParticles)
                MoveParticles(referencePosition);

            if (UpdateTrailRenderers)
                MoveTrailRenderers(referencePosition);

            if (UpdateLineRenderers)
                MoveLineRenderers(referencePosition);
        }
    }

    private void MoveRootTransforms(Vector3 offset)
    {
        if (UpdateAllScenes)
        {
            for (int z = 0; z < SceneManager.sceneCount; z++)
            {
                foreach (GameObject g in SceneManager.GetSceneAt(z).GetRootGameObjects())
                    g.transform.position -= offset;
            }
        }
        else
        {
            foreach (GameObject g in SceneManager.GetActiveScene().GetRootGameObjects())
                g.transform.position -= offset;
        }
    }

    private void MoveTrailRenderers(Vector3 offset)
    {
        var trails = FindObjectsOfType<TrailRenderer>() as TrailRenderer[];
        foreach (var trail in trails)
        {
            Vector3[] positions = new Vector3[trail.positionCount];

            int positionCount = trail.GetPositions(positions);
            for (int i = 0; i < positionCount; ++i)
                positions[i] -= offset;

            trail.SetPositions(positions);
        }
    }

    private void MoveLineRenderers(Vector3 offset)
    {
        var lines = FindObjectsOfType<LineRenderer>() as LineRenderer[];
        foreach (var line in lines)
        {
            Vector3[] positions = new Vector3[line.positionCount];

            int positionCount = line.GetPositions(positions);
            for (int i = 0; i < positionCount; ++i)
                positions[i] -= offset;

            line.SetPositions(positions);
        }
    }

    private void MoveParticles(Vector3 offset)
    {
        var particles = FindObjectsOfType<ParticleSystem>() as ParticleSystem[];
        foreach (ParticleSystem system in particles)
        {
            if (system.main.simulationSpace != ParticleSystemSimulationSpace.World)
                continue;

            int particlesNeeded = system.main.maxParticles;

            if (particlesNeeded <= 0)
                continue;

            bool wasPaused = system.isPaused;
            bool wasPlaying = system.isPlaying;

            if (!wasPaused)
                system.Pause();

            // ensure a sufficiently large array in which to store the particles
            if (parts == null || parts.Length < particlesNeeded)
            {
                parts = new ParticleSystem.Particle[particlesNeeded];
            }

            // now get the particles
            int num = system.GetParticles(parts);

            for (int i = 0; i < num; i++)
            {
                parts[i].position -= offset;
            }

            system.SetParticles(parts, num);

            if (wasPlaying)
                system.Play();
        }
        
        var particles2 = FindObjectsOfType<VisualEffect>() as VisualEffect[];
        foreach (VisualEffect system in particles2)
        {
            int particlesNeeded = system.aliveParticleCount;

            if (particlesNeeded <= 0)
                continue;

            bool wasPaused = !system.isActiveAndEnabled;
            bool wasPlaying = system.isActiveAndEnabled;

            if (!wasPaused)
                system.Stop();
            // ensure a sufficiently large array in which to store the particles
            if (visualEffect == null || visualEffect.Length < particlesNeeded)
            {
                visualEffect = new VisualEffect().visualEffectAsset[particlesNeeded];
            }

            // now get the particles
            int num = system.GetParticles(parts);

            for (int i = 0; i < num; i++)
            {
                parts[i].position -= offset;
            }

            system.SetParticles(parts, num);

            if (wasPlaying)
                system.Play();
            
        }
    } 
}

在线(这是一条错误的线,它下面的所有东西也是)

visualEffect = new VisualEffect().visualEffectAsset[particlesNeeded];

,我需要创建一个与该行类似的数组(正确的一个,但是对于旧的粒子系统)

parts = new ParticleSystem.Particle[particlesNeeded];

创建充满粒子的数组(但使用 VisualEffect 类)。

如果我能解决这个问题,其余的应该不会有任何问题。我认为解决这个问题将在现在和将来帮助成千上万的人,因为统一中浮动原点的限制是可怕的,并且大多数在统一中工作的人将需要使用 VFX 图形粒子的游戏世界的浮动原点。

谢谢您的帮助。

4

1 回答 1

0

我的问题已在这里得到解答: https ://forum.unity.com/threads/floating-origin-and-visual-effect-graph.962646/#post-6270837

于 2020-09-03T10:43:38.407 回答