Init() public method

public Init ( ) : void
return void
Esempio n. 1
0
        // Should be called often to ensure we catch if selected Particle System is dragged in/out of root hierarchy
        public bool InitializeIfNeeded(IEnumerable <ParticleSystem> systems)
        {
            bool anyAdded = false;

            ParticleSystem[] allSystems     = systems.ToArray();
            bool             usingMultiEdit = (allSystems.Count() > 1);

            bool           initializeRequired = false;
            ParticleSystem mainSystem         = null;

            foreach (ParticleSystem shuriken in allSystems)
            {
                ParticleSystem[] shurikens;
                if (!usingMultiEdit)
                {
                    ParticleSystem root = ParticleSystemEditorUtils.GetRoot(shuriken);
                    if (root == null)
                    {
                        continue;
                    }

                    shurikens  = GetParticleSystems(root);
                    mainSystem = root;

                    // Check if we need to re-initialize?
                    if (m_SelectedParticleSystems != null && m_SelectedParticleSystems.Count > 0)
                    {
                        if (root == ParticleSystemEditorUtils.GetRoot(m_SelectedParticleSystems[0]))
                        {
                            if (m_ParticleSystemCurveEditor != null && m_Emitters != null && shurikens.Length == m_Emitters.Length && shuriken.gameObject.activeInHierarchy == m_EmittersActiveInHierarchy)
                            {
                                m_SelectedParticleSystems = new List <ParticleSystem>();
                                m_SelectedParticleSystems.Add(shuriken);

                                if (IsShowOnlySelectedMode())
                                {
                                    RefreshShowOnlySelected(); // always refresh
                                }
                                continue;
                            }
                        }
                    }
                }
                else
                {
                    // in multi-edit mode, we explicitly choose the systems to edit, so don't automatically add child systems or search for the root
                    shurikens  = new ParticleSystem[] { shuriken };
                    mainSystem = shuriken;
                }

                // Cleanup before initializing
                if (m_ParticleSystemCurveEditor != null)
                {
                    Clear();
                }

                // Now initialize
                initializeRequired = true;
                if (!anyAdded)
                {
                    m_SelectedParticleSystems = new List <ParticleSystem>();
                    anyAdded = true;
                }
                m_SelectedParticleSystems.Add(shuriken);

                // Single edit emitter setup
                if (!usingMultiEdit)
                {
                    // Init CurveEditor before modules (they may add curves during construction)
                    m_ParticleSystemCurveEditor = new ParticleSystemCurveEditor();
                    m_ParticleSystemCurveEditor.Init();

                    int numEmitters = shurikens.Length;
                    if (numEmitters > 0)
                    {
                        m_Emitters = new ParticleSystemUI[numEmitters];

                        for (int i = 0; i < numEmitters; ++i)
                        {
                            m_Emitters[i] = new ParticleSystemUI();
                            m_Emitters[i].Init(this, new ParticleSystem[] { shurikens[i] });
                        }

                        m_EmittersActiveInHierarchy = shuriken.gameObject.activeInHierarchy;
                    }
                }
            }

            if (initializeRequired)
            {
                // Multi-edit emitter setup
                if (usingMultiEdit)
                {
                    // Init CurveEditor before modules (they may add curves during construction)
                    m_ParticleSystemCurveEditor = new ParticleSystemCurveEditor();
                    m_ParticleSystemCurveEditor.Init();

                    int numEmitters = m_SelectedParticleSystems.Count;
                    if (numEmitters > 0)
                    {
                        m_Emitters    = new ParticleSystemUI[1];
                        m_Emitters[0] = new ParticleSystemUI();
                        m_Emitters[0].Init(this, m_SelectedParticleSystems.ToArray());
                        m_EmittersActiveInHierarchy = m_SelectedParticleSystems[0].gameObject.activeInHierarchy;
                    }
                }

                // Allow modules to validate their state (the user can have moved emitters around in the hierarchy)
                foreach (ParticleSystemUI e in m_Emitters)
                {
                    foreach (ModuleUI m in e.m_Modules)
                    {
                        if (m != null)
                        {
                            m.Validate();
                        }
                    }
                }

                // Sync to state
                if (GetAllModulesVisible())
                {
                    SetAllModulesVisible(true);
                }

                m_EmitterAreaWidth      = EditorPrefs.GetFloat("ParticleSystemEmitterAreaWidth", k_MinEmitterAreaSize.x);
                m_CurveEditorAreaHeight = EditorPrefs.GetFloat("ParticleSystemCurveEditorAreaHeight", k_MinCurveAreaSize.y);

                // For now only allow ShowOnlySelectedMode for ParticleSystemWindow
                SetShowOnlySelectedMode((m_Owner is ParticleSystemWindow) ? SessionState.GetBool(k_ShowSelectedId + mainSystem.GetInstanceID(), false) : false);

                m_EmitterAreaScrollPos.x = SessionState.GetFloat("CurrentEmitterAreaScroll", 0.0f);

                if (ShouldManagePlaybackState(mainSystem))
                {
                    // Restore lastPlayBackTime if available in session cache
                    Vector3 simulationState = SessionState.GetVector3(k_SimulationStateId + mainSystem.GetInstanceID(), Vector3.zero);
                    if (mainSystem.GetInstanceID() == (int)simulationState.x)
                    {
                        float lastPlayBackTime = simulationState.z;
                        if (lastPlayBackTime > 0f)
                        {
                            ParticleSystemEditorUtils.playbackTime = lastPlayBackTime;
                            ParticleSystemEditorUtils.PerformCompleteResimulation();
                        }
                    }

                    // Play when selecting a new particle effect
                    if (m_MainPlaybackSystem != mainSystem)
                    {
                        Play();
                    }
                }
            }

            m_MainPlaybackSystem = mainSystem;

            return(initializeRequired);
        }