Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MikePage_Artrix · Jun 28, 2017 at 11:50 PM · inspectorassets

Custom inspector to get asset path

Hi all,

I've done a fair bit of browsing and I've got a solution to this but it seems very very messy.

I have a list of strings in my scriptableobject.

I want to write an inspector that allows me to drag an asset into a field and save them asset path into this list. The list should be populated in OnEnable() with object references that match the current asset path strings in the list.

I've got it so I can create ObjectFields that populate temporary Objects and I can drop an asset into this field. I use GUI.Changed when this happens and I can save the asset path of the object into the ScriptableObject's serialisedproperty.

However I'm having real trouble recreating the list of asset references in the ObjectFields in the OnEnable of my custom editor. Also I'm not sure if this is the best approach for something like this.

The simple goal is to be able to drag an asset into a field, save the path of that asset and then load that asset during runtime via Resources.Load.

I just want to save designers having to type in the paths.

Code from my custom editor:

 public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
 
         AssetStreamerData prefabStreamer = (AssetStreamerData)target;
         SerializedProperty property = serializedObject.FindProperty("AssetResourcePaths");
 
         if (property == null)
             return;
 
          if (GUILayout.Button(new GUIContent("+", "Create Set"), EditorStyles.miniButtonLeft, GUILayout.Width(20)))
         {
             AssetObjects.Add(null);
         }
 
         for (int index = 0; index < AssetObjects.Count; index++)
         {
             AssetObjects[index] = EditorGUILayout.ObjectField("Asset", AssetObjects[index], typeof(Object), false);
         }
 
         if(GUI.changed)
         {
             for (int index = 0; index < AssetObjects.Count; index++)
             {
                 if (AssetObjects[index] == null)
                     continue;
 
                 if (index >= property.arraySize || property.arraySize == 0)
                 {
                     property.InsertArrayElementAtIndex(0);
                     SerializedProperty stringProperty = property.GetArrayElementAtIndex(0);
                     stringProperty.stringValue = AssetDatabase.GetAssetPath(AssetObjects[index]);
                 }
                 else
                 {
                     SerializedProperty stringProperty = property.GetArrayElementAtIndex(index);
                     stringProperty.stringValue = AssetDatabase.GetAssetPath(AssetObjects[index]);
                 }
             }
         }
 
         serializedObject.ApplyModifiedProperties();
     }


Thanks,

Mike.

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

0 Replies

· Add your reply
  • Sort: 

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

108 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 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 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 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 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 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

How to reference custom assets from Inspector or Script 0 Answers

ScriptableObjects corrupted? Reaching "uneditable/unreferenceable" state in Inspector 1 Answer

I can't seem to find my project's assets? 0 Answers

Shader problems everything is pink 0 Answers

Needed objects destroyed after deserialization 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