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 ramSilva · Oct 25, 2016 at 01:22 PM · buildspriteunityeditorslice

Slice sprites in a build

I currently have some code that allows me to import an image from an url, create a sprite from it, slice the sprite at runtime and get a one of the slices, all at runtime.

However, all of this funcionality requires the UnityEditor namespace, which isn't available when trying to build the project.

My question is if it is possible to achieve this functionality using some other namespaces, or even external libraries if needed.

The code in question:

 void Start() {
         StartCoroutine(loadImage("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png", "Assets/Resources/Sprites/Map/", "map"));
     }
 
     IEnumerator loadImage(string url, string path, string name) { 
         WWW www = new WWW(url);
 
         // Wait for download to complete
         yield return www;
 
         Sprite sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f));
         sprite.name = name;
 
         File.WriteAllBytes(path + name + ".png", www.texture.EncodeToPNG());
         AssetDatabase.Refresh();
         AssetDatabase.AddObjectToAsset(sprite, path + name + ".png");
         AssetDatabase.SaveAssets();
     }
 
     Sprite getSubSpriteAtCoordinate(Sprite sprite, int x, int y, float width, float height) {
         string path = AssetDatabase.GetAssetPath(sprite);
         string resourcesPath = path.Replace("Assets/Resources/", "").Replace(sprite.name + ".png", "");
         string spriteName = x.ToString() + y.ToString() + width.ToString() + height.ToString();
 
         Sprite loadedSprite = Resources.Load<Sprite>(resourcesPath + sprite.name);
         if (loadedSprite != null) {
             return loadedSprite;
         }
 
         TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
 
         ti.isReadable = true;
         ti.textureType = TextureImporterType.Sprite;
         ti.spriteImportMode = SpriteImportMode.Multiple;
         List<SpriteMetaData> newData = new List<SpriteMetaData>(ti.spritesheet);
 
         SpriteMetaData smd = new SpriteMetaData();
         smd.pivot = new Vector2(0.5f, 0.5f);
         smd.alignment = 9;
         smd.name = spriteName;
         smd.rect = new Rect(x, y, width, height);
 
         newData.Add(smd);
 
         ti.spritesheet = newData.ToArray();
         AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
 
         loadedSprite = Resources.Load<Sprite>(resourcesPath + sprite.name);
 
         return loadedSprite;
     }
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

0 Replies

· Add your reply
  • Sort: 

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

96 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

Related Questions

After I built my game the exe opens and closes immediately !!! 0 Answers

Why are my sprites glitching and flashing in build? 2 Answers

Unity build Webos ? 0 Answers

Strange pixels on sprites after build on Android 0 Answers

il2cpp.exe error pls help me and solve out my problem 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