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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
3
Question by Lohoris2 · Dec 01, 2013 at 12:43 AM · 2dtexture2drenderer

I'm trying to create a Tiled Sprite, what am I doing wrong?

Since apparently Unity 2D still lacks tiled sprites, I'm tyring to create a class which takes a normal Sprite and tiles it using custom dimentions.

It doesn't crash, but doesn't yield results either: I just get an empty sprite apparently.

The transform position is correct (unaltered), I do use Apply, and the new width is multiple of the original one. Now I'm clueless about what could be the problem.

Hint: I'm >>>NOT<<< talking about atlases, which now are natively supported, I'm talking about having a single sprite and displaying tiled multiple times (which might be supported in future, but currently apparently isn't)

Here's the script, can anybody help?

 using UnityEngine;
 using System.Collections;
 
 public class TiledSprite : MonoBehaviour
 {
     public int width,height;
 
     private SpriteRenderer renderer;
     private Sprite originalSprite;
 
     void Awake ()
     {
         renderer=GetComponent<SpriteRenderer>();
         originalSprite=renderer.sprite;
 
         // crea il nuovo Sprite
 
         Texture2D target = new Texture2D(width,height);
         int ow = originalSprite.texture.width;
         int oh = originalSprite.texture.height;
 
         Color32[] pixOrig = originalSprite.texture.GetPixels32();
         Color32[] pixDest = new Color32[width*height];
 
         int osize=ow*oh,dsize=width*height;
         int cur=0;
         while (cur<dsize)
         {
             for (int cur2=0; cur2<osize; cur2++)
             {
                 pixDest[cur++]=pixOrig[cur2];
             }
         }
 
         target.SetPixels32(pixDest);
         target.Apply();
 
         Sprite newSprite = Sprite.Create(target,new Rect(0,0,width,height),new Vector2(width/2,height/2));
         renderer.sprite=newSprite;
     }
 }


Comment
Add comment · Show 1
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 hmortensen2907 · Dec 31, 2013 at 08:33 AM 0
Share

Hi there. I'm stuck with the exact same problem. I'm trying to do the same as you, create a tile importer, and everything works but the link to the sprite.. Did you ever figure out the problem? If you have made any headway, I would really like to know.

Link to my resent posted question http://forum.unity3d.com/threads/219939-Prefab-created-at-runtime-is-missing-sprite-reference-(original-object-is-not)

Thanks. - Henning

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Lohoris2 · Jan 05, 2014 at 02:51 PM

While I still have no clue why this didn't work, I've been helped into a much more direct solution: Unity already supports this kind of thing, it's just matter of knowing how to do that.

  1. set your image Texture Type: Texture, Wrap Mode: Repeat

  2. create a new Material using that image, set the Shader: Particles/Alpha Blended, set the Tiling to how many times you want to repeat it

  3. in the scene create a Quad using that Material

Not a perfect solution, especially since you have to manually set the tiling in the Material itself, but was good enough for what I was doing.

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 hmortensen2907 · Jan 05, 2014 at 07:28 PM 0
Share

Thanks, but that was kind of the old way of doing it. I was expecting a better way in 4.3+.. So as you say, not perfect.

I found another solution that worked for me, but it relays on the spritesheet already existing.

snippet from my blog post on the subject. (link text)


  • had newer written an editor script for unity before, and did have a lot of problems with references to the sprites I generated in the script… They did not exsist after the script exitede! :/ After a day of googling, asking in the unity forum, installing IRC and asking in the channel #unity3d without anyone being able to help, I did figure it out myself. I had to import the sprites in unity as I normaly would, so they exsisted and would be referencable from the script, this I could do by loading them via Resources.LoadAll(name).*


Thanks for getting back with your findings :)

avatar image Ash-Blue · Jul 23, 2014 at 02:28 PM 0
Share

While this works to repeat a simple image, it doesn't work very well if you have a more complicated repeating item that needs to tile on the fly from my findings. For example a ladder doesn't tile very well with this technique (unless I'm missing something).

avatar image berk_can · Sep 23, 2017 at 09:15 AM 0
Share

I dont have Texture mode to be selected from $$anonymous$$ultiBox, what can I do, I am using Unity 5

avatar image
0

Answer by Ash-Blue · Jul 23, 2014 at 04:23 PM

This is quite easy to do with a simple script and nothing else except a sprite renderer. See my answer at the link below for more details (note it might not be live yet since I'm awaiting moderator approval).

http://answers.unity3d.com/answers/754993/view.html

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to make my sprite face way Im walking 1 Answer

Changing two different objects renderer colour 1 Answer

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer

What is the best way to draw a 2D sine wave? 3 Answers

Unity2D: Renderer off/ on by triggers 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