- Home /
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 =]
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
Really? Nobody? This is probably the easiest thing I have asked, and nothing to do with meshes!
I have seen this done, but have lost the link. I even saw a script where someone actually wrote a shader (in string format format from memory) in the JS, then applied it to the renderer, but I cannot find that now either.
Please, it is really quite simple, create gameObject, add components, assign the Default-Particle material. Anyone?
$$anonymous$$any thanks for the comments. I need to look into those links regarding instantiating materials in edit mode, so thankyou.
But my problem is slightly different here. I tend to go on so I have edited my question hoping it clarifies what I am trying to say!
In this case, I wish to find the material Default-Particle and then assign it to the gameObject. But many thanks again for the other material links =]
Answer by Seth-Bergman · Nov 14, 2012 at 05:49 AM
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...)
I personally really get annoyed when people just write back it doesn't work, but as I am beyond my realm of knowledge I have no choice, sorry.
var temp$$anonymous$$aterial = new $$anonymous$$aterial( go.renderer.shared$$anonymous$$aterial );
throws this error :
NullReferenceException UnityEngine.$$anonymous$$aterial..ctor (UnityEngine.$$anonymous$$aterial source) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/Graphics.cs:1125) CustomParticles.newParticle$$anonymous$$esh () (at Assets/Editor/CustomParticles.js:30)
I don't know if I am approaching this wrong, or mixed everything up by trying to add the Particle shader insted of the Diffuse_Particle material which should have the shader as a preset.
For example, to create a primitive cube, the command is GameObject.CreatePrimitive(PrimitiveType.Cube);
There must be an in-built command to call the default_particle material in the same way ?! e.g. $$anonymous$$aterial.CreateDefault($$anonymous$$aterialType.Diffuse_Particle); Note this is made up!
you can create it via the shader constructor...
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.$$anonymous$$aterial.html
ok, how about this:
var myShader : Shader = Shader.Find("Particles/Alpha Blended Premultiply");
var temp$$anonymous$$aterial : $$anonymous$$aterial = new $$anonymous$$aterial(myShader);
etc...
(also, just curious, did you try with just go.renderer.material?)
You're a legend, I think that is the link I was referring to in my first comment I even saw a script where someone actually wrote a shader (in string format format from memory) in the JS, then applied it to the renderer.
Let me have a play-around and get back with my results. Just happy I have that link again, ( $$anonymous$$aterial.$$anonymous$$aterial - D'oh, no wonder I couldn't find it in the API! Didn't drill down to it, just renderer ). Thanks =]
Thanks again for sticking with this question and helping me out. I was able to finally assign a material with :
var color = Color.white;
var shaderText =
"Shader \"Alpha Additive\" {" +
"Properties { _Color (\"$$anonymous$$ain Color\", Color) = (1,1,1,0) }" +
"SubShader {" +
" Tags { \"Queue\" = \"Transparent\" }" +
" Pass {" +
" Blend One One ZWrite Off Color$$anonymous$$ask RGB" +
" $$anonymous$$aterial { Diffuse [_Color] Ambient [_Color] }" +
" Lighting On" +
" SetTexture [_Dummy] { combine primary double, primary }" +
" }" +
"}" +
"}";
go.renderer.shared$$anonymous$$aterial = new $$anonymous$$aterial( shaderText );
go.renderer.shared$$anonymous$$aterial.color = color;
But that is just using the example shader in the $$anonymous$$aterial.$$anonymous$$aterial API link. If I were to edit this for the shader I want, that is alot of code to put in (but doable).
Shader : http://answers.unity3d.com/questions/24533/disabling-fog-on-alpha-blended-premultiply.html : http://answers.unity3d.com/questions/283093/adding-colortint-to-premultiplied-alpha-blending-s.html
I am voting and accepting this answer to your credit, but have asked another question that follows the theme of this : http://answers.unity3d.com/questions/348041/list-of-all-default-unity-shaders.html
Answer by IMD · Nov 16, 2017 at 03:16 PM
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 :)
[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 :)
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Why is only the last Material in the Materials array on a Mesh Renderer used? 2 Answers
Unity 5.4 - SetVectorArray - how to read values back from MaterialPropertyBlock? 0 Answers
Select material of Canvas Renderer 0 Answers
Changing the color at runtime is causing my material to look very different. 1 Answer