Skip to content

Commit

Permalink
add camera example
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 28, 2017
1 parent e907c00 commit a06a5d1
Show file tree
Hide file tree
Showing 35 changed files with 1,009 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Assets/HappyFunTimes-README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Assets
└── HappyFunTimes
├── 3rdparty see below
├── controllers
│   ├── camera deletable
│   ├── character-select deletable
│   ├── clean deletable
│   ├── gamepad see below
Expand Down Expand Up @@ -104,6 +105,30 @@ http://docs.happyfuntimes.net/docs/unity/rendezvous-server.html

## Change List

* 2.1.4

* Added MoreSamples/Camera sample

* 2.1.3

* Updated for 2017

* 2.1.2

* Fix sounds example

* 2.1.1

* Disable auto connect on iOS installation mode.

Apple either removed or broke this feature on iOS

* Upadte to Unity 5.6.1

* 2.1.0

* Add Character example

* 2.0.6

* Update to Unity 5.6
Expand Down
9 changes: 9 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera/Materials.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera/Scenes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Camera Example

This sample is based of the Simple Example in `Assets/HappyFunTimes/MoreSamples/Simple`
It shows letting the user take or select an image on their camera then
sending that image to the game and using it as a texture.

It uses a custom controller found in
`Assets/WebPlayerTemplates/HappyFunTimes/controllers/camera/controller.html`

Tersely, if you select the `ExampleCameraSpawner` GameObject you'll see it has a `PlayerSpawner`
component set to spawn the `PrefabForExampleCamera` prefab anytime a player connects their phone.

The `PrefabForExampleCamera` prefab has the `ExampleCameraPlayer` script which uses the `NetPlayer`
object passed to it to send and receive messages from the phone.

The code in `Assets/WebPlayerTemplates/HappyFunTimes/controllers/camera/controller.html`
uses an input type=file tag to ask the user to select an image. It then scales that image to 256x256,
using a 2D canvas, converts it to a dataUrl and sends it to the game. The game converts the dataUrl
to binary and passes it to `Texture2D.Load` in unity.

For more info see [http://docs.happyfuntimes.net/docs/unity/](http://docs.happyfuntimes.net/docs/unity/).

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEngine;

namespace HappyFunTimesExample {

// A Singlton-ish class for some global settings.
// PS: I know Singltons suck but dependency injection is hard
// in Unity.
public class ExampleCameraGameSettings : MonoBehaviour {

public int areaWidth = 300; // matches JavaScript
public int areaHeight = 300;

public static ExampleCameraGameSettings settings() {
return s_settings;
}

static private ExampleCameraGameSettings s_settings;

void Awake() {
if (s_settings != null) {
throw new System.InvalidProgramException("there is more than one game settings object!");
}
s_settings = this;
}

void Cleanup() {
s_settings = null;
}

void OnDestroy() {
Cleanup();
}

void OnApplicationExit() {
Cleanup();
}
}

} // namespace HappyFunTimesExample

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions Assets/HappyFunTimes/MoreSamples/camera/Scripts/ExampleCameraPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using HappyFunTimes;
using CSSParse;

namespace HappyFunTimesExample {

class ExampleCameraPlayer : MonoBehaviour {
private class MessageMove {
public float x = 0;
public float y = 0;
};

private class MessagePic {
public string dataUrl;
}

void InitializeNetPlayer(SpawnInfo spawnInfo) {
// Save the netplayer object so we can use it send messages to the phone
m_netPlayer = spawnInfo.netPlayer;

// Register handler to call if the player disconnects from the game.
m_netPlayer.OnDisconnect += Remove;

// Track name changes
m_playerNameManager = new HFTPlayerNameManager(m_netPlayer);
m_playerNameManager.OnNameChange += ChangeName;

// Setup events for the different messages.
m_netPlayer.RegisterCmdHandler<MessageMove>("move", OnMove);
m_netPlayer.RegisterCmdHandler<MessagePic>("pic", OnPicture);

ExampleCameraGameSettings settings = ExampleCameraGameSettings.settings();
m_position = new Vector3(UnityEngine.Random.Range(0, settings.areaWidth), 0, UnityEngine.Random.Range(0, settings.areaHeight));
transform.localPosition = m_position;

SetName(m_playerNameManager.Name);
}

void Start() {
m_renderer = gameObject.GetComponent<Renderer>();
m_position = gameObject.transform.localPosition;
SetColor(new Color(0.0f, 1.0f, 0.0f));
}

void Update() {
}

void OnDestroy() {
if (m_playerNameManager != null) {
m_playerNameManager.Close();
m_playerNameManager = null;
}
}

void OnGUI()
{
Vector2 size = m_guiStyle.CalcSize(m_guiName);
Vector3 coords = Camera.main.WorldToScreenPoint(transform.position);
m_nameRect.x = coords.x - size.x * 0.5f - 5.0f;
m_nameRect.y = Screen.height - coords.y - 30.0f;
GUI.Box(m_nameRect, m_name, m_guiStyle);
}

void SetName(string name) {
m_name = name;
gameObject.name = "Player-" + m_name;
m_guiName = new GUIContent(m_name);
m_guiStyle.normal.textColor = Color.black;
m_guiStyle.contentOffset = new Vector2(4.0f, 2.0f);
Vector2 size = m_guiStyle.CalcSize(m_guiName);
m_nameRect.width = size.x + 12;
m_nameRect.height = size.y + 5;
}

void SetColor(Color color)
{
m_color = color;
m_renderer.material.color = m_color;
Color[] pix = new Color[1];
pix[0] = color;
Texture2D tex = new Texture2D(1, 1);
tex.SetPixels(pix);
tex.Apply();
m_guiStyle.normal.background = tex;
}

void SetTexture(Byte[] binaryData)
{
Texture2D tex = new Texture2D(4, 4);
tex.LoadImage(binaryData);
m_renderer.material.mainTexture = tex;
m_renderer.material.color = Color.white;
}

private void Remove(object sender, EventArgs e) {
Destroy(gameObject);
}


private static string s_dataUrlHeader = "data:image/png;base64,";

private void OnPicture(MessagePic data) {
SetTexture(Convert.FromBase64String(data.dataUrl.Substring(s_dataUrlHeader.Length)));
}

private void OnMove(MessageMove data) {
ExampleCameraGameSettings settings = ExampleCameraGameSettings.settings();
m_position.x = data.x * settings.areaWidth;
m_position.z = settings.areaHeight - (data.y * settings.areaHeight) - 1; // because in 2D down is positive.

gameObject.transform.localPosition = m_position;
}

private void ChangeName(string name) {
SetName(name);
}

private NetPlayer m_netPlayer;
private HFTPlayerNameManager m_playerNameManager;
private Renderer m_renderer;
private Vector3 m_position;
private Color m_color;
private string m_name;
private GUIStyle m_guiStyle = new GUIStyle();
private GUIContent m_guiName = new GUIContent("");
private Rect m_nameRect = new Rect(0,0,0,0);
}

} // namespace HappyFunTimesExample

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
["icon-16.png","3rdparty/audiomanager-min.js","3rdparty/audiomanager.js","3rdparty/chroma.min.js","3rdparty/game-utils-min.js","3rdparty/game-utils.js","3rdparty/gyronorm.complete.min.js","controllers/character-select/controller.html","controllers/character-select/css/controller.css","controllers/character-select/images/cube.svg","controllers/character-select/images/cylinder.svg","controllers/character-select/images/sphere.svg","controllers/character-select/scripts/controller.js","controllers/clean/controller.html","controllers/gamepad/controller.html","controllers/gamepad/css/controller.css","controllers/gamepad/images/bird.png","controllers/gamepad/images/hft-circle-button.svg","controllers/gamepad/images/hft-down-arrow-button.svg","controllers/gamepad/images/hft-left-arrow-button.svg","controllers/gamepad/images/hft-right-arrow-button.svg","controllers/gamepad/images/hft-up-arrow-button.svg","controllers/gamepad/scripts/controller.js","controllers/gamepad/sounds/explosion.mp3","controllers/gamepad/sounds/gameover.mp3","controllers/gamepad/sounds/launch.mp3","controllers/gamepad/sounds/sounds.jsfx.txt","controllers/multi-machine/controller.html","controllers/multi-machine/css/controller.css","controllers/multi-machine/images/bird.png","controllers/multi-machine/images/hft-circle-button.svg","controllers/multi-machine/images/hft-down-arrow-button.svg","controllers/multi-machine/images/hft-left-arrow-button.svg","controllers/multi-machine/images/hft-right-arrow-button.svg","controllers/multi-machine/images/hft-up-arrow-button.svg","controllers/multi-machine/scripts/controller.js","controllers/simple/controller.html","controllers/simple/css/controller.css","controllers/simple/scripts/controller.js","controllers/syncedclock/controller.html","controllers/syncedclock/assets/clocktick.mp3","controllers/syncedclock/assets/clocktick.ogg","controllers/syncedclock/css/controller.css","controllers/syncedclock/scripts/controller.js","hft/hft-min.js","hft/hft.js","hft/missing.html","hft/assets/gear-icon.svg","hft/captive-portal/captive-portal.html","hft/captive-portal/game-login.html","hft/captive-portal/login.css","hft/captive-portal/xtra2.bin","sample-ui/sample-ui-min.js","sample-ui/sample-ui.css","sample-ui/sample-ui.js"]
["icon-16.png","3rdparty/audiomanager-min.js","3rdparty/audiomanager.js","3rdparty/chroma.min.js","3rdparty/game-utils-min.js","3rdparty/game-utils.js","3rdparty/gyronorm.complete.min.js","controllers/camera/controller.html","controllers/camera/css/controller.css","controllers/camera/scripts/controller.js","controllers/character-select/controller.html","controllers/character-select/css/controller.css","controllers/character-select/images/cube.svg","controllers/character-select/images/cylinder.svg","controllers/character-select/images/sphere.svg","controllers/character-select/scripts/controller.js","controllers/clean/controller.html","controllers/gamepad/controller.html","controllers/gamepad/css/controller.css","controllers/gamepad/images/bird.png","controllers/gamepad/images/hft-circle-button.svg","controllers/gamepad/images/hft-down-arrow-button.svg","controllers/gamepad/images/hft-left-arrow-button.svg","controllers/gamepad/images/hft-right-arrow-button.svg","controllers/gamepad/images/hft-up-arrow-button.svg","controllers/gamepad/scripts/controller.js","controllers/gamepad/sounds/explosion.mp3","controllers/gamepad/sounds/gameover.mp3","controllers/gamepad/sounds/launch.mp3","controllers/gamepad/sounds/sounds.jsfx.txt","controllers/multi-machine/controller.html","controllers/multi-machine/css/controller.css","controllers/multi-machine/images/bird.png","controllers/multi-machine/images/hft-circle-button.svg","controllers/multi-machine/images/hft-down-arrow-button.svg","controllers/multi-machine/images/hft-left-arrow-button.svg","controllers/multi-machine/images/hft-right-arrow-button.svg","controllers/multi-machine/images/hft-up-arrow-button.svg","controllers/multi-machine/scripts/controller.js","controllers/simple/controller.html","controllers/simple/css/controller.css","controllers/simple/scripts/controller.js","controllers/syncedclock/controller.html","controllers/syncedclock/assets/clocktick.mp3","controllers/syncedclock/assets/clocktick.ogg","controllers/syncedclock/css/controller.css","controllers/syncedclock/scripts/controller.js","hft/hft-min.js","hft/hft.js","hft/missing.html","hft/assets/gear-icon.svg","hft/captive-portal/captive-portal.html","hft/captive-portal/game-login.html","hft/captive-portal/login.css","hft/captive-portal/xtra2.bin","sample-ui/sample-ui-min.js","sample-ui/sample-ui.css","sample-ui/sample-ui.js"]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a06a5d1

Please sign in to comment.