Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Deivore · Aug 26, 2018 at 03:26 PM · editor-scriptingtexture2dassetdatabasetextureimporter

How can I save a texture and edit its importer options with TextureImporter in the same pass?

I'm attempting to create a drag-and-drop editor script for a certain filetype that will ultimately make a sprite renderer prefab with a dynamically created material and textures with specific import settings.


Where I'm stuck is between creating the image and setting the import settings. I can create the texture and save it to a png just fine, and I can load from a png and edit its import settings using TextureImporter.GetAtPath(), but I can't do both in the same pass. When the texture is created and saved in the same script, TextureImporter.GetAtPath() returns null and crashes the script. However if the texture existed since the beginning of the script, everything works just fine. What seems to me to be happening is that there is some kind of importer event in Unity's editor loop that is populating some metadata the TextureImporter needs, but I can't seem to find the right call to make this happen.


Code is as follows:

 private static string type = "LBM.json";
 
     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
     {
         foreach (var path in importedAssets)
         {
             if (path.LastIndexOf('.') - 3 == path.ToLower().IndexOf(type.ToLower()))
             {
                 ImportLBM(path);
             }
         }
     }
 
     private static void ImportLBM(string assetPath)
     {
         //get folder location
         string assetPathLower = assetPath.ToLower();
         string folder = assetPathLower.Substring(0,assetPathLower.IndexOf(type.ToLower()) - 1);
 
         LBM_Asset lbm = JsonUtility.FromJson<LBM_Asset>(File.ReadAllText(assetPathLower));
         if(lbm.width == 0 || lbm.height == 0 || lbm.pixels.Count != lbm.width*lbm.height)
         {
             throw new System.Exception("Bad LBM file!");
         }
 
         //Get the name
         string filename = lbm.filename.Substring(0, lbm.filename.LastIndexOf('.')-1);
 
         //---------------
         //Make the indexed image
         var indexedTexture = new Texture2D(lbm.width, lbm.height, TextureFormat.ARGB32, false);
         for (int i = 0; i < lbm.height; i++)
         {
             for (int j = 0; j < lbm.width; j++)
             {
                 int index = i * lbm.width + j ;
                 indexedTexture.SetPixel(j, lbm.height -i-1, new Color(lbm.pixels[index] / 255f, lbm.pixels[index] / 255f, lbm.pixels[index] / 255f, 1.0f));
             }
         }
         indexedTexture.filterMode = FilterMode.Point;
         indexedTexture.Apply();
         byte[] Ibytes = indexedTexture.EncodeToPNG();
         File.WriteAllBytes(folder + "_indices.png", Ibytes); //.png saves just fine
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
         AssetDatabase.ImportAsset(folder + "_indices.png", ImportAssetOptions.ForceUpdate);
         //!!Missing something here?
         
         //Set indices importer info
         TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(folder + "_indices.png"); //returns null
         importer.spritePixelsPerUnit = 1; //null pointer crash
         importer.textureCompression = TextureImporterCompression.Uncompressed;
         importer.filterMode = FilterMode.Point;
         importer.SaveAndReimport();

I've also tried using AssetDatabase.CreateAsset(indexedTexture, folder + "_indices.asset"); with the .asset extension and the .png extension. The former creates an correct-looking asset file whose import properties I can't edit, and the latter makes a corrupted png but doesn't crash and allows me to modify settings in the same pass.


As you can see this is done by hooking into the OnPostprocessAllAssets because I'm currently overriding json import behavior (lbm.json), though a more complete version of this will probably override the extension .lbm itself, from which I'm generating an lbm.json file.

Cheers!

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 Deivore · Sep 09, 2018 at 04:22 PM

So as far as I can tell, AssetDatabase.Refresh(); won't work as long as your function is being called from OnPostprocessAllAssets().

The solution is to call ImportLBM() or whatever your import funciton is called via EditorApplication.update CallBackFunction. Since I need the path preserved between calls, I need a static reservoir for those paths:

     private static EditorApplication.CallbackFunction _importDelegate;
     private static List<string> _assetsMarkedForImport = new List<string>();

Inside OnPostprocessAllAssets, I set the delegate up and add some new paths if new paths to import are found:

 if (_importDelegate == null)
                 {
                     _importDelegate = new EditorApplication.CallbackFunction(ImportLBM);
                 }
                 _assetsMarkedForImport.Add(path);
                 EditorApplication.update = Delegate.Combine(EditorApplication.update, _importDelegate) as EditorApplication.CallbackFunction;

Finally, the inside of the actual import function is wrapped up thus:

 EditorApplication.update = Delegate.Remove(EditorApplication.update, _importDelegate as EditorApplication.CallbackFunction) as EditorApplication.CallbackFunction;
 
         try
         {
 
             foreach (string assetPath in _assetsMarkedForImport)
             {
                     <import code here>
              }
         catch (Exception e)
         {
             Debug.Log(e);
         }
         finally
         {
             _assetsMarkedForImport.Clear();
         }

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

165 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

Related Questions

Changes to asset data persist - unable to discard changes 1 Answer

TextureImporterType.Sprite does not work 0 Answers

How to change Texture in editor 0 Answers

Forece recompile from code after modifying scriptingDefines 2 Answers

How can I get a sub asset? 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