Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating multiple textures with ScriptedImporter

Discussion in 'Scripting' started by afr088, Nov 14, 2017.

  1. afr088

    afr088

    Joined:
    Nov 12, 2017
    Posts:
    2
    So, I created my own ScriptedImporter. It works fine, except that it sets one main asset (using SetMainAsset) and multiple sub assets (using SetSubAsset).
    I'd prefer to create multiple textures rather than using SetSubAsset. But assuming I *have* to use SetSubAsset, how can I create, for instance, a canvas and set it's source to a sub asset of a particular texture?

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.Text;
    4. using System.Drawing;
    5. using UnityEngine;
    6. using UnityEditor.Experimental.AssetImporters;
    7. using Files.FAR3;
    8.  
    9. namespace Files
    10. {
    11.     [ScriptedImporter(1, "dat")]
    12.     public class FARImporter : ScriptedImporter
    13.     {
    14.         public uint CheckVersion(string Path, bool ThrowException)
    15.         {
    16.             FileReader Reader = new FileReader(Path, false);
    17.  
    18.             ASCIIEncoding Enc = new ASCIIEncoding();
    19.             string MagicNumber = Enc.GetString(Reader.ReadBytes(8));
    20.  
    21.             if (!MagicNumber.Equals("FAR!byAZ", StringComparison.InvariantCultureIgnoreCase))
    22.             {
    23.                 if (ThrowException)
    24.                     throw new Exception("MagicNumber was wrong - FARImporter.cs!");
    25.                 else
    26.                 {
    27.                     Reader.Close();
    28.                     return 0;
    29.                 }
    30.             }
    31.  
    32.             uint Version = Reader.ReadUInt32(); //Version.
    33.             Reader.Close();
    34.  
    35.             return Version;
    36.         }
    37.  
    38.         public override void OnImportAsset(AssetImportContext ctx)
    39.         {
    40.             uint Version = CheckVersion(ctx.assetPath, true);
    41.  
    42.             if (Version == 1)
    43.             {
    44.                 FAR1Archive Archive = new FAR1Archive(ctx.assetPath);
    45.                 Debug.Log("Opened archive: " + ctx.assetPath);
    46.                 Archive.ReadArchive(true);
    47.             }
    48.             else if(Version == 3)
    49.             {
    50.                 MemoryStream MemStream = new MemoryStream();
    51.                 FAR3Archive Archive = new FAR3Archive(ctx.assetPath);
    52.                 Debug.Log("Opened archive: " + ctx.assetPath);
    53.                 Archive.ReadArchive(true);
    54.  
    55.                 bool LoadedFirstTexture = false;
    56.  
    57.                 foreach (FAR3Entry Entry in Archive.GrabAllEntries())
    58.                 {
    59.                     MemStream.Position = 0;
    60.  
    61.                     try
    62.                     {
    63.                         Bitmap BMap = new Bitmap(Archive.GrabEntry(new UniqueFileID(Entry.TypeID, Entry.FileID).UniqueID));
    64.                         Texture2D Tex = new Texture2D(BMap.Width, BMap.Height);
    65.                         BMap.MakeTransparent(System.Drawing.Color.FromArgb(255, 0, 255));
    66.                         BMap.MakeTransparent(System.Drawing.Color.FromArgb(255, 1, 255));
    67.                         BMap.MakeTransparent(System.Drawing.Color.FromArgb(254, 2, 254));
    68.                         BMap.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
    69.                         Tex.LoadImage(MemStream.ToArray());
    70.  
    71.                         if (!LoadedFirstTexture)
    72.                         {
    73.                             ctx.SetMainAsset(Entry.Filename, Tex, Tex);
    74.                             LoadedFirstTexture = true;
    75.                         }
    76.                         else
    77.                             ctx.AddSubAsset(Entry.Filename, Tex, Tex);
    78.  
    79.                         BMap.Dispose();
    80.                     }
    81.                     catch (Exception)
    82.                     {
    83.                         Paloma.TargaImage TImg = new Paloma.TargaImage(Archive.GrabEntry(new UniqueFileID(Entry.TypeID, Entry.FileID).UniqueID));
    84.                         Texture2D Tex = new Texture2D(TImg.Image.Width, TImg.Image.Height);
    85.                         TImg.Image.MakeTransparent(System.Drawing.Color.FromArgb(255, 0, 255));
    86.                         TImg.Image.MakeTransparent(System.Drawing.Color.FromArgb(255, 1, 255));
    87.                         TImg.Image.MakeTransparent(System.Drawing.Color.FromArgb(254, 2, 254));
    88.                         TImg.Image.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
    89.                         Tex.LoadImage(MemStream.ToArray());
    90.  
    91.                         if (!LoadedFirstTexture)
    92.                         {
    93.                             ctx.SetMainAsset(Entry.Filename, Tex, Tex);
    94.                             LoadedFirstTexture = true;
    95.                         }
    96.                         else
    97.                             ctx.AddSubAsset(Entry.Filename, Tex, Tex);
    98.                     }
    99.                 }
    100.             }
    101.         }
    102.     }
    103. }
    104.  
     
  2. Foxaphantsum

    Foxaphantsum

    Joined:
    Jul 5, 2013
    Posts:
    139
    Yea this is also something I'm looking for.
     
  3. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    412
    This link is a textbook Unity Form/Answers experience.