• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ivangarciafilho · Feb 24, 2020 at 08:16 PM · particlesparticlesystemparticle systembakingbake

ParticleSystem.Bake() not working with specific particle

Trying to figure out which of the modules or parameters are "crashing" my Particle System Baker Script...

That's the particle system causing a complete editor crash on unity 2018 to 2020 : https://drive.google.com/open?id=1doqDHu8OUU3geV6NJC6P1tp7-bzuZ1fk

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using System;
 using System.IO;
 
 public class ParticleSystemBaker {
     class ParticleIngredient {
         public Transform transform;
         public ParticleIngredient parent;
 
         public Material material;
         public Material trailsMaterial;
         public Mesh mesh;
         public Mesh trailsMesh;
 
         public GameObject generatedObject;
     }
 
     static int counterSaved = 0;
 
     static List < ParticleIngredient > ingredients = new List < ParticleIngredient > ();
 
     [MenuItem("CONTEXT/ParticleSystem/Bake Into Mesh")]
     static void BakePS(MenuCommand command) {
         ingredients.Clear();
 
         ParticleSystem ps = (ParticleSystem) command.context;
         Transform t = ps.transform;
         InvestigateTransform(t, true);
 
         GameObject NewObjectForMesh(Mesh m, Material mat) {
             GameObject newObject = new GameObject();
 
             MeshFilter meshFilter = newObject.AddComponent < MeshFilter > ();
             meshFilter.sharedMesh = m;
 
             MeshRenderer meshRenderer = newObject.AddComponent < MeshRenderer > ();
             meshRenderer.sharedMaterial = mat;
 
             return newObject;
         }
 
         string path = "";
         string key = counterSaved.ToString() + DateTime.Now.ToString("MMddyyyy_hhmmsstt"); {
             path = Application.dataPath + "/GeneratedMeshes/";
             if (!Directory.Exists(path)) Directory.CreateDirectory(path);
 
             path = "Assets/GeneratedMeshes";
         }
 
         string objPath = path + "/" + key + ".prefab";
         Debug.Log(objPath);
         GameObject wholeObj = new GameObject();
         GameObject p = PrefabUtility.SaveAsPrefabAssetAndConnect(wholeObj, objPath, InteractionMode.AutomatedAction);
 
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
 
         Dictionary < ParticleIngredient,
         Transform > transformByParticleIngredient = new Dictionary < ParticleIngredient,
         Transform > ();
 
         for (int i = 0; i < ingredients.Count; i++) {
             ParticleIngredient pi = ingredients[i];
 
             AssetDatabase.AddObjectToAsset(pi.mesh, AssetDatabase.GetAssetPath(p));
 
             GameObject obj = NewObjectForMesh(pi.mesh, pi.material);
             obj.transform.SetParent(wholeObj.transform);
 
             obj.name = "_Particles";
 
             //if(pi.parent != null)
             //    obj.transform.localPosition = pi.transform.localPosition;
 
             if (pi.trailsMesh != null) {
                 AssetDatabase.AddObjectToAsset(pi.trailsMesh, AssetDatabase.GetAssetPath(p));
                 GameObject trailObj = NewObjectForMesh(pi.trailsMesh, pi.trailsMaterial);
                 trailObj.name = "_Trails";
 
                 trailObj.transform.SetParent(obj.transform);
             }
 
             pi.generatedObject = obj;
             transformByParticleIngredient.Add(pi, obj.transform);
         }
 
         for (int i = 0; i < ingredients.Count; i++) {
             ParticleIngredient pi = ingredients[i];
             if (pi.parent != null) {
                 if (transformByParticleIngredient.ContainsKey(pi.parent)) {
                     pi.generatedObject.transform.SetParent(transformByParticleIngredient[pi.parent]);
                 }
             }
         }
 
         PrefabUtility.ApplyPrefabInstance(wholeObj, InteractionMode.AutomatedAction);
 
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
 
         GameObject.DestroyImmediate(wholeObj);
     }
 
     static Matrix4x4 scaleaMatrix =
 default(Matrix4x4);
 
     static ParticleIngredient InvestigateTransform(Transform t, bool first = false) {
         ParticleIngredient ingredient = null;
 
         ParticleSystemRenderer renderer = t.GetComponent < ParticleSystemRenderer > ();
         if (renderer) {
             ParticleSystem ps = t.GetComponent < ParticleSystem > ();
 
             ingredient = new ParticleIngredient();
             ingredient.transform = t;
 
             Mesh newMesh = new Mesh();
             newMesh.name = "_Mesh";
             renderer.BakeMesh(newMesh, true);
             ingredient.mesh = newMesh;
             ingredient.material = renderer.sharedMaterial;
 
             if (ps.trails.enabled) {
                 Mesh trailsMesh = new Mesh();
                 trailsMesh.name = "_TrailsMesh";
                 renderer.BakeTrailsMesh(trailsMesh, true);
                 ingredient.trailsMesh = trailsMesh;
                 ingredient.trailsMaterial = renderer.trailMaterial;
             }
 
             ingredients.Add(ingredient);
 
         }
 
         for (int i = 0; i < t.childCount; i++) {
             var childIngredient = InvestigateTransform(t.GetChild(i));
 
             if (ingredient != null && childIngredient != null) childIngredient.parent = ingredient;
         }
 
         return ingredient;
     }
 }
 




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some question about ParticleSystem.emit() rendering 0 Answers

Can I shoot a ray from a particle that hit a collider? 1 Answer

Why can I not add my own image to particle material? 1 Answer

Are there any disadvantages to using the legacy Ellipsoid Particle System in Unity 5 over Shuriken. 0 Answers

Particles disappear when the parent rotates,My particles disappear when the parent rotates 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges