How to find and assign Default-Particle material to a renderer

As I tend to go on and over-explain things, here is the short point version =]

  • I am writing a script =]
  • From a Menu Item I am creating a gameObject
  • To this gameObject I am adding : MeshFilter, “EllipsoidParticleEmitter”, ParticleAnimator, ParticleRenderer
  • To the ParticleRenderer, I wish to assign the material Default-Particle

How do I find and then assign the material Default-Particle to the ParticleRenderer from a Menu Item, not runtime. Thanks.


Here is my current script :

@MenuItem("Custom/Particles/Mesh Emitter")
static function newParticleMesh() 
{
	var go : GameObject = new GameObject( "LegacyParticleMesh" );
	go.transform.position = Vector3.zero;
	go.AddComponent( MeshFilter );
	go.AddComponent( "MeshParticleEmitter" ); // as ParticleEmitter;
	go.AddComponent( ParticleAnimator );
	go.AddComponent( ParticleRenderer );
	
	//go.renderer.material.shader = Shader.Find("Particles/Alpha Blended Premultiply");
	//go.renderer.sharedMaterial.shader = Shader.Find("Particles/Alpha Blended Premultiply");
}

Original Question :

How do I find and assign a native material and shader to a renderer? I am talking about the Default-Particle material that comes with Unity.

I am creating a custom menu item for legacy particle objects. Everything is fine except for when I want to apply Unitys native Default-Particle material.

when I use :

go.renderer.material.shader = Shader.Find("Particles/Alpha Blended Premultiply");

this is the error message :

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.
UnityEngine.Renderer:get_material()
CustomParticles:newParticleMesh() (at Assets/Editor/CustomParticles.js:27)

when I use :

go.renderer.sharedMaterial.shader = Shader.Find("Particles/Alpha Blended Premultiply");

this is the error message :

NullReferenceException
CustomParticles.newParticleMesh () (at Assets/Editor/CustomParticles.js:27)

I am trying to find and apply the Default-Particle texture with the default Alpha Blended Premultiply shader. How is the correct way to assign these to the particle renderer using script/code?

Like these ones =]

alt text

unity how to find and assign default particle material

here is the code :

#pragma strict

#if UNITY_EDITOR

// -- Custom / Particles --

@MenuItem("Custom/Particles/Ellipsoid Emitter")
static function newParticleEllipsoid() 
{
	var go : GameObject = new GameObject( "LegacyParticleEllipsoid" );
	go.transform.position = Vector3.zero;
	go.AddComponent( "EllipsoidParticleEmitter" );
	go.AddComponent( ParticleAnimator );
	go.AddComponent( ParticleRenderer );
}

@MenuItem("Custom/Particles/Mesh Emitter")
static function newParticleMesh() 
{
	var go : GameObject = new GameObject( "LegacyParticleMesh" );
	go.transform.position = Vector3.zero;
	go.AddComponent( MeshFilter );
	go.AddComponent( "MeshParticleEmitter" );
	go.AddComponent( ParticleAnimator );
	go.AddComponent( ParticleRenderer );
	
	go.renderer.sharedMaterial.shader = Shader.Find("Particles/Alpha Blended Premultiply");
}

#endif

Try it like this:

var tempMaterial = new Material(go.renderer.sharedMaterial);
tempMaterial.shader = Shader.Find("Particles/Alpha Blended Premultiply");
renderer.sharedMaterial = tempMaterial;

I’m pretty sure the point is you need to create your own instance of the Material to assign to the renderer… as Eric5h5 explained in one of those links, an instance is always created behind the scenes when you do this either way… BUT in editor mode, this instance would not be implicitly DESTROYED, hence the error… in the above example (as adjusted from the second link in comment), we create our own instance instead… So no need to implicitly create one (which would then not be cleaned up, in other words “leaked” into the scene)

at least I THINK that’s right…

(not really sure if/why sharedMaterial would be preferred though, seems to me renderer.material should be ok…)

Hello! I’m resurrecting this post, because I couldn’t find an up-to-date answer on accessing Unity’s Default-Particle material at runtime. The reason I needed this material was because I wanted to create a new Particle System at runtime which when created unfortunately contains an empty / null material slot on the renderer. I needed to set this to reference the Default-Particle material by default (like what happens if adding a ParticleSystem via the Unity Editor) .

To achieve this I’ve created the Menu tool below which you can drop into any editor script which will clone the Default-Particle material from an editor created particle system in your scene and save it as a new asset for reference in your runtime code. Hope it helps some people :slight_smile:

[MenuItem("Tools/Clone Default Particle System Material",false, 100)]
	static public void CloneParticleMaterial() {
		try {
			ParticleSystem particleSys = GameObject.FindObjectOfType<ParticleSystem>();
			ParticleSystemRenderer pr = particleSys.gameObject.GetComponent<ParticleSystemRenderer>();
			Material m = new Material(pr.sharedMaterial);
			string uPath = AssetDatabase.GenerateUniqueAssetPath("Assets/Default-Particle(Clone).mat");
			AssetDatabase.CreateAsset(m, uPath);
			AssetDatabase.SaveAssets();
			Selection.activeObject = m;
		} catch {
			EditorUtility.DisplayDialog("Unable to find Default-Particle material in scene", "Please add a new particle system component to a gameobject in the scene before running this tool.", "Ok");
		}
	}

Cheers,
Isaac :slight_smile: