• 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 dry-bones · Sep 30, 2019 at 07:22 AM · importspritesheetmetadata

Adding metadata to imported image?

So I'm making a game where you can customize your character by putting sprites into the streamingAssets folder, and the game will load the sprites into the game. Right now, each sprite has to be saved as an individual image and imported one-by-one, but I'm trying to make a system where you can make just import 1 large image, and the game will automatically apply spritesheet settings.

Here's what I've got right now. It checks the streamingAssets folder for any image with the filename containing the word "head", and if so, it turns it into one of the optional sprites for your character's head. alt text But as you can see, at this rate I'll have to do the same for every single body part. So how can I take a sprite from streamingAssets and apply metadata for a spritesheet?

.

public void LoadFiles() { path = Application.streamingAssetsPath; DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] fileinfo = dir.GetFiles(); foreach (FileInfo files in fileinfo) if (files.Name.Contains("head")) { //converts fileinfo into sprite byte[] bytes = File.ReadAllBytes(files.FullName);//Location is set before hand

                 Debug.LogError("byte array");
                 Texture2D imgTexture = new Texture2D(900, 900, TextureFormat.DXT1, false);
                 Debug.LogError("textture2d");
                 imgTexture.filterMode = FilterMode.Bilinear;
                 imgTexture.LoadImage(bytes);
                 Sprite head1 = Sprite.Create(imgTexture, new Rect(0, 0, 900, 900), new Vector2(0f, 0f), 1.0f);
             }
         }
 }


script.png (28.8 kB)
script.png (30.6 kB)
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

Answer by dry-bones · Oct 01, 2019 at 07:29 AM

Short of being able to add custom metadata, I did learn that I can slice my own spritesheet at runtime. After importing the Texture2d, I can make a sprite like so: Sprite head1 = Sprite.Create(imgTexture, new Rect(0, 0, 350, 350), new Vector2(0f, 0f), 1.0f);

And I can do this multiple times for the same sprite. So if I made a spritesheet, I can check the metadata in the inspector, and just read the values of each sprite. Then when I cut sprites at runtime, I can make them match the same position and size of the ones on the spritesheet.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

192 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 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

Imported Models Not Rendering Correctly? 5 Answers

When is the Materials problem going to get fixed? 1 Answer

obj import from 3ds Max adds a transform object 0 Answers

import won't work 2 Answers

Importing 3D model from Wolfram Mathematica 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges