• 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 Alex_May · Aug 31, 2015 at 09:58 PM · assetdatabase

AssetDatabase.CreateAsset creates extensionless assets

Hi everybody.

I'm extracting jpegs from a file format that contains other data along with several bundled jpegs. I've found and extracted the byte data, turned them into Texture2D objects, which work great. Now I want to save them:

 AssetDatabase.CreateAsset(nt, "Assets/Skins/"+skinName+sbs+".asset");
 Debug.Log(AssetDatabase.GetAssetPath(nt));

Looks ok, no? sbs contains a leading '/', so the aim is to end up with e.g. "Assets/Skins/bonemachine/navpane.asset".

But the asset that is created has no extension. If I go and rename the file to add the .asset extension, it works fine. So why is CreateAsset clipping off the extension?

Here's the full code:

     void ReadFile(TextAsset target)
     {
         Stream s = new MemoryStream(target.bytes);
         BinaryReader br = new BinaryReader(s);
         // some hard offsets
         uint ptr = 0x70;
         uint skip1 = 0xc4 - ptr;
         uint skip = 0xd4 - ptr;
         AssetDatabase.StartAssetEditing();
         do {
             s.Seek(ptr, SeekOrigin.Begin); // go to the start of the next filename
             if((char)br.ReadByte() == '/') // make sure it's a filename - if not then we've finished
             {
                 byte[] fn = br.ReadBytes(54); // hard max length for filenames
                 string filename = System.Text.Encoding.UTF8.GetString(fn);
                 // read four bytes for address, then four for length
                 s.Seek(ptr + skip1, SeekOrigin.Begin); // skip to address location
                 byte[] ptrs = br.ReadBytes(8);
                 int address = ptrs[0] + (ptrs[1] << 8) + (ptrs[2] << 16) + (ptrs[3] << 24);
                 int length = ptrs[4] + (ptrs[5] << 8) + (ptrs[6] << 16) + (ptrs[7] << 24);
                 
                 s.Seek(address, SeekOrigin.Begin); // i herd u like data so i put a file in ur file
                 int header = s.ReadByte() + (s.ReadByte() << 8);
                 if (header == 0xd8ff) // check it's a jpeg, if not, we don't care FOR NOW
                 {
                     s.Seek(address, SeekOrigin.Begin); // can't remember if i need this after prev reads
                     byte[] jpeg = br.ReadBytes(length);
                     Texture2D nt = new Texture2D(1,1);
                     nt.LoadImage(jpeg);
                     textures.Add(nt); // ignore this
                     string sbs = filename.Substring(filename.LastIndexOf('/'));
                     if (!Directory.Exists("Assets/Skins")) AssetDatabase.CreateFolder("Assets", "Skins");
                     if (!Directory.Exists("Assets/Skins/"+skinName)) AssetDatabase.CreateFolder("Assets/Skins", skinName); // not sure if assetdatabase will create folders automatically
                     AssetDatabase.CreateAsset(nt, "Assets/Skins/"+skinName+sbs+".asset"); // hmm looks like I DEFINITELY ADDED THE EXTENSION
                     Debug.Log(AssetDatabase.GetAssetPath(nt)); // this reports filename with no extension
                 }
             }
             else break;
             ptr += skip;
         }
         while(true);
         AssetDatabase.StopAssetEditing();
         AssetDatabase.SaveAssets();
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Alex_May · Sep 01, 2015 at 09:29 AM

The error is at line 14, where the byte array is converted into a string. Since it contains a bunch of zeroes, these are converted into invalid characters in the string, which don't show up as characters when viewed in the debugger but cause an exception in standard .NET file creation. In Unity's file handling system an unreported error silently causes the file to be created but without an extension, in this case, for some reason, without even logging a warning. Probably as soon as it encounters an invalid character, it clips the filename at that point.

Here is code that fixes that:

                 int nullterminator = 0;
                 for (int i = 0; i < fn.Length; i++) {
                     if (fn[i] == 0)
                     {nullterminator = i; break;}
                 }
                 string filename = System.Text.Encoding.ASCII.GetString(fn,0,nullterminator);//System.Text.Encoding.UTF8.GetString(fn);
 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

When looping through assetbundle assetdatabase how do you reference a material object 0 Answers

Proper way to refresh asset 0 Answers

How to make a new path register in the AssetDatabase? 2 Answers

Custom assets give Missing (Mono Script) 0 Answers

Detect when a component has been removed. 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