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
1
Question by Simon Says · Aug 13, 2013 at 09:33 PM · inspectorassetscustomeditor

CustomEditor for an asset group -- is it possible?

Ok, this is a hard one. I've been searching the web for an answer for two days and haven't found anything. Here we go...

Suppose I have a group of assets created like this:

 var customScript1 = ScriptableObject.CreateInstance<CustomScript1>();
 var customScript2 = ScriptableObject.CreateInstance<CustomScript2>();
 AssetDatabase.CreateAsset(customScript1, "Assets/");
 AssetDatabase.AddObjectToAsset(customScript2, customScript1);

Now, the produced (combined) asset should be akin to this (as seen in the project view):

 - root (asset?)
   + custom script 1
   + custom script 2

Can I attach a CustomEditor to the root to show in the Inspector?

Comment
Add comment · Show 4
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 Jamora · Aug 14, 2013 at 09:22 PM 0
Share

What do you want to show in the inspector? Do you want it to look like the inspector for a GameObject with the different Components etc. ?

avatar image Simon Says · Aug 14, 2013 at 09:28 PM 0
Share

A few serialized fields from one of the scripts. Why? Do you know of a way to attach a custom editor to the root?

avatar image bblanque · Jun 15, 2015 at 03:59 AM 0
Share

This is very useful! also look at Unite 2014 - The Hack Spectrum: Tips, Tricks and Hacks for Unity.

avatar image Simon Says · Jun 15, 2015 at 06:03 AM 1
Share

Thanks. Note that it changed slightly in Unity 5. It's no longer [CustomEditor(typeof(Object))], it's [CustomEditor(typeof(DefaultAsset))] ins$$anonymous$$d.

1 Reply

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

Answer by Jamora · Aug 14, 2013 at 09:46 PM

It is possible to show custom editors for child assets in a combined asset like what you describe. My script has a few assumptions:

  • You need a custom editor script for each (my script assumes it is classname+"Editor").

  • You need to have your combined asset beginning with "custom".

If you want to make them look like what Unity does with GameObjects, you'll need at least the default inspector titlebar.

This is basically what I came up with:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(Object))]
 public class testEditor : Editor {
     
     Object[] targetArray;
     
     void OnEnable(){
         if(targetArray == null)
             targetArray = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(target));
     }
     
     public override void OnInspectorGUI(){
         /*this can be left out but will cause this to be urn on everything you select in the hierarchy.
          * Right now it only runs on things that begin with custom.
          */
         if(target.name.StartsWith("custom")){
             
             foreach(ScriptableObject obj in targetArray){
                 //Show the custom editors for all the ScriptableObjects that are within the target
                 CreateEditor(obj,System.Type.GetType(obj.GetType().ToString()+"Editor")).OnInspectorGUI();
             }
         }
     }
 }


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 Simon Says · Aug 14, 2013 at 10:08 PM 0
Share

I see. So basically you override the Inspector view for every object, which doesn't have a more specific CustomEditor defined. And then filter the asset in question by name. It's an interesting approach. I'll think on it. Thank you.

Just a small correction to your code though:

  if(target.name.StartsWith("custom")){
    ...
  }
  else {
    DrawDefaultInspector();
  }
avatar image Simon Says · Aug 15, 2013 at 09:45 PM 2
Share

Thanks a lot! You were very helpful.

Turns out [CustomEditor(typeof(UnityEngine.Object))] applies only to general assets such as combined assets, folders, scenes, and unrecognized custom assets, and nothing else (not even ScriptableObjects, even with CustomEditor(typeof(UnityEngine.Object), true)). So I could leave out the na$$anonymous$$g mumbo-jumbo and use this code for all unrecognized custom assets (which shows better info than a blank page anyway):

 [CustomEditor(typeof(UnityEngine.Object))]
 public class ObjectInspector : Editor
 {
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
         
         if (combinedAssets != null) {
             int i = 0;
             foreach (var asset in combinedAssets) {
                 if (asset != target) {
                     foldStates[i] = EditorGUILayout.InspectorTitlebar(foldStates[i], asset);
                     
                     if (foldStates[i]) {
                         var editor = Editor.CreateEditor(asset);
                         editor.OnInspectorGUI();
                     }
                 }
                 
                 ++i;
             }
         }
     }
     
     protected virtual void OnEnable()
     {        
         var assetPath = AssetDatabase.GetAssetPath(target);
         if ((assetPath != null) && (assetPath.EndsWith(".asset"))) {
             combinedAssets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
             
             if (combinedAssets != null) {
                 foldStates = new List<bool>(combinedAssets.Length);
                 for (int i = 0; i < combinedAssets.Length; ++i) {
                     foldStates.Add(true);
                 }
             }
         }
     }
     
     protected virtual void OnDisable() 
     {
         combinedAssets = null;
         foldStates = null;
     }
 
     Object[] combinedAssets;
     List<bool> foldStates;
 }

It appears that a default editor always exists for any asset.

avatar image Simon Says · Jun 15, 2015 at 06:03 AM 0
Share

Note that it changed slightly in Unity 5. It's no longer [CustomEditor(typeof(Object))], it's [CustomEditor(typeof(DefaultAsset))] ins$$anonymous$$d.

avatar image silentslack Simon Says · Feb 26, 2016 at 09:03 PM 0
Share

I couldn't get the custom editor firing on the parent asset no matter what I tried, either Object or DefaultAsset. Is there something I'm missing? I'm trying to get any kind of life from my custom editor when selecting the group asset's parent but nada.

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

Change variable in inspector depending on enum using struct 0 Answers

Inspector:Reference a component in Assets, not in scene. 0 Answers

How to add UnityEvent using Custom Inspector? 1 Answer

Custom Editor Window not displaying anything 1 Answer

Setting default import settings? 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