Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 reincarnationofunity · Jun 29, 2017 at 10:31 PM · scriptableobjectcustom-inspector

How to tie Scriptable Object's file name with a "name" field in a Scriptable Object?

I just learned about ScriptableObjects and I think they're great. But after working for a while, I realized Unity could use a lot of things to streamline the workflow. As for now, I want to manually write code that will help me along.

I want to be able to say Assets -> Create -> ScriptableObjectName and have that Asset's filename be automatically highlighted in my inspector, the same way creating a new C# script works. Then I want to type in my name (just like renaming something). Optional: when I hit enter, the name I entered in the inspector will also update a field in the ScriptableObject called "name."

Or, I want to be able to input a name field in an already created ScriptableObject asset and when I hit enter in the inspector, I want the filename of the ScriptableObject asset to change to match my name field.

If anyone knows of any way to achieve at least one of the above, I would be a happy camper. ;)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by H1ddeNGames · Dec 17, 2018 at 09:13 AM

I know this question is over a year old but whoever finds this might be helped by this solution.


If you want to create an asset by right clicking on your project folder or going into the Assets Menu > Create then do this:

 [CreateAssetMenu(fileName = "New Item", menuName = "H1ddenGames/ItemSystem/Item")]
         public class Item : ScriptableObject



When you make it using this method, it should automatically highlight the name of the new ScriptableObject file so you can rename as you wish.

I couldn't exactly figure out how to change the name field of the ScriptableObject upon pressing enter but I did do it this way in another script.


 string assetPath = AssetDatabase.GetAssetPath(item.GetInstanceID());
                         item.ItemIdentifiers.ItemName = Path.GetFileNameWithoutExtension(assetPath);



Here "item" is the Scriptable Object file that I have in this type of folder structure:

= Scriptable Object = = Resources = = = Items = = = = Armor

Please note that Scriptable Object is the top level folder, then Resources is inside Scriptable Object, then Items is inside Resources... etc.


Placing the above code into a method like the one below will allow you to press the context wheel in the inspector of the script you place this code snippet into and press "FileNameToNameField" option that will run even outside of play mode.

 [ContextMenu("FileNameToNameField")]
             public void FileNameToNameField()
             {
                 if(ArmorList.Count > 0)
                 {
                     foreach (var item in ArmorList)
                     {
                         string assetPath = AssetDatabase.GetAssetPath(item.GetInstanceID());
                         item.ItemIdentifiers.ItemName = Path.GetFileNameWithoutExtension(assetPath);
                     }
                 }
 }



Hope that helped. If it doesn't make sense try to look at my project that implements this idea here: https://github.com/h1ddengames/Item-System

I have several other projects where I try to do unconventional things like what you're tying to do here with this question so feel free to take a look and get ideas.

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 Draco18s · Oct 08, 2021 at 01:25 AM 1
Share

You can use OnValidate for this.

 #if UNITY_EDITOR
         void OnValidate() {
             string assetPath = AssetDatabase.GetAssetPath(this.GetInstanceID());
             displayName = Path.GetFileNameWithoutExtension(assetPath);
         }
     }
 #endif
avatar image
1

Answer by aurorasunrisegames · Jun 29, 2021 at 07:45 AM

I was looking for a similar answer and try the comment below, than do few minutes research and here it is, more simplier issue. Here is my thanks to you, @H1ddeNGames

This script will change the name value after you edit any other values of SO.

using System.IO; using UnityEditor; using UnityEngine;

[CreateAssetMenu(fileName = "newCell", menuName = "New Cell")] public class Cell : ScriptableObject { public new string name; public Sprite sprite; public Color32 backColor = Color.white;

 private void OnValidate()
 {
     name = Path.GetFileNameWithoutExtension( AssetDatabase.GetAssetPath(this));
 }

}

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

70 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

Related Questions

Custom inspector for scriptable object resets object 2 Answers

How should I serialize data that is also editable in the Inspector? 2 Answers

Expose nested ScriptableObject fields in array, custom inspector 0 Answers

Deep Copy of ScriptableObject 1 Answer

Custom Inspector for ScriptableObject 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