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
3
Question by numberkruncher · Jun 09, 2012 at 02:30 AM · editorassetscriptableobjectcustomeditor

Problem with ScriptableObject and Custom Editor

I have written an editor script that generates a custom .asset file. The asset file is created using a ScriptableObject and then sub-assets are added using AssetDatabase.AddObjectToAsset.

I would like to add an inspector GUI that is shown when the asset file is selected. For some reason the inspector GUI is not showing up when the asset file is selected. Instead I have to expand the asset file in project panel and then select the the same asset name again.

alt text

 [CustomEditor(typeof(TestAsset))]
 public class TestAssetEditor : UnityEditor.Editor {
 
     public override void OnInspectorGUI() {
         if (GUILayout.Button("Test"))
             Debug.Log("TEST");
     }
 
     [MenuItem("Test/Test")]
     public static void Test() {
         Object asset = ScriptableObject.CreateInstance<TestAsset>();
         asset.name = "Test Name";
         AssetDatabase.CreateAsset(asset, "Assets/Test.asset");
 
         Mesh test = new Mesh();
         test.name = "test_mesh";
         test.vertices = new Vector3[] {
             new Vector3(0, 0, 0),
             new Vector3(0, 1, 0),
             new Vector3(1, 1, 0)
         };
         test.uv = new Vector2[] {
             new Vector2(0, 0),
             new Vector2(0, 1),
             new Vector2(1, 1)
         };
         test.triangles = new int[] { 0, 1, 2 };
 
         test.RecalculateNormals();

         AssetDatabase.AddObjectToAsset(test, asset);

         AssetDatabase.ImportAsset("Assets/Test.asset");
     }
 
 }
 
 [System.Serializable]
 public class TestAsset : ScriptableObject {
 
     public int test = 0;
 
     
 
 }


screeny.png (25.9 kB)
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 numberkruncher · Jun 15, 2012 at 02:08 AM 0
Share

It seems as though this is not possible...

avatar image numberkruncher · Jun 15, 2012 at 02:21 AM 0
Share

Is there a way to specify the mainAsset (http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.Is$$anonymous$$ainAsset.html) ?

avatar image numberkruncher · Jun 15, 2012 at 04:16 PM 0
Share

I have just had a go at creating a ".prefab" and attaching a custom $$anonymous$$onoBehaviour with my custom editor and that works fine. From trial-and-error it would also appear that AssetDatabase.AddObjectToAsset works fine with this.

Is this advisable?

The documentation states that AssetDatabase.AddObjectToAsset should only be used with ".asset" files because "imported models or texture assets for example will lose their data". I can understand that custom data will be lost when texture/model is re-imported. But surely ".prefab" would be just as safe as ".asset" right?

If this is a safe option, then this will solve my problem :-)

2 Replies

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

Answer by numberkruncher · Jul 01, 2012 at 02:43 PM

After an e-mail conversation with Unity support it appears that this is in fact a bug with Unity.

Whilst there are no immediate side effects to implanting assets into a ".prefab" file this is not recommended because other Unity functionality might assume that a prefab file only contains a prefab. Additionally future changes to the way in which prefabs are stored might cause added assets to become corrupt or lost.

So until this bug gets fixed the only solution is to expand the asset file and select the ScriptableObject. Not perfect but this is at least a workable solution for now.

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 Tenebrous · Feb 26, 2014 at 10:59 AM

Actually, there is a "workaround" for this. Create an inspector which works on Object - in the OnEnable see if the target is a valid asset, and use LoadAllAssetRepresentationsAtPath to find the one you're interested in, and then call the inspector GUI for your one in the OnInspectorGUI.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

ScriptableObject Asset corrupted after reload of Unity 1 Answer

Are all ScriptableObject assets included in the build? 4 Answers

Is there a message to be listen to when editor enter play mode? 2 Answers

Save ScriptableObject On Unity Application Quit? 1 Answer

How can I find all instances of a Scriptable Object in the Project (Editor) 3 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