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.
EDIT:
It turned out reference to materials was missing. So, it was resolved based on help/comment by user 'Programmer'.