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
6
Question by Soviut · Jan 30, 2013 at 11:59 AM · editormeshrenderereditorscript

How do I apply Default-Diffuse material to a MeshRenderer in code?

As part of a custom mesh script, in edit mode, I have a menu item that creates an empty game object to host the custom mesh component as well as adds a MeshFilter and a MeshRenderer.

 GameObject go = new GameObject();
 go.name = "Expandable Plane";
 go.AddComponent("MeshFilter");
 go.AddComponent("MeshRenderer");

However, the MeshRenderer has no materials or sharedMaterials set on it. When I manually create primitives like cubes or spheres, the MeshRenderer automatically has the Default-Diffuse material applied. I'd like to apply this to my MeshRenderer, but am struggling to actually find the material using an Editor script.

 GameObject.Find("Default-Diffuse");

Turns up null, so does:

 Shader.Find("Default-Diffuse");

I know there's a "Diffuse" shader that I could make a new material instance of, but I want to use the Default-Diffuse material as a sharedMaterial to avoid leaks.

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

6 Replies

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

Answer by Soviut · Jan 30, 2013 at 11:40 PM

My current hack solution is to temporarily create another primitive (plane, cube, whatever) and get the Default-Diffuse material from its sharedMaterial and apply it to my new object.

 GameObject primitive = GameObject.CreatePrimitive(PrimitiveType.Plane);
 primitive.active = false;
 Material diffuse = primitive.GetComponent<MeshRenderer>().sharedMaterial;
 DestroyImmediate(primitive);
 // ...
 go.renderer.sharedMaterial = diffuse;
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
avatar image
16
Wiki

Answer by sirbrialliance · Sep 02, 2013 at 08:03 PM

Here's a simple snippet:

 //set the default material
 MeshRenderer mr = go.GetComponent<MeshRenderer>();
 mr.material = new Material(Shader.Find("Diffuse"));
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
avatar image
14

Answer by numberkruncher · Feb 17, 2015 at 03:07 AM

The following undocumented function seems to work in editor scripts:

 var mat = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Diffuse.mat");
Comment
Add comment · Show 2 · 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 JeromeJ · Jan 13, 2018 at 01:19 PM 0
Share

Sadly relies on Unity.Editor (thus can't be used in a build). EDIT: Which OP doesn't care but others readers might. Workaround solution: Set it on Reset :)

avatar image NathanJSmith · Oct 11, 2018 at 01:26 AM 0
Share

[+1] - This is just what I need, it's great for assigning run-time debug object in the scene. Thanks :D

avatar image
2

Answer by Acegikmo · Oct 23, 2014 at 12:12 PM

Late answer, but, here's a way of getting it without creating a default primitive :)

 // using System.Reflection;
 // using UnityEditor;
 MethodInfo getBuiltinExtraResourcesMethod;
 Material GetDefaultMaterial() {
     if( getBuiltinExtraResourcesMethod == null ) {
         BindingFlags bfs = BindingFlags.NonPublic | BindingFlags.Static;
         getBuiltinExtraResourcesMethod = typeof( EditorGUIUtility ).GetMethod( "GetBuiltinExtraResource", bfs );
     }
     return (Material)getBuiltinExtraResourcesMethod.Invoke( null, new object[] { typeof( Material ), "Default-Diffuse.mat" } );
 }
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
avatar image
1

Answer by Berenger · Jan 30, 2013 at 12:17 PM

Try that :

 Material diffuse = new Material
 (
     "Shader \"Tutorial/Basic\" {\n"
     +"    Properties {\n"
     +"        _Color (\"Main Color\", Color) = (1,1,1,1)\n"
     +"    }\n"
     +"    SubShader {\n"
     +"        Pass {\n"
     +"            Material {\n"
     +"                Diffuse [_Color]\n"
     +"            }\n"
     +"        Lighting On\n"
     +"        }\n"
     +"    }\n"
     +"}"
 );
 
 renderer.sharedMaterial = diffuse;

More infos : http://docs.unity3d.com/Documentation/ScriptReference/Material.Material.html

Comment
Add comment · Show 6 · 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 Soviut · Jan 30, 2013 at 12:39 PM 0
Share

I'm not interested in creating a new shader or material, I just want to find and use the Default-Diffuse material that can be found in the Assets list in the material picker.

avatar image Berenger · Jan 30, 2013 at 12:55 PM 0
Share

I don't think you can access those.

avatar image Soviut · Jan 30, 2013 at 01:08 PM 0
Share

I can manually use choose the Default-Diffuse shader in the editor, it would make sense that I could do the same through code.

avatar image Berenger · Jan 30, 2013 at 01:19 PM 1
Share

You could use a GameObject that specifically hold that material. Create a script with a public $$anonymous$$aterial, affect it on a GameObject and set the default diffuse mat. Then, anywhere in your codes, you can find that GameObject, get the component and access the material.

A clean way would be better obviously, but that's all I got :/

avatar image jogo13 · Jan 30, 2013 at 04:23 PM 0
Share

Unity is very efficient and when building only exports the materials that are used in a scene. So you need to add a $$anonymous$$aterial variable somewhere in code to a script, or just attached to a gameobject like the comment above. I personaly create an script class that just holds various materials that need to be dynamically changed during the game. If you had alot of materials (and textures in those materials) this method could be wasteful during runtime.

Show more comments
  • 1
  • 2
  • ›

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

[Unity Editor] Emulate Touch Input - Still Asking 12/10 1 Answer

Object2Terrain error/wont work. 1 Answer

Is there a function like Start for starting up unity? 1 Answer

Why do I need to clic apply after editing a list content via editor script to access the list content in playmode 0 Answers


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