Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. // @NOTE the attached sprite's position should be "top left" or the children will not align properly
  5. // Strech out the image as you need in the sprite render, the following script will auto-correct it when rendered in the game
  6. [RequireComponent (typeof (SpriteRenderer))]
  7.  
  8. // Generates a nice set of repeated sprites inside a streched sprite renderer
  9. // @NOTE Vertical only, you can easily expand this to horizontal with a little tweaking
  10. public class RepeteSprite : MonoBehaviour {
  11.     SpriteRenderer sprite;
  12.     public Sprite cantoDireito,cantoEsquerdo;
  13.     public PhysicsMaterial2D material;
  14.     void Awake () {
  15.         print ("roou repete sprite");
  16.         // Get the current sprite with an unscaled size
  17.         sprite = GetComponent<SpriteRenderer>();
  18.         Vector2 spriteSize = new Vector2(sprite.bounds.size.x / transform.localScale.x, sprite.bounds.size.y / transform.localScale.y);
  19.        
  20.         // Generate a child prefab of the sprite renderer
  21.         GameObject childPrefab = new GameObject();
  22.         SpriteRenderer childSprite = childPrefab.AddComponent<SpriteRenderer>();
  23.         BoxCollider2D coll= childPrefab.AddComponent<BoxCollider2D>();
  24.         coll.sharedMaterial = material;
  25.         coll.size = sprite.sprite.bounds.size;
  26.         childPrefab.transform.position = transform.position;
  27.         childSprite.sprite = sprite.sprite;
  28.         childPrefab.layer = 8;
  29.  
  30.        
  31.         // Loop through and spit out repeated tiles
  32.         GameObject child;
  33.         print ("l = "+(int)Mathf.Round(sprite.bounds.size.x));
  34.         for (int i = 1, l = (int)Mathf.Round(sprite.bounds.size.x); i <=l*0.5; i++) {
  35.             child = Instantiate(childPrefab) as GameObject;
  36.             child.transform.position = transform.position - (new Vector3(spriteSize.x,0, 0) * i);
  37.             child.transform.parent = transform;
  38.         }
  39.  
  40.         for (int i = 1, l = (int)Mathf.Round(sprite.bounds.size.x); i <=l*0.5; i++) {
  41.             child = Instantiate(childPrefab) as GameObject;
  42.             child.transform.position = transform.position + (new Vector3(spriteSize.x,0, 0) * i);
  43.             child.transform.parent = transform;
  44.         }
  45.  
  46.         for (int i = 1, l = (int)Mathf.Round(sprite.bounds.size.y); i <l*0.8f; i++) {
  47.             child = Instantiate(childPrefab) as GameObject;
  48.             child.transform.position = transform.position - (new Vector3(0,sprite.sprite.bounds.size.y, 0) * i);
  49.             child.transform.parent = transform;
  50.             print ("instanciou em y");
  51.         }
  52.        
  53.         for (int i = 1, l = (int)Mathf.Round(sprite.bounds.size.y); i <l*0.8f; i++) {
  54.             child = Instantiate(childPrefab) as GameObject;
  55.             child.transform.position = transform.position + (new Vector3(0,sprite.sprite.bounds.size.y, 0) * i);
  56.             child.transform.parent = transform;
  57.             print ("instanciou em y");
  58.         }
  59.  
  60.         //GetComponent<BoxCollider2D>().size = new Vector2(Mathf.Round(sprite.bounds.size.x),Mathf.Round(sprite.bounds.size.y)*0.8f);
  61.        
  62.         // Set the parent last on the prefab to prevent transform displacement
  63.         childPrefab.transform.parent = transform;
  64.        
  65.         // Disable the currently existing sprite component since its now a repeated image
  66.         sprite.enabled = false;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement