0

I'm making a game for a school project, and we have been tasked to create a game using the HappyFunTimes plugin for Unity. For the game my group is making, we need to generate a random sprite image in a C# script in Unity, and get the sprite to display in the upper middle of the phone's controller screen. Unfortunately, at this point in my education I'm only familiar with C# so I'm having trouble working through the HappyFunTimes's game-pad code. Any help would be appreciated.

B. Mc
  • 11
  • 3

1 Answers1

0

I don't know how to generate an image in Unity. I know last time I checked there was no easy 2D API for direct rendering (as in draw a line, draw a rectangle, draw a circle, etc...)

You can render a scene to a texture though, see render texture, render to it, and then get the data out.

Or if you could just create an array and set values if it's a small image. (for example the gravatar images here on SO or a QR code).

Once you have the data you'd need to base64 encode it there should be some C# functions to do that. Then you can send it to the phone. On the phone you'd need to decode it, put it in an ImageData then you can copy it into a canvas with putImageData

You can then either display that canvas as is, OR, use it to draw into another canvas, OR, generate a dataURL for an image. All of that will have to be in JavaScript/HTML/CSS not C#

... although you could make C# send a dataURL directly and skip the decoding, imagedata, and canvas parts. Just assign the image's src property to the dataURL

Community
  • 1
  • 1
gman
  • 100,619
  • 31
  • 269
  • 393
  • Thank you! We have a random PNG file that is chosen and displayed through a sprite renderer, not drawing it through code. Would you be able to point me to the HTML file that is being passed to the phone that we will need to modify? (We're using the d-pad with 2 buttons one that comes with HFT). – B. Mc Mar 09 '17 at 23:11
  • The HTML file used depends on the sample. As it says in the docs all files served to the phone are in `Assets/WebPlayerTemplate/HappyFunTimes`. Which file is used for the controller is set in the `PlayerSpanwer` or `PlayerConnector` relative to the folder. If you're just using a randomly selected PNG put a copy of the PNG files somewhere inside that folder and then just send the filename to the controller and point some img.src to that filename. – gman Mar 10 '17 at 10:54