Part of my application for a game development study is to create a game prototype in Unity. Since it’s a 2D game I decided to use orthello 2D. I really like the plugin, but googling for answers on questions is virtually impossible.
I’ve finished the game itself. And now I just need to make the main menu etc. It’s just a prototype so I want to do it as simple as possible.
In the first scene I just have a background sprite and a play button. (which is an OTanimationSprite)
I attached the followinf javascript code to the button
#pragma strict
private var button:OTAnimatingSprite; // player sprite reference
private var initialized:boolean = false; // initialization notifier
function Start ()
{
}
function checkInitialize()
{
if (!OT.isValid) return;
// We call the application initialization function from Update() once
// because we want to be sure that all Orthello objects have been started.
if (!initialized)
{
Initialize();
return;
}
Debug.Log("playButton initialized");
}
function Initialize()
{
OT.view.alwaysPixelPerfect = true;
// Get reference to player animation sprite
button = OT.ObjectByName("playButton") as OTAnimatingSprite;
// Because Javascript does not support C# delegate we have to notify our sprite class that
// we want to receive notification callbacks.
// If we have initialized for callbacks our (!IMPORTANT->) 'public' declared call back
// functions will be asutomaticly called when an event takes place.
// HINT : This technique can be used within a C# class as well.
button.InitCallBacks(this);
initialized = true; // make sure this function is only ran once
}
function Update ()
{
checkInitialize();
OnInput(button);
}
function OnInput(owner:OTObject)
{
if (Input.GetMouseButtonDown(0))
{
Application.LoadLevel (1);
}
}
it kinda works. The game registers the mouse input and switches scene. However, for some reason the game doesn’t seem to notice if my mouse is hovering over the button or not. No matter were in the stage I click, the game always switches scenes.
I tried adding a hovering effect as wel but the same problem ocurred. The game always seems to think I’m hovering over the button.
Can anyone tell me what I’m doing wrong? I’ve been trying to find the answer by looking through the documentation and examples from Orthello2D for a few hours now. But I just can’t find it.
You call your OnInput method in your every Update() cycle, so it is always called and within that method you check for a click, not a click on the button.
2 Solutions…
Remove the OnUpdate from your Update method, so it is only called when you click on the button. Make sure you have registerinput on your button checked.