Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by sokmeng999 · Sep 09, 2014 at 11:10 AM · sprite

How to divide Sprite texture dynamically?

Dear all friends using Unity,

My name is Sokmeng, a junior unity programmer, and currently I have a problem with Sprite Texture.

Normally I can use sprite editor to divide one texture to several pieces and drag all of those generated pieces to the Hierarchy. However, I just want to do it dynamically with c# script to divide sprite texture and assign those generated piece onto the hierarchy.

The reason for this question is that I want to create a dynamic Puzzle Game on Unity.

I look forward to hearing from you. Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by JustFun · Sep 09, 2014 at 01:05 PM

Here's a simple example. Texture of size 1024x1024 is being cut into 64 equal pieces(8x8) and then they are placed on equal distance. All sprites are parented by some gameobject called "SpritesRoot", which exists in the scene.

 using UnityEngine;
 using System.Collections;
 
 public class TextureDivider : MonoBehaviour {
 
     public Texture2D source;
     
     // Use this for initialization
     void Start () {
 
         GameObject spritesRoot = GameObject.Find("SpritesRoot");
 
         for(int i = 0; i < 8; i++)
         {
             for(int j = 0; j < 8; j++)
             {
                 Sprite newSprite = Sprite.Create(source, new Rect(i*128, j*128, 128, 128), new Vector2(0.5f, 0.5f));
                 GameObject n = new GameObject();
                 SpriteRenderer sr = n.AddComponent<SpriteRenderer>();
                 sr.sprite = newSprite;
                 n.transform.position = new Vector3(i*2, j*2 , 0);
                 n.transform.parent = spritesRoot.transform;
             }
         }
     }
 }
 

In Sprite.Create function 128 is the size of tile. Vector2(0.5f, 0.5f) makes sprites pivot to be placed in the center of tile.

Comment
Add comment · Show 3 · 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
avatar image AubryStrawberry · Jun 21, 2015 at 10:03 AM 0
Share

Good answer - but how would you split up a texture into non-rectangular parts? Procedurally splitting a square or other into a triangles for example.

avatar image startas · Jun 21, 2015 at 04:25 PM 0
Share

I guess you have to edit sprite or texture then, and set part or your image as fully transparent.

avatar image freedom667 · Feb 24, 2018 at 10:49 AM 0
Share

I have 9 objects. how can we place the pieces without add? I couldn't do it

 Texture2D img = QuestionImage[Random.Range(0, QuestionImage.Length)];
         for (int i = 0; i < 3; i++){
             for (int j = 0; j < 3; j++){
                 Sprite newSprite = Sprite.Create(img, new Rect(i * 200, j * 200, 200, 200), new Vector2(0.5f, 0.5f));
                 Image image = list.GetComponentInChildren<Image>();
                 image.sprite = newSprite;
             }
         }

 
avatar image
0

Answer by ismaelnascimento01 · Sep 13, 2017 at 07:42 PM

Here: https://forum.unity.com/threads/dynamic-2d-destruction.235005/

Comment
Add comment · Show 1 · 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
avatar image nebosite · Oct 14, 2018 at 03:28 AM 0
Share

This approach works for me, but it is a bit slow. I am starting from a 2000x2000 image and slicing into 25 frames and this takes around 1 second. I would be interested in ideas for how to speed this up.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to do make up using brush to draw sprite and remove using shower? 2 Answers

Screen resolution make sprites pixelated 0 Answers

How to make a different sprite load after every hit? 1 Answer

GameObject With Background Covers GameObject Without Background 1 Answer

How to merge Sorting Layers ? 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