Browse Source

New AddButton() and RemoveButton() room methods

Use AddButton() to make any sprite clickable
Use RemoveButton() to remove clickable functionality
master
chrisgregan 11 years ago
parent
commit
4f40c9c06f
  1. 39
      Assets/Fungus/Scripts/Button.cs
  2. 60
      Assets/Fungus/Scripts/Commands.cs
  3. 12
      Assets/Fungus/Scripts/Room.cs
  4. BIN
      Assets/FungusExample/Scenes/Example.unity
  5. 12
      Assets/FungusExample/Scripts/SpritesRoom.cs

39
Assets/Fungus/Scripts/Button.cs

@ -1,4 +1,5 @@
using UnityEngine; using UnityEngine;
using System;
using System.Collections; using System.Collections;
using Fungus; using Fungus;
@ -10,25 +11,34 @@ namespace Fungus
[RequireComponent (typeof (BoxCollider2D))] [RequireComponent (typeof (BoxCollider2D))]
public class Button : MonoBehaviour public class Button : MonoBehaviour
{ {
public string methodName; public Action buttonAction;
public SpriteRenderer spriteRenderer; public SpriteRenderer spriteRenderer;
public bool autoDisable = false; // Makes a sprite into a clickable button
public static void MakeButton(SpriteRenderer _spriteRenderer, Action _buttonAction)
void Start()
{ {
spriteRenderer = GetComponent<SpriteRenderer>(); if (_spriteRenderer == null)
{
Debug.LogError("Sprite renderer must not be null");
return;
} }
void OnMouseUpAsButton() // Remove any previous button component that was added
{ Button oldButton = _spriteRenderer.gameObject.GetComponent<Button>();
if (methodName == "") if (oldButton != null)
{ {
return; Destroy(oldButton);
} }
// Ignore button press if button is not fully visible // This will automatically add a BoxCollider2d if none currently exists
Button button = _spriteRenderer.gameObject.AddComponent<Button>();
button.buttonAction = _buttonAction;
button.spriteRenderer = _spriteRenderer;
}
void OnMouseUpAsButton()
{
// Ignore button press if sprite is not fully visible
if (spriteRenderer.color.a != 1f) if (spriteRenderer.color.a != 1f)
{ {
return; return;
@ -40,12 +50,7 @@ namespace Fungus
return; return;
} }
room.ExecuteCommandMethod(methodName); room.ExecuteCommandMethod(buttonAction);
if (autoDisable)
{
gameObject.SetActive(false);
}
} }
} }
} }

60
Assets/Fungus/Scripts/Commands.cs

@ -351,7 +351,7 @@ namespace Fungus
} }
} }
// Sets an animator trigger to change the animation state for an animated sprite // Sets an animator trigger to change the animator state for an animated sprite
public class SetAnimatorTriggerCommand : CommandQueue.Command public class SetAnimatorTriggerCommand : CommandQueue.Command
{ {
Animator animator; Animator animator;
@ -381,6 +381,64 @@ namespace Fungus
} }
} }
// Makes a sprite behave as a clickable button
public class AddButtonCommand : CommandQueue.Command
{
SpriteRenderer spriteRenderer;
Action buttonAction;
public AddButtonCommand(SpriteRenderer _spriteRenderer,
Action _buttonAction)
{
if (_spriteRenderer == null)
{
Debug.LogError("Sprite renderer must not be null.");
return;
}
spriteRenderer = _spriteRenderer;
buttonAction = _buttonAction;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Button.MakeButton(spriteRenderer, buttonAction);
if (onComplete != null)
{
onComplete();
}
}
}
// Makes a sprite stop behaving as a clickable button
public class RemoveButtonCommand : CommandQueue.Command
{
SpriteRenderer spriteRenderer;
public RemoveButtonCommand(SpriteRenderer _spriteRenderer)
{
if (_spriteRenderer == null)
{
Debug.LogError("Sprite renderer must not be null.");
return;
}
spriteRenderer = _spriteRenderer;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Button button = spriteRenderer.gameObject.GetComponent<Button>();
GameObject.Destroy(button);
if (onComplete != null)
{
onComplete();
}
}
}
// Pans the camera to a view over a period of time. // Pans the camera to a view over a period of time.
public class PanToViewCommand : CommandQueue.Command public class PanToViewCommand : CommandQueue.Command
{ {

12
Assets/Fungus/Scripts/Room.cs

@ -323,6 +323,18 @@ namespace Fungus
commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, duration, slideOffset)); commandQueue.AddCommand(new FadeSpriteCommand(spriteRenderer, color, duration, slideOffset));
} }
// Makes a sprite behave as a clickable button
public void AddButton(SpriteRenderer buttonSprite, Action buttonAction)
{
commandQueue.AddCommand(new AddButtonCommand(buttonSprite, buttonAction));
}
// Makes a sprite stop behaving as a clickable button
public void RemoveButton(SpriteRenderer buttonSprite)
{
commandQueue.AddCommand(new RemoveButtonCommand(buttonSprite));
}
// Sets an animator trigger to change the animation state for an animated sprite // Sets an animator trigger to change the animation state for an animated sprite
public void SetAnimatorTrigger(Animator animator, string triggerName) public void SetAnimatorTrigger(Animator animator, string triggerName)
{ {

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

12
Assets/FungusExample/Scripts/SpritesRoom.cs

@ -12,7 +12,6 @@ public class SpritesRoom : Room
void OnEnter() void OnEnter()
{ {
HideSprite(redMushroomSprite); HideSprite(redMushroomSprite);
FadeSprite(redMushroomSprite, 1f, 2f);
ShowSprite(blueAlienSprite); ShowSprite(blueAlienSprite);
@ -24,15 +23,24 @@ public class SpritesRoom : Room
Say("Blue Alien starts to dance."); Say("Blue Alien starts to dance.");
Say("Tap on Blue Alien to stop him dancing."); Say("Tap on Blue Alien to stop him dancing.");
FadeSprite(redMushroomSprite, 0f, 2f); AddButton(blueAlienSprite, StopDancing);
} }
// This method is called from the Button component on the BlueAlien object // This method is called from the Button component on the BlueAlien object
void StopDancing() void StopDancing()
{ {
RemoveButton(blueAlienSprite);
SetAnimatorTrigger(blueAlienAnim, "Stop"); SetAnimatorTrigger(blueAlienAnim, "Stop");
Say("Nice moves there Blue Alien!"); Say("Nice moves there Blue Alien!");
Say("Maybe you want a nice mushroom to sit down on?");
FadeSprite(redMushroomSprite, 1f, 1f);
Say("Don't want to sit? Ok, no problem.");
FadeSprite(redMushroomSprite, 0f, 1f);
Say("Uh oh, you look like you're turning a little green after all that dancing!"); Say("Uh oh, you look like you're turning a little green after all that dancing!");
SetAnimatorTrigger(blueAlienAnim, "StartGreenWalk"); SetAnimatorTrigger(blueAlienAnim, "StartGreenWalk");

Loading…
Cancel
Save