0
votes

I'm getting started with Unity, but having trouble with the most basic of operations - getting an image to appear on the screen.

I've tried researching this, but almost all the tutorials / info on Unity are about more advanced usage and don't cover the basics. There's very little info out there on basic image loading and drawing.

My project has one object, with this script attached:

public class Boxland_Main : MonoBehaviour
{
     Texture2D test_sprite;

     void Start ()
     {
       print ("STUFF");  // this works
       test_sprite = (Texture2D) Resources.Load ("richard_green.png");
     }

     void Update ()
     {
       GUI.DrawTexture (new Rect(0, 0, 50, 50), test_sprite);  // this doesn't work
     }
}

My image file is in the Resources directory, but all I ever get is a powder blue screen.

EDIT:

For clarity, I'd be interested in any solution that just gets an image up on the screen. I feel I'm getting close to going back to XNA or GameMaker.

UPDATE:

Here's what I've got now (still with no sprite being drawn):

public class Boxland_Main : MonoBehaviour
  {
  public Texture2D test_sprite;

  void Start ()
    {
    test_sprite = (Texture2D) Resources.Load ("richard_green");
    }

  void OnGui ()
    {
    if (test_sprite) GUI.DrawTexture (new Rect (0, 0, 50, 50), test_sprite);
    }
  }

UPDATE:

I found that if I drag an image directly from the asset window onto the scene window in the Unity UI, it adds the sprite to the view and the sprite shows up on compile. However, it's rendering blurry and pixelated, and there doesn't seem to be any way to specify a particular frame on the sprite sheet.

UPDATE:

I came across some material while researching that may point to a partial answer:

Unity 4.3 - 2D, how to assign programmatically sprites to an object http://docs.unity3d.com/ScriptReference/Resources.Load.html

This seems to cover loading an image, but never goes into explaining how to draw the image.

4
"doesn't work" is not a good description - does it present an error, if so, which one? Note that the Resources class can only load resources located in the Resources folder.LearnCocos2D
@LearnCocos2D: As I stated in the last line, the file is in the resources folder. There are no errors, just a blank, blue background.Nightmare Games
is it important that you load your image from the resources folder? if this is the most basic thing you're trying to do in unity, then you're still not thinking basic enoughryanscottmurphy
@ryanscottmurphy: The few examples and tips I was able to dredge up while researching seem to suggest the image files need to be in that folder, or they won't get loaded.Nightmare Games

4 Answers

1
votes

You should call GUI functions insde OnGUI, not the Update (http://docs.unity3d.com/Manual/gui-Basics.html)

So try to change your Update() to

void OnGUI() {
    if(test_sprite) {
        GUI.DrawTexture (new Rect(0, 0, 50, 50), test_sprite);
    }
}
0
votes

You dont specify the .png for resources. Just the name.

test_sprite = (Texture2D) Resources.Load("richard_green", Texture2D);
0
votes

I never did get Unity to draw anything (that wasn't blurry and compressed-looking), using any technique, but I just tried out Monogame (http://www.monogame.net/) and got good-looking sprites on the screen within five minutes. The downside to Monogame is the lack of Xbox One support, but I'm willing to live with that for now.

It looks like my final conclusion is either:

A) Unity is STILL just not a good solution for making 2D games, or

B) It's such a different animal from other engines I've used that I just can't wrap my brain around it.

And yet, obviously, lots of developers have made nice games in it (with graphics). Someone really needs to write a good, basic intro tutorial in the style of "Step 1: Starting a project, Step 2: Loading an image, Step 3: Drawing an image".

I'll likely come back to this post sometime in the future when I'm ready to give Unity a third try, and see if there are any new ideas / techniques out there.

0
votes

Did you try the example on unity? https://docs.unity3d.com/530/Documentation/ScriptReference/Texture2D.LoadImage.html

using UnityEngine;

public class ExampleClass : MonoBehaviour {
    // Load a .jpg or .png file by adding .bytes extensions to the file
    // and dragging it on the imageAsset variable.
    public TextAsset imageAsset;
    public void Start() {
        // Create a texture. Texture size does not matter, since
        // LoadImage will replace with with incoming image size.
        Texture2D tex = new Texture2D(2, 2);
        tex.LoadImage(imageAsset.bytes);
        GetComponent<Renderer>().material.mainTexture = tex;
    } }