Search Unity

Can't get ParticleSystem GetCustomParticleData/SetCustomParticleData to work

Discussion in 'Scripting' started by GearKlik, Sep 20, 2017.

  1. GearKlik

    GearKlik

    Joined:
    Sep 21, 2015
    Posts:
    58
    Hi all, I'm trying to use ParticleSystem GetCustomParticleData/SetCustomParticleData, I've had this working in 5.6 but don't have the code in front of me.

    I've tried this simple example from the Unity docs but when I step through the code GetCustomParticleData always returns vectors of 0,0,0,0

    Has anyone tested this lately? I'm on 2017.1.0f3

    Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.  
    7.     private ParticleSystem ps;
    8.     private List<Vector4> customData = new List<Vector4>();
    9.     private int uniqueID;
    10.  
    11.     void Start()
    12.     {
    13.        
    14.         ps = GetComponent<ParticleSystem>();
    15.         UnityEngine.ParticleSystem.CustomDataModule customData = ps.customData;
    16.         customData.enabled = true;
    17.  
    18.         customData.SetMode(ParticleSystemCustomData.Custom1, UnityEngine.ParticleSystemCustomDataMode.Vector);
    19.         customData.SetVectorComponentCount(ParticleSystemCustomData.Custom1, 4);
    20.         //customData.SetVector(ParticleSystemCustomData.Custom1, 0, 0);
    21.         //customData.SetVector(ParticleSystemCustomData.Custom1, 1, 0);
    22.         //customData.SetVector(ParticleSystemCustomData.Custom1, 2, 0);
    23.         //customData.SetVector(ParticleSystemCustomData.Custom1, 3, 0);
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.  
    29.         ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    30.  
    31.         for (int i = 0; i < customData.Count; i++)
    32.         {
    33.             // set custom data to the next ID, if it is in the default 0 state
    34.             if (customData[i].x == 0.0f)
    35.             {
    36.                 customData[i] = new Vector4(++uniqueID, 0, 0, 0);              
    37.             }
    38.         }
    39.  
    40.         ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    41.     }
    42. }
    TNKS!

    G
     
  2. GearKlik

    GearKlik

    Joined:
    Sep 21, 2015
    Posts:
    58
    EDIT: Spoke too soon, trying to come up with a good test case to log a bug

    TL;DT Solution: Setup custom particle data in Start not Awake

    I worked it out. The above is a simple test which actually works. In my dev code I was setting up the custom data in Awake not Start. I think the Particle system wasn't correctly initialed when I was trying to set properties on it.
     
    Last edited: Sep 22, 2017
  3. wutongtongtong

    wutongtongtong

    Joined:
    Jan 11, 2018
    Posts:
    8
    The same happened to me GetCustomParticleData always returns vectors of 0,0,0,0, only returns the correct uniqueID
    when GetCustomParticleData happened after SetCustomParticleData in the update.
    why? Is this is a bug? I'm on 2017.3.0
     
  4. GearKlik

    GearKlik

    Joined:
    Sep 21, 2015
    Posts:
    58
  5. GearKlik

    GearKlik

    Joined:
    Sep 21, 2015
    Posts:
    58