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 adrianrodriguez · Oct 28, 2015 at 01:59 PM · javascriptspritestiles2d platformer

Best way to create 2D tiled sprite. (UnityScript)

I've created a modified version of this answer here using UnityScript on how to make a 2D tiled sprite. http://answers.unity3d.com/questions/599263/how-to-make-2d-sprite-tiled.html

 // using UnityEngine;
 // using System.Collections;
 
 #pragma strict
 
 public var gridX : float = 0.0f;
 public var gridY : float = 0.0f;
 
 var sprite : SpriteRenderer;
 
 function Awake () {
 
     sprite = GetComponent.<SpriteRenderer>();
     // if(!GetSpriteAlignment(gameObject).Equals(SpriteAlignment.TopRight)){
     //     Debug.LogError("You forgot change the sprite pivot to Top Right.");
     // }
     var spriteSize_wu : Vector2 = new Vector2(sprite.bounds.size.x / transform.localScale.x, sprite.bounds.size.y / transform.localScale.y);
     var scale : Vector3 = new Vector3(1.0f, 1.0f, 1.0f);
 
     if (0.0f != gridX) {
         var width_wu : float = sprite.bounds.size.x / gridX;
         scale.x = width_wu / spriteSize_wu.x;
         spriteSize_wu.x = width_wu;
     }
 
     if (0.0f != gridY) {
         var height_wu : float = sprite.bounds.size.y / gridY;
         scale.y = height_wu / spriteSize_wu.y;
         spriteSize_wu.y = height_wu;
     }
 
     var childPrefab : GameObject = new GameObject();
 
     var childSprite : SpriteRenderer = childPrefab.AddComponent.<SpriteRenderer>();
     childPrefab.transform.position = transform.position;
     childSprite.sprite = sprite.sprite;
 
     var child : GameObject ;
     var h : int = Mathf.Round(sprite.bounds.size.y);
 
     for (var i : int = 0; i*spriteSize_wu.y < h; i++) {
 
         var w : int = Mathf.Round(sprite.bounds.size.x);
 
         for (var j : int = 0; j*spriteSize_wu.x < w; j++) {
             child = Instantiate(childPrefab) as GameObject;
             child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, spriteSize_wu.y*i, 0));
             child.transform.localScale = scale;
             child.transform.parent = transform;
         }
 
     }
 
     Destroy(childPrefab);
     sprite.enabled = false; // Disable this SpriteRenderer and let the prefab children render themselves
 
 }
 

I'm not the best at doing this and am still very new to Unity, so it's working but with one caveat: It's creating two rows of these tiles instead of one:

tiled rows

I'm basically trying to generate portions of the platformer to avoid having to duplicate a gameobject over and over manually.

So, am I not understanding quite what is being done with the code, or am I missing something? It's not a deal breaker, and with the camera I am using I'm sure I could find a work around, but I'm hoping someone will point out my mistakes and aid me in the right direction. :)

UPDATE OCT 29

The code above works perfectly for creating a 2D tilemap as statement pointed below and here is the adjustment if you want basically use this on only one row:

 var child : GameObject ;
     var w : int = Mathf.Round(sprite.bounds.size.x);
 
     for (var j : int = 0; j*spriteSize_wu.x < w; j++) {
         child = Instantiate(childPrefab) as GameObject;
         child.transform.position = transform.position - (new Vector3(spriteSize_wu.x*j, 0, 0));
         child.transform.localScale = scale;
         child.transform.parent = transform;
     }

screen-shot-2015-10-28-at-85041-am.png (140.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
1
Best Answer

Answer by Statement · Oct 28, 2015 at 11:23 PM

The script is creating a 2d tilemap.

  var h : int = Mathf.Round(sprite.bounds.size.y);
  for (var i : int = 0; i*spriteSize_wu.y < h; i++) {

I haven't tested the code or anything but the above looks dodgy. It'll round the sprites bounds height to the nearest integer. Then it will repeat on for as many spriteSize_wy.y fits in that height. Eh. My brain died a little. Anyway, it means that if the bounds height was 0.5, it would round it up to 1. If spriteSize_wu.y was 0.5, it would run it twice. 0*0.5 = 0, so 0 < 1. Then the next iteration 1*0.5 = 0.5, so 0.5 < 1. The third iteration, 2 * 0.5 = 5, so 1 == 1 and not < 1, meaning the loop would stop there. It would do 2 iterations. I have no idea why that round is in there, but it doesn't seem to do what you want.

If you want to make a strip, you could remove the loop and just set y to 0. Or you could try using the real height without any rounding.

  var h = sprite.bounds.size.y;
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 adrianrodriguez · Oct 29, 2015 at 06:09 PM 0
Share

@Statement thanks for pointing that out, now I understand the script better, and realize this is indeed perfect for creating a 2D tilemap, but also by setting y to 0 and removing the loop it did exactly what I needed to do.

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

34 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

Related Questions

Tiling Sprites and performance sprites vs. GameObjects 0 Answers

Updating tilemap tile sprites? 0 Answers

Sprites get horribly cluttered when using Tile palette 1 Answer

Making my variable global. 1 Answer

Fix tilemap cell size 0 Answers


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