Nvidia Flex: Why can't I set particle count in FlexArray Actor? (Unity)

437 views Asked by At

Im new to coding in C# and I'm not able to set the particle count in the Nvidia FlexArray script, the particle count public class appears grey and can't be edited. It is somehow permanently set to 0.

Ive attached an image and the script that comes with the package. Id greatly appreciate any help. The image for the inspector view on array actor

using UnityEngine;

namespace NVIDIA.Flex
{
    [ExecuteInEditMode]
    public class FlexArrayAsset : FlexAsset
    {
        #region Properties

        public Mesh boundaryMesh
        {
            get { return m_boundaryMesh; }
            set { m_boundaryMesh = value; }
        }

        public Vector3 meshLocalScale
        {
            get { return m_meshLocalScale; }
            set { m_meshLocalScale = value; }
        }

        public float meshExpansion
        {
            get { return m_meshExpansion; }
            set { m_meshExpansion = value; }
        }

        public float particleSpacing
        {
            get { return m_particleSpacing; }
            set { m_particleSpacing = Mathf.Max(value, 0.01f); }
        }
        public float particleCount
        {
            get { return m_particleCount; }
            set { m_particleCount = Mathf.Max(value, 516f); }
        }
        
        #endregion

        #region Methods


        #endregion

        #region Messages


        #endregion

        #region Protected

        protected override void ValidateFields()
        {
            base.ValidateFields();
            m_particleSpacing = Mathf.Max(m_particleSpacing, 0.01f);
        }

        protected override void RebuildAsset()
        {
            BuildFromMesh();
            base.RebuildAsset();
        }

        #endregion

        #region Private

        void BuildFromMesh()
        {
            if (m_boundaryMesh)
            {
                Vector3[] vertices = m_boundaryMesh.vertices;
                if (vertices != null && vertices.Length > 0)
                {
                    for (int i = 0; i < vertices.Length; ++i)
                    {
                        Vector3 v = vertices[i];
                        vertices[i] = new Vector3(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z);
                    }
                    int[] indices = m_boundaryMesh.triangles;
                    if (indices != null && indices.Length > 0)
                    {
                        FlexExt.Asset.Handle assetHandle = FlexExt.CreateRigidFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, m_particleSpacing, m_meshExpansion);
                        if (assetHandle)
                        {
                            FlexExt.Asset asset = assetHandle.asset;
                            FlexExt.Asset particlesOnly = new FlexExt.Asset();
                            particlesOnly.numParticles = asset.numParticles;
                            particlesOnly.maxParticles = asset.numParticles;
                            particlesOnly.particles = asset.particles;
                            StoreAsset(particlesOnly);

                            FlexExt.DestroyAsset(assetHandle);
                        }
                    }
                }
            }
        }

        [SerializeField]
        Mesh m_boundaryMesh = null;
        [SerializeField]
        Vector3 m_meshLocalScale = Vector3.one;
        [SerializeField, Tooltip("Particles will be moved inwards (if negative) or outwards (if positive) from the surface of the mesh according to this factor")]
        float m_meshExpansion = 0.0f;
        [SerializeField, Tooltip("The spacing used for voxelization, note that the number of voxels grows proportional to the inverse cube of radius, currently this method limits construction to resolutions < 64^3")]
        float m_particleSpacing = 0.1f;

        #endregion
    }
}

1

There are 1 answers

3
Empty On

Properties can't be serialized directly in Unity.

Recommended flow would be to mark m_particleCount as a [SerializeField], which exposes it to the editor even if it's private.

It looks like it might be buried in the Flex API though, so I'm not sure if you can edit it directly. A more complex editor script could do the job in such a case, I'd recommend looking at trying to hack the value with your own value, then using OnValidate() to write that value to the real one.

That said, is this value really what you think it is? I haven't used the API but 'particleCount' is usually the number of live particles in the system, naturally at edit time it would be 0, but at runtime it could be 10 or 20 or 1000 depending on what the particle system is doing.