首页 > 解决方案 > Nvidia Flex:为什么我不能在 FlexArray Actor 中设置粒子数?(统一)

问题描述

我是 C# 编码的新手,无法在 Nvidia FlexArray 脚本中设置粒子计数,粒子计数公共类显示为灰色且无法编辑。它以某种方式永久设置为0。

我附上了一张图片和包装附带的脚本。我非常感谢任何帮助。数组 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
    }
}

标签: c#unity3dnvidia-flex

解决方案


属性不能在 Unity 中直接序列化。

推荐的流程是将 m_particleCount 标记为 [SerializeField],即使它是私有的,它也会将其公开给编辑器。

看起来它可能隐藏在 Flex API 中,所以我不确定你是否可以直接编辑它。在这种情况下,更复杂的编辑器脚本可以完成这项工作,我建议尝试用您自己的值破解该值,然后使用OnValidate()将该值写入真实值。

也就是说,这个值真的是你想象的那样吗?我没有使用 API,但“particleCount”通常是系统中活动粒子的数量,在编辑时自然为 0,但在运行时可能为 10 或 20 或 1000,具体取决于粒子系统在做什么.


推荐阅读