• 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 TheOrangeMan · Apr 01, 2013 at 09:39 PM · editormaterialinspectorcustom editor

Edit chosen material in the inspector for custom editor.

So when an object has a mesh renderer with a material selected, you can edit the material in the same inspector window. Is there anyway to do this with a custom editor?

Comment
Add comment · Show 2
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 numberkruncher · Apr 01, 2013 at 10:25 PM 0
Share

You can create enumerate the properties of a material to present a custom user interface similar to the default Unity one. You will need to be using Unity Pro in order to render a real-time preview of the material in your editor interface since the indie version cannot do this properly due to restrictions.

avatar image TheOrangeMan · Apr 01, 2013 at 10:56 PM 0
Share

Currently, I'm just creating an editor for the desired material (Editor::CreateEditor) and drawing that. This almost works, but it leaves out the preview bar. If the only option's to create that by hand though, I'll probably just leave it how it is.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Xaurrien · May 28, 2015 at 01:29 PM

Hi, I know this topic is old but I think I have a nice way to achieve this with shader selection. I use a MaterialEditor with the following methods: MaterialEditor.DrawHeader () and MaterialEditor.OnInspectorGUI ().

Example code:

 using UnityEditor;
 
 [CustomEditor(typeof(MyScript))]
 public class MyScriptEditor : Editor
 {
     private MyScript _myScript;
 
     // We need to use and to call an instnace of the default MaterialEditor
     private MaterialEditor _materialEditor; 
 
     void OnEnable ()
     {
         _myScript = (MyScript)target;
 
         if (_myScript.material != null) {
             // Create an instance of the default MaterialEditor
             _materialEditor = (MaterialEditor)CreateEditor (_myScript.material);
         }
     }
 
     public override void OnInspectorGUI ()
     {
         EditorGUI.BeginChangeCheck ();
 
         // Draw the material field of MyScript
         EditorGUILayout.PropertyField (serializedObject.FindProperty ("material"));
 
         if (EditorGUI.EndChangeCheck ()) {
             serializedObject.ApplyModifiedProperties (); 
 
             if (_materialEditor != null) {
                 // Free the memory used by the previous MaterialEditor
                 DestroyImmediate (_materialEditor);
             }
 
             if (_myScript.material != null) {
                 // Create a new instance of the default MaterialEditor
                 _materialEditor = (MaterialEditor)CreateEditor (_myScript.material);
 
             }
         }
 
 
         if (_materialEditor != null) {
             // Draw the material's foldout and the material shader field
             // Required to call _materialEditor.OnInspectorGUI ();
             _materialEditor.DrawHeader (); 
         
             //  We need to prevent the user to edit Unity default materials
             bool isDefaultMaterial = !AssetDatabase.GetAssetPath (_myScript.material).StartsWith ("Assets");
 
             using (new EditorGUI.DisabledGroupScope(isDefaultMaterial)) {
 
                 // Draw the material properties
                 // Works only if the foldout of _materialEditor.DrawHeader () is open
                 _materialEditor.OnInspectorGUI (); 
             }
         }
     }
 
     void OnDisable ()
     {
         if (_materialEditor != null) {
             // Free the memory used by default MaterialEditor
             DestroyImmediate (_materialEditor);
         }
     }
 }

Comment
Add comment · Show 1 · 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 TobiasB · Mar 16, 2017 at 01:07 PM 0
Share

Just what I needed! Thanks a bunch! :)

avatar image
0

Answer by numberkruncher · Apr 01, 2013 at 11:12 PM

With the new Unity 4 Editor.CreateEditor class you can in fact render a preview interface as well. Here is a simple demonstration:

 using UnityEngine;
 using UnityEditor;

 class MaterialEditorWindow : EditorWindow {
 
     [MenuItem("Window/Material Editor Test")]
     static void ShowWindow() {
         GetWindow<MaterialEditorWindow>("Material Editor Test");
     }
 
     Material mat;
     Editor matEditor;
 
     void OnGUI() {
         mat = EditorGUILayout.ObjectField(mat, typeof(Material)) as Material;
         if (mat != null) {
             if (matEditor == null)
                 matEditor = Editor.CreateEditor(mat);
 
             matEditor.OnPreviewGUI(GUILayoutUtility.GetRect(200, 300), EditorStyles.whiteLabel);
         }
     }
 
 }
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 TheOrangeMan · Apr 01, 2013 at 11:23 PM 0
Share

Sorry, I'm actually talking about editing the properties of the material, not just previewing it. Currently, I'm using CreateEditor and then calling OnInspectorGUI with the material editor. However, this leaves out the top bar that appears for built in components.

avatar image numberkruncher · Apr 01, 2013 at 11:25 PM 0
Share

EditorGUILayout.InspectorTitlebar? http://docs.unity3d.com/Documentation/ScriptReference/EditorGUILayout.InspectorTitlebar.html

avatar image numberkruncher · Apr 01, 2013 at 11:26 PM 0
Share

Though this doesn't include shader selection.

avatar image TheOrangeMan · Apr 02, 2013 at 01:07 AM 0
Share

Gave it a shot. It's better than nothing at all, but as you said, there's unfortunately no shader selection.

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

14 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

Related Questions

How could I merge 2 components' inspectors through a custom editor? 2 Answers

Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 Answers

Change the color of a material for only one object 7 Answers

How to set a shared material in inspector? 0 Answers

Why is my unity editor script not saving changes to the actual script? 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