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
0
Question by mmangual_83 · Dec 09, 2013 at 04:30 PM · c#2dguicreate a texture

Creating 2D art in a 3D world

I have to draw 2D sprites that follow a 3d object for a school project we are doing in Unity. It should look like something like this:

alt text

I am somewhat new to the whole 2d on 3d integration. I have the images (about 15) so I need to know how to create a method that can loop these images during run time. So, can someone show me a good starting point on how I should go about this? Thank you in advance.

spritesheet.png (1.8 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by x_ivan_x@live.nl · Dec 09, 2013 at 07:16 PM

Create a quad/plane and put a material on that quad/plane with frame 1 attached. Make sure that all the frame pictures are in separate picture files & that the dimensions(x & y) are the same from frame 1 till 5. Assign them in the inspector.

 using UnityEngine;
 using System.Collections;
 
 public class SwitchBitmap : MonoBehaviour {
     
     public float frameSwitchSpeed = 0.25f;
     public Texture[] sprites;
     
     void Start()
     {
         UpdateSprite(0);
     }
     
     void UpdateSprite(int i)
     {
         renderer.material.mainTexture = sprites[i];
         StartCoroutine("LoopSprites", i);
     }
     
     IEnumerator LoopSprites(int i)
     {
         i++;
         if(i >= sprites.Length)
             i = 0;
         yield return new WaitForSeconds(frameSwitchSpeed);
         UpdateSprite (i);
     }
 }
 
Comment
Add comment · Show 6 · 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 mmangual_83 · Dec 09, 2013 at 07:18 PM 0
Share

The class runs fine but there is an issue where the sprites are not being drawn on the screen. Is there something I may have missed???

avatar image x_ivan_x@live.nl · Dec 09, 2013 at 08:36 PM 0
Share

Do you have the quad/plane object pointing towards the camera in game? Also, are there any errors in the console?

Note that this script only loops through the sprites, it doesn't follow an object. here's a link that helps you with that: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html Basically make it follow the 3d object & make it look at the camera.

-edit sorry, i misunderstood your question. i got confused with the 2d/3d thing. Please explain this concept in more detail. Are you trying to make a 2d game or is that just a top view of something 3d that you're trying to create?

avatar image mmangual_83 · Dec 09, 2013 at 08:40 PM 0
Share

How do I get the quad/plane? I am very new at this

avatar image mmangual_83 · Dec 09, 2013 at 08:46 PM 0
Share

do i write the lookup all in the same sprite animator class?

avatar image mmangual_83 · Dec 09, 2013 at 09:05 PM 0
Share

@x_ivan_x@live.nl: You are right. What I am trying to accomplish is to place a 2d sprite in a 3d world; kind of like GUI. But I require a way to loop an array of 2d sprites (animate them). The class you provided above works! In the inspector I can see the sprite images looping but in the game world nothing shows up. Here is how I am setting everything up: 1 - created a class and pasted your code there. 2- when i go to into unity and try to play it it gives me an error saying that the class (the sprite animation class) is trying to access rendered which I have not placed in the game, so I added 2D sprite renderer and that seemed to have taken care of that error. 3- I play it but nothing shows up.

And that is where I am at right now.

Show more comments
avatar image
0

Answer by CoalCzar · Dec 09, 2013 at 09:26 PM

For Unity 4.3, you are on the right track with the Sprite Renderer. And x_ivan_x@live.nl's class will work if you want a scripting method, however, there is now a different (and I think easier) way.

  1. In the Project window, select all your textures for the animation.

  2. In the Inspector window, click on the Texture Type drop down box and select Sprite.

  3. (If you know what settings you want for the textures, go ahead and do that as well)

  4. Press Apply.

  5. Go back to the Project window. With all the textures selected, drag them into an empty space in the Hierarchy.

  6. Save the animation at the prompt.

  7. You can now edit the animation in the Animation window and trigger it using scripts and the Animator window.

Comment
Add comment · Show 4 · 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 mmangual_83 · Dec 09, 2013 at 09:32 PM 0
Share

I do not understand steps 5-7 because Unity will not let me get that far. When I select what I want to select it does not let me drag the sprites into empty space in the hierarchy.

avatar image CoalCzar · Dec 09, 2013 at 10:23 PM 0
Share

Hmm, and they all are labeled as a Sprite Texture Type in the inspector? I tested all the steps as listed before I posted my answer.

avatar image mmangual_83 · Dec 09, 2013 at 11:33 PM 0
Share

Yep. I made double sure. It will not let me take the images and put them in the hierarchy

avatar image CoalCzar · Dec 10, 2013 at 03:02 AM 0
Share

Are you able to drag even one Sprite into the Hierarchy from the Project folder?

avatar image
0

Answer by mmangual_83 · Dec 10, 2013 at 04:42 PM

This is the solution to my problem. Many thanks to the people who responded.

 public Texture2D[] mSprites;
 private Texture2D currentSprite;
 private int counter;
 public float switchTime = 0.5f;
  
 public Rect r_Sprite;
  
 void Start(){
     counter = 0;
     StartCoroutine("SwitchSprite");
 }
  
 void OnGUI(){
     GUI.DrawTexture(r_Sprite,currentSprite);
 }

 
 private IEnumerator SwitchSprite(){
 currentSprite = mSprites[counter];
 
 if(counter < mSprites.Length){
    counter++;
 }else{
    counter = 0;
 }
 
 yield return new WaitForSeconds(switchTime);
 StartCoroutine("SwitchSprite");

}

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

18 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

Related Questions

C# Applying Transparency to a Single GUI.Button 1 Answer

C# Boolean Doesn't Change Value 1 Answer

Custom AsyncOperation to show LoadingImage for slowish XML conversion & email upload method in Unity? 1 Answer

Enable and disable a button selection 3 Answers

Issues creating a progress bar 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