1

I have a 2d flat image texture and I want to draw it in my canvas inventory UI as a cube, without generating an actual cube made of mesh. What would be the most efficient and simplest way of doing it?

I once had this problem in android SDK and solved it by creating an image of a cube with sides seperated and then on each one I drew the pixels of the texture, this is probably very expensive and I'm not sure it will work in unity(or how) (this is the solution: Create 3D cube with Canvas)

3Dave
  • 28,657
  • 18
  • 88
  • 151
SpoocyCrep
  • 604
  • 7
  • 23
  • Why don't you want to render this using a real cube? Is it moving? (If not, just render it once, grab the screen and save the result to a PNG, then use the result.) – 3Dave Jul 09 '18 at 22:57
  • BTW in Unity you really don't have to grab the screen (although you could), just use a render texture. – Fattie Jul 09 '18 at 23:06

1 Answers1

4

It's trivial to do this various ways:

(1) normally when you have a 3D object (eg, a cube, dragon, whatever) which you want to use "as an icon" in your inventory list - you just do that. it's really very easy to use a 3D item on a separate camera (like, "icon camera!") and position it correctly.

This is a very basic thing in a typical Unity scene, and y'all will need to master the techniques.

(2) You could indeed use a "render texture" and then "make" a PNG of the image you want (again, starting with the actual 3D cube).

Again, these are basic things in a typical Unity scene, and y'all will need to master the techniques.

Really "1" is easier, and there's little reason to not do it that way.

It's totally common in "menus" and so on you see in Unity games (or indeed any platform).


One trick is to get the camera images "in the right order" you need so nothing is hidden below, the trick is the "Depth" setting in Camera

enter image description here

https://docs.unity3d.com/560/Documentation/Manual/class-Camera.html

"Depth The camera’s position in the draw order. Cameras with a larger value will be drawn on top of cameras with a smaller value."

(No matter how many zillion hours I have worked with Unity, I never remember which way is which on that! :) )

One surprising thing when you're new to Unity (or any game engine) is that you often have "many cameras".

You may have just a little camera that does nothing other than make "a small black area", and then another camera on top of that, or whatever.

It's an "assemblage".


Absolutely critical tip when working w/ the UI in Unity...

https://stackoverflow.com/a/36268018/294884

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Thanks for your help, will the only problem I had so far with the first solution is that the cubes would be drawn under the canvas and by setting screen-space camera I couldn't use the drag event on them as Input.mousePosition wasn't relevent anymore. I'll try to figure out fixes but overall your solution worked well – SpoocyCrep Jul 10 '18 at 08:17
  • 1
    @SpoocyCrep (1) I put in an edit with the fix for that, (2) sorry my answer is very "brief" ! – Fattie Jul 10 '18 at 11:26
  • 1
    @Fattie - Small hint: You can imagine "depth" as something like rendering in ascending order (0, 1, 2, etc). Whatever is drawn later overlaps anything before it. That works for shaders rendering orders, UI child index, sprites sorting order. Imagine a stack of cards which you put one over another - the top ones (higher index) overlap previous ones. – Battle Jul 10 '18 at 12:08