Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by AlucardJay · Nov 08, 2012 at 12:33 PM · shadermaterialrendererfindmenuitem

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


Comment
Add comment · Show 3
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
avatar image AlucardJay · Nov 09, 2012 at 05:42 AM 0
Share

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?

avatar image Seth-Bergman · Nov 09, 2012 at 05:50 AM 0
Share

http://forum.unity3d.com/threads/10396-propper-way-to-change-GuiText-color-in-edit-mode

maybe this helps

http://answers.unity3d.com/questions/283271/material-leak-in-editor.html

avatar image AlucardJay · Nov 10, 2012 at 01:44 PM 0
Share

$$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 =]

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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...)

Comment
Add comment · Show 4 · Share
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
avatar image AlucardJay · Nov 14, 2012 at 06:02 AM 0
Share

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!

avatar image Seth-Bergman · Nov 14, 2012 at 06:11 AM 0
Share

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?)

avatar image AlucardJay · Nov 14, 2012 at 06:17 AM 0
Share

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 =]

avatar image AlucardJay · Nov 14, 2012 at 07:32 AM 0
Share

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

avatar image
0

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 :)

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

11 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

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


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