• 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
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

People who like this

0 Show 0
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

changing the radius of a particle system with unity script 1 Answer

Moving particle system via script causes choppy movement in build but works fine in pay mode 1 Answer

Moving a singular particle to the cursor 0 Answers

How to prevent particle being to clipping objects in URP 0 Answers

I have a particle system that is playing at the wrong time... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges