- Home /
Change file name in a scriptable object
I am building asset bundles from a scriptable object and I would like to change the filename according to the build options I set in my scriptable object.
I've tried object.name = "newName" however that is does not change the filename.
Any pointers please? Thanks
If it's an object in Unity scene, this must work :http://docs.unity3d.com/Documentation/ScriptReference/Object-name.html
But if you really want to change the filename I think it's better to use C#/Unity functions like this : http://docs.unity3d.com/Documentation/ScriptReference/Path.GetFileName.html or System.IO.Path.GetFileNameWithoutExtension( m_goYourObject );
I don't really know if it's what you search or want :/
Object.name does not work on a scriptable object.
GetFileName does what it states, it gives you the file name only. I need to change that.
Yes I know but with some more lines extract from one of my C# script :
http://pastebin.com/pxh8$$anonymous$$qdm
And with some ideas from your $$anonymous$$d, you can do great things you know :) I hope this can help you
Answer by ZenithCode · Oct 30, 2012 at 10:36 AM
@liszto Thanks you got some ideas churning which helped me solve the problem.
Final solution is this:
string listingFilePath = Path.GetFullPath(myAssetPath);
string listingFilePathNoFileName = listingFilePath.Remove(listingFilePath.Length - (myAssetPath.Length)); //Remove filename
string listingRelativePath = AssetDatabase.GetAssetPath(myAsset); //Get path withing the unity folder
string listingFullPath = listingFilePathNoFileName + listingRelativePath;
string newFilePath +="mychanges";
File.Copy(listingFullPath, newFilePath, true);
Answer by silentslack · Feb 05, 2016 at 02:41 AM
Think I found a better way to rename an asset:
string assetPath = AssetDatabase.GetAssetPath(myAsset.GetInstanceID());
AssetDatabase.RenameAsset(assetPath, newName);
AssetDatabase.SaveAssets();
Answer by JouPal · May 23, 2020 at 12:03 PM
public bool renameOnValidate = false;
public int id;
public int itemName
private void OnValidate()
{
if (renameOnValidate)
{
string thisFileNewName = id + "_" + itemName;
string assetPath = UnityEditor.AssetDatabase.GetAssetPath(this.GetInstanceID());
UnityEditor.AssetDatabase.RenameAsset(assetPath, thisFileNewName);
renameOnValidate = false;
}
}
Your answer
Follow this Question
Related Questions
Prevent BuildStreamedSceneAssetBundle showing progress bar 0 Answers
Queue method to be called after asset processor is done 0 Answers
How to import the object from server to unity 2 Answers
Asset bundles for gaming consoles (ps3, xbox and wii)? 0 Answers
Some GameObjects not visible in android build but work fine in the editor? 0 Answers