简体   繁体   English

Unity 不显示精灵/纹理

[英]Unity Not Displaying Sprites / Textures

I'm getting started with Unity, but having trouble with the most basic of operations - getting an image to appear on the screen.我刚开始使用 Unity,但在最基本的操作上遇到了麻烦 - 让图像出现在屏幕上。

I've tried researching this, but almost all the tutorials / info on Unity are about more advanced usage and don't cover the basics.我试过研究这个,但几乎所有关于 Unity 的教程/信息都是关于更高级的用法,不包括基础知识。 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.我的图像文件位于 Resources 目录中,但我得到的只是粉蓝色屏幕。

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.我觉得我离回到 XNA 或 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.我发现如果我将图像直接从资源窗口拖到 Unity UI 中的场景窗口上,它会将精灵添加到视图中,并且精灵会在编译时显示出来。 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 Unity 4.3 - 2D,如何以编程方式将精灵分配给对象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.这似乎涵盖了加载图像,但从未解释过如何绘制图像。

You should call GUI functions insde OnGUI, not the Update ( http://docs.unity3d.com/Manual/gui-Basics.html )您应该在 OnGUI 中调用 GUI 函数,而不是更新 ( http://docs.unity3d.com/Manual/gui-Basics.html )

So try to change your Update() to所以尝试将您的 Update() 更改为

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

You dont specify the .png for resources.您没有为资源指定 .png。 Just the name.只是名字。

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

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.我从来没有让 Unity 使用任何技术绘制任何东西(不是模糊和压缩的),但我只是尝试了 Monogame( http://www.monogame.net/ )并在五分钟内屏幕。 The downside to Monogame is the lack of Xbox One support, but I'm willing to live with that for now. Monogame 的缺点是缺乏 Xbox One 支持,但我现在愿意接受这一点。

It looks like my final conclusion is either:看起来我的最终结论是:

A) Unity is STILL just not a good solution for making 2D games, or A) Unity 仍然不是制作 2D 游戏的好解决方案,或者

B) It's such a different animal from other engines I've used that I just can't wrap my brain around it. B)它与我使用过的其他引擎是如此不同,以至于我无法将我的大脑环绕在它周围。

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".有人真的需要以“第 1 步:启动项目,第 2 步:加载图像,第 3 步:绘制图像”的风格编写一个好的基本介绍教程。

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.当我准备好第三次尝试 Unity 时,我可能会在将来的某个时候回到这篇文章,看看是否有任何新的想法/技术。

Did you try the example on unity?你有没有尝试过统一的例子? https://docs.unity3d.com/530/Documentation/ScriptReference/Texture2D.LoadImage.html 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;
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM