Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
2
Question by savlon · Jul 01, 2015 at 06:08 AM · spriteassetchangenameprogrammatically

How to programmatically change name of sub-assets

I am trying to change the name of the sub-sprites of a sprite sheet. I have searched and tried everything I can think of and cannot get it to work, which makes me think it can't be done at this point in time.

Let's say I have a sprite sheet that has already been split up into individual sprites inside of Unity. Sub-sprite assets contained within Sprite sheet asset

I am attempting to change the sub-sprite names programmatically.

I tried using

 AssetDatabase.RenameAsset (...)

on the sprite sheet asset, which only changes the sprite sheet name and not sub-sprites.

I then obtained a sub-sprite and attempted to change its name using the below code.

             if (AssetDatabase.IsSubAsset (subSprite))
             {
                 AssetDatabase.RenameAsset (AssetDatabase.GetAssetPath (subSprite), "newSprite" + i.ToString ());
             }

However, this too only changes the sprite sheet name.

Renamed sprite sheet

I'm not sure where else I can take this to achieve my desired outcome, apart from programmatically copying the original sprite sheet and using that to create a copy. But even then, I still am unable to alter the sub-sprite names.

I also tried changing subSprite.name, but this just changes the internal name and not the asset name.

Any ideas?

screen-shot-2015-07-01-at-35917-pm.png (15.5 kB)
screen-shot-2015-07-01-at-34749-pm.png (15.3 kB)
Comment
Add comment · Show 1
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 savlon · Jul 01, 2015 at 08:45 PM 0
Share

Anyone know of a solution? Apart from manually doing it...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Carl-Gran · Apr 13, 2016 at 11:47 AM

try:

    AssetDatabase.ClearLabels(item);
    item.name = newName;
    AssetDatabase.SetLabels(item, new string[]{newName});
    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(item));

Where item is the object reference of your sub asset.

Comment
Add comment · Show 2 · 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 TDMJ · Mar 14, 2018 at 07:15 AM 0
Share

friend, it not work

avatar image PineTreeDev · Jan 18 at 12:02 PM 0
Share

If anyone stumbles across this old thread, this solution is working in unity 2020-2021 as far as I know, a lot simpler than the previous solution if you're working with Scriptable Objects with a lot of sub assets.

avatar image
1

Answer by ParkyRander · Aug 21, 2018 at 01:17 PM

             string path = AssetDatabase.GetAssetPath(mainAsset);
             UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(path);
             string prefix = string.Format("{0}_", mainAsset.name);
 
             for(int i = 0; i < assets.Length; i++)
             {
                 if (AssetDatabase.IsSubAsset(assets[i]))
                 {
                     if (assets[i].name.StartsWith(prefix) == false)
                     {
                         assets[i].name = prefix + assets[i].name;
                         EditorUtility.SetDirty(assets[i]);
                     }
                 }
             }
             EditorUtility.SetDirty(mainAsset);
             AssetDatabase.ImportAsset(path);


This worked for me, so long as you have access to the mainAsset.

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

25 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

Related Questions

cutout animation in top down view 2d 1 Answer

how do I change a 2D Sprite Character Image/Graphic to another Sprite Character Image/Graphic with the same movements, animation,mechanim etc. 2 Answers

changing sprites depending on int value 1 Answer

How to use Float as Name in GameObject.Find command? 1 Answer

Object sprite changes 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