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
0
Question by DuskLight-Studios · May 01, 2020 at 09:45 AM · editorserializationscriptableobjectrenameproject-panel

No way to rename Sub Assets properly

I'm trying to rename a Scriptable Object, which is a sub asset within another Scriptable Object.

The eventual goal is to create a localization index which contains a list of keys. I want each key to become a referenceable Scriptable Object under the index SO automatically.

The name of the sub assets must become the key that was entered in the list on the main asset. I have this working by doing this:

 for (int o = 0; o < keyObjects.arraySize; o++) {
   SerializedObject keyObj = new SerializedObject(keyObjects.GetArrayElementAtIndex(o).objectReferenceValue);
   SerializedProperty objKey = keyObj.FindProperty("key");
   SerializedProperty objIndex = keyObj.FindProperty("index");
   SerializedProperty objName = keyObj.FindProperty("m_Name");
 
   string useKey = keys.GetArrayElementAtIndex(objIndex.intValue).stringValue;
   objKey.stringValue = useKey;
   objName.stringValue = objIndex.intValue + "-" + useKey;
 
   keyObj.ApplyModifiedProperties();
   AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(keyObj.targetObject), ImportAssetOptions.ForceUpdate);
 }

The main problem is that the Project View is not updated properly. The sub assets are renamed, but it doesn't show until you collapse and re-expand the main SO object. I've tried many different methods to try getting it to work, with no success. Reimporting the asset doesn't work, refreshing the AssetDatabase doesn't work, repainting the Project View doesn't work.

The method AssetDatabase.Rename works for the main asset. If I use it for the sub asset, everything breaks. I've tried setting the sub asset as the Main Asset temporarily, rename it, then make the original main asset the main asset again, but this just breaks down entirely with weird errors.

Is there any legit way to rename Sub Assets? And if not, what went wrong in the development of these features? This sounds like a relatively easy feature to add.

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
0
Best Answer

Answer by DuskLight-Studios · May 01, 2020 at 08:25 PM

There is a way. Using the above method to rename the sub asset, you then need to: - Add a new sub asset - AssetDatabase.Refresh(); - Remove the newly added sub asset.

That is the only way to force a repaint of the project view. Which, to be honest, is ridiculous.

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
0

Answer by ModLunar · Apr 11 at 03:11 AM

So you can rename the sub-asset by using its .name property.

This WILL work internally, but its result won't show in the Project Windows until you forcibly repaint them.

They don't have an easy way to do this, so I used the following silly code that works for me in Unity 2020.3.32f1:

 /// <summary>
 /// Forces Unity to repaint the project windows. This seems to be the only solution that works after renaming a sub-asset.
 /// The following do NOT work:
 /// <list type="bullet">
 /// <item><see cref="AssetDatabase.Refresh"/></item>
 /// <item><see cref="AssetDatabase.SaveAssets"/></item>
 /// <item><see cref="UnityEditor.Editor.Repaint"/></item>
 /// </list>
 /// </summary>
 private static void ForceUpdateProjectWindows() {
     string tempAssetPath = "Assets/temp-987654321.anim";
     AssetDatabase.CreateAsset(new AnimationClip(), tempAssetPath);
     AssetDatabase.DeleteAsset(tempAssetPath);
 }
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

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

Why does the value of the public property of [Serializable] class persist in ScriptableObject between Editor runs ? 2 Answers

RenameAsset does not change the name of asset in Project Window in Awake() or OnEnable() 1 Answer

Cannot serialize System.Type field 3 Answers

Scriptable Object resets after Unity is closed 2 Answers

How should I serialize data that is also editable in the Inspector? 2 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