0

When I use the following method in my Update with Unity2017 and MonoDevelop on a Mac, everything is fine and I can see the graph animating and changing colors.

However, when I use it on a Windows machine using VisualStudio2017 (because it is part of a HoloLens project) visual studio complains that .color and .size are deprecated, so I should use their replacements, but when I do that, everything shows in pink.

Does anyone know how I can correct this issue?

using UnityEngine;

public class GraphAnimator : MonoBehaviour
{
    [Range(30, 100)]
    public int _resolution = 30;
    private int _currentResolution;

    private ParticleSystem.Particle[] _points;
    private ParticleSystem _particleSystem;

    public enum FunctionOption
    {
        Sine,
        Linear,
        Exponential,
        Parabola
    }
    public FunctionOption _function;

    private delegate float FunctionDelegate(float x);
    private static FunctionDelegate[] _functionDelegates =
    {
        Sine,
        Linear,
        Exponential,
        Parabola
    };


    private void Start()
    {
        _particleSystem = GetComponent<ParticleSystem>();    
        CreatePoints();
    }


    private void CreatePoints()
    {
        if (_resolution < 30 || _resolution > 100)
        {
            Debug.LogWarning("Grapher resolution out of bounds, resetting to 30", this);
            _resolution = 30;
        }

        _currentResolution = _resolution;

        _points = new ParticleSystem.Particle[_resolution];

        float increment = 1f / (_resolution - 1);
        for (int i = 0; i < _resolution; i++)
        {
            float x = i * increment;
            _points[i].position = new Vector3(x, 0f, 0f);
            _points[i].color = new Color(x, 0f, 0f);
            _points[i].size = 0.1f;
        }
    }


    private void Update()
    {
        if (_currentResolution != _resolution || _points == null)
        {
            CreatePoints();
        }

        FunctionDelegate f = _functionDelegates[(int)_function];

        for (int i = 0; i < _resolution; i++)
        {
            Vector3 p = _points[i].position;
            p.y = f(p.x);
            _points[i].position = p;

            Color c = _points[i].color;
            c.g = p.y;
            _points[i].color = c;
        }

        _particleSystem.SetParticles(_points, _points.Length);
    }


    private static float Sine(float x)
    {
        return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad);
    }

    private static float Linear(float x)
    {
        return x;
    }

    private static float Exponential(float x)
    {
        return x * x;
    }

    private static float Parabola(float x)
    {
        x = 2f * x - 1f;
        return x * x;
    }
}

I have attached 2 screenshots that show the before (Unity2017 and MonoDevelop on a Mac) and after (Windows machine using VisualStudio2017) colors for the animated graph.

The replacements I tried include _points[i].startColor = new Color(x, 0f, 0f); and _points[i].startSize = 0.1f; as well as declaring ParticleSystem.MainModule main = _particleSystem.main; and

ParticleSystem.MainModule settings = GetComponent<ParticleSystem>().main;
settings.startColor = new ParticleSystem.MinMaxGradient( new Color(x, 0f, 0f) );

plus

settings.startColor = new Color(x, 0f, 0f);

I get no warnings or errors, but it all shows in pink, as visible in the screenshots. This script is attached to an empty game object that also has a particle system component attached to.

enter image description here enter image description here


EDIT:

It turned out reference to materials was missing. So, it was resolved based on help/comment by user 'Programmer'.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • @Programmer It is not quite the same as that. I have seen that post, specifically the new color and size assignment, but what I cannot figure out in the new API is how to assign these properties to individual particles. It seem to me that it is now all done in the main particle system. From that page, can you figure out how to alter the size and color for every element of my array that contains single particle points? I have tried a couple of variations with the new startColor and startSize, but it all shows in pink... –  Sep 06 '17 at 09:43
  • It's the-same thing. If you think it's not, at least add EDIT in your question and add the new code you made from the duplicate that doesn't work. Also explain what doesn't work in the new code. Also please explain what you meant by "but when I do that, everything shows in pink". What is pink? The code? Objects in the scene? How about a screenshot? – Programmer Sep 06 '17 at 09:45
  • 1
    I just saw your update. It works fine for me. You still did not update your question with your new code I can't continue to help since I can't see your code. By the way, the pink stuff means that the ParticleSystem lost reference to its Material. Happy coding! – Programmer Sep 07 '17 at 00:10
  • @Programmer Thank you. You are right. It was the materials missing, even though I never used any materials in the first place (on my Mac) but it was resolved. –  Sep 07 '17 at 06:16

0 Answers0