简体   繁体   English

如何在屏幕上显示 2d 图像 unity3d

[英]How to display 2d image on the screen unity3d

I created a unity application for desktop and mobile platforms.我为桌面和移动平台创建了一个统一的应用程序。 There is a map on top-left corner of the screen.屏幕左上角有一张地图。 I draw circle on this map each frame.我每一帧都在这张地图上画圆圈。 The position of the circle changes at runtime anytime.圆圈的位置在运行时随时更改。

To do for this task, I draw circle on texture and draw this texture.为了完成这项任务,我在纹理上绘制圆圈并绘制此纹理。 This code works on emulator but not android phone or tablet.此代码适用于模拟器,但不适用于安卓手机或平板电脑。 I don't see any circle on the map.我在地图上看不到任何圆圈。

My code is here :我的代码在这里:

TextAsset imageAsset; // .png changes to .png.bytes. And the file dragged to this variable on inspector.

Start () {
  texture.LoadImage(imageAsset.bytes); 
}

void OnGUI () {

  Circle(texture, x, y, Color.red);

  if (Event.current.type.Equals(EventType.Repaint))
  {
    Graphics.DrawTexture(new Rect(..), texture);
  }
}

Some people say deprecated OnGUI .有人说不推荐使用OnGUI But if I create a button in OnGUI , it is displayed at runtime on android.但是如果我在OnGUI创建一个按钮,它会在运行时在 android 上显示。

I tried another solution.我尝试了另一种解决方案。 I create a 3d plane object on my scene, and drag .png file as texture.我在我的场景中创建了一个 3d 平面对象,并将 .png 文件拖动为纹理。 But I don't change this texture using Texture2D variable that is drawed circle.但是我不会使用绘制圆形的Texture2D变量来更改此纹理。

You should use sprites, and why did you use the OnGui Function instead of the Update ?您应该使用精灵,为什么使用 OnGui 函数而不是 Update ?

Maybe get a look on the asset store : 2DToolkit也许看看资产商店:2DToolkit

What is the resolution of your device and your image?您的设备和图像的分辨率是多少?

I'm asking this because you should consider change of resolutions between your computer's screen and yours device.我问这个是因为您应该考虑更改计算机屏幕和设备之间的分辨率。

As you can see here your rect values must be in pixels.正如您在此处看到的您的 rect 值必须以像素为单位。

I recommend you to use raw image for loading your texture, a Canvas for handling drawing and RectTransform for controlling your circle position instead.我建议您使用原始图像来加载纹理,使用Canvas处理绘图,使用RectTransform来控制圆形位置。

These components have many functionalities to deal with this resolutions problems that may happen.这些组件具有许多功能来处理可能发生的这种分辨率问题。

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

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