How to Detect single and Double click in unity? - c#

I've written the following code:
void OnMouseUpAsButton()
{
if (Type == 0) {
if (click && Time.time <= (clickTime + clickDelta))
{
Debug.Log("Double");
Destroy(sp);
click = false;
}
else
{
Debug.Log("one");
click = true;
clickTime = Time.time;
Destroy(sp);
}
}else if (Type == -1)
{
Destroy(sp);
}
}
It detects double-click But has a problem!
in Both, objects will be removed
When you first click happens, Object is deleted And the second click is not recognized!
I want for Android
please help me. thanks

The following class can be used in Editor or device.
public class InputController : MonoBehaviour
{
public event Action OnSingleTap;
public event Action OnDoubleTap;
[Tooltip("Defines the maximum time between two taps to make it double tap")]
[SerializeField]private float tapThreshold = 0.25f;
private Action updateDelegate;
private float tapTimer = 0.0f;
private bool tap = false;
private void Awake()
{
#if UNITY_EDITOR || UNITY_STANDALONE
updateDelegate = UpdateEditor;
#elif UNITY_IOS || UNITY_ANDROID
updateDelegate = UpdateMobile;
#endif
}
private void Update()
{
if(updateDelegate != null){ updateDelegate();}
}
private void OnDestroy()
{
OnSingleTap = null;
OnDoubleTap = null;
}
#if UNITY_EDITOR || UNITY_STANDALONE
private void UpdateEditor()
{
if (Input.GetMouseButtonDown(0))
{
if (Time.time < this.tapTimer + this.tapThreshold)
{
if(OnDoubleTap != null){ OnDoubleTap(); }
this.tap = false;
return;
}
this.tap = true;
this.tapTimer = Time.time;
}
if (this.tap == true && Time.time>this.tapTimer + this.tapThreshold)
{
this.tap = false;
if(OnSingleTap != null){ OnSingleTap();}
}
}
#elif UNITY_IOS || UNITY_ANDROID
private void UpdateMobile ()
{
for(int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 2)
{
if(OnDoubleTap != null){ OnDoubleTap();}
}
if(Input.GetTouch(i).tapCount == 1)
{
if(OnSingleTap != null) { OnSingleTap(); }
}
}
}
}
#endif
}

You're telling it to destroy the object in both halves of your if-statement (re-read your inner else block). You would need to set up your update method or coroutine to process a single click after the Double Click timer has been reached.
Simple sample below:
void OnMouseUpAsButton()
{
if(!clicked)
{
clicked = true;
return;
}
if(Time.time <= (clickTime + clickDelta))
{
//Double Click occured
clicked = false;
}
}
void Update()
{
if(clicked)
{
if(Time.time >= (clickTime + clickDelta))
{
//Handle single click
clicked = false;
}
}
}
Note this is just to demonstrate a simple way to handle this using most of what you provided.
You can also find additional information at this question:
http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html

Related

What should I add in the Update to apply the lookAtSpecificTarget flag and the lookAtSpecificTargetIndex changes at run time?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public FLookAnimator lookAnimator;
public Transform[] targets;
public bool lookAtSpecificTarget = false;
public int lookAtSpecificTargetIndex;
public float switchingTime;
public bool switchingLoop = false;
private bool loopOnce = true;
// Start is called before the first frame update
private void Start()
{
if (lookAtSpecificTarget && targets.Length > 0)
{
if (lookAtSpecificTargetIndex >= 0)
{
lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];
}
}
else
{
StartCoroutine(SwitchTargetsPeriodically());
}
}
private void Update()
{
}
IEnumerator SwitchTargetsPeriodically()
{
for (int i = 0; true; i = (i + 1) % targets.Length)
{
if (i <= targets.Length - 1 && switchingLoop)
{
loopOnce = true;
lookAnimator.ObjectToFollow = targets[i];
}
if (switchingLoop == false && loopOnce)
{
if (i <= targets.Length - 1)
{
lookAnimator.ObjectToFollow = targets[i];
}
if (i == (targets.Length - 1))
{
loopOnce = false;
}
}
yield return new WaitForSeconds(switchingTime);
}
}
}
It's working fine the first time when running the game but what should I do in the Update to apply the changes of the flag and index of the target in the Update at run time?
The goal is to be able to switch the target index for looking at run time if the flag is true.
if the flag is false don't use the lookAtSpecificTargetIndex and continue with the Coroutine but if the flag set to true then pause the Coroutine and use the lookAtSpecificTargetIndex :
lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];
If you are looking for a quick way just for the editor you can use [ContextMenu] like
private Coroutine _routine;
[ContextMenu(nameof(ApplyTargetIndex))]
private void ApplyLookAtTarget()
{
if (lookAtSpecificTarget && targets.Length > 0)
{
if(_routine!= null) StopCoroutine(_routine);
_routine = null;
if (lookAtSpecificTargetIndex >= 0)
{
lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];
}
}
else
{
_routine = StartCoroutine(SwitchTargetsPeriodically());
}
}
this allows you to execute this method via the Context menu of the Inspector of your component

Scene is Loading in Unity but unable to Load Scene in Android

My Game is working perfectly in Unity but when I export apk tp Android Mobile 2nd Scene doesn't load properly and does not work.
Please look at the image. It is the second Scene and not loaded properly and the Run button is not working which is working perfectly in unity play mode.
enter image description here
CODES ARE BELOW
SCENE 1 CODE#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_ANALYTICS
using UnityEngine.Analytics;
#endif
#if UNITY_PURCHASING
using UnityEngine.Purchasing;
#endif
public class StartButton : MonoBehaviour
{
public void StartGame()
{
if (PlayerData.instance.ftueLevel == 0)
{
PlayerData.instance.ftueLevel = 1;
PlayerData.instance.Save();
#if UNITY_ANALYTICS
AnalyticsEvent.FirstInteraction("start_button_pressed");
#endif
}
#if UNITY_PURCHASING
var module = StandardPurchasingModule.Instance();
#endif
SceneManager.LoadScene("main");
}
}
SCENE 2 CODE#
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
#if UNITY_ANALYTICS
using UnityEngine.Analytics;
#endif
/// <summary>
/// State pushed on the GameManager during the Loadout, when player select player, theme and accessories
/// Take care of init the UI, load all the data used for it etc.
/// </summary>
public class LoadoutState : AState
{
public Canvas inventoryCanvas;
[Header("Char UI")]
public Text charNameDisplay;
public RectTransform charSelect;
public Transform charPosition;
[Header("Theme UI")]
public Text themeNameDisplay;
public RectTransform themeSelect;
public Image themeIcon;
[Header("PowerUp UI")]
public RectTransform powerupSelect;
public Image powerupIcon;
public Text powerupCount;
public Sprite noItemIcon;
[Header("Accessory UI")]
public RectTransform accessoriesSelector;
public Text accesoryNameDisplay;
public Image accessoryIconDisplay;
[Header("Other Data")]
public Leaderboard leaderboard;
public MissionUI missionPopup;
public Button runButton;
public GameObject tutorialBlocker;
public GameObject tutorialPrompt;
public MeshFilter skyMeshFilter;
public MeshFilter UIGroundFilter;
public AudioClip menuTheme;
[Header("Prefabs")]
public ConsumableIcon consumableIcon;
Consumable.ConsumableType m_PowerupToUse = Consumable.ConsumableType.NONE;
protected GameObject m_Character;
protected List<int> m_OwnedAccesories = new List<int>();
protected int m_UsedAccessory = -1;
protected int m_UsedPowerupIndex;
protected bool m_IsLoadingCharacter;
protected Modifier m_CurrentModifier = new Modifier();
protected const float k_CharacterRotationSpeed = 45f;
protected const string k_ShopSceneName = "shop";
protected const float k_OwnedAccessoriesCharacterOffset = -0.1f;
protected int k_UILayer;
protected readonly Quaternion k_FlippedYAxisRotation = Quaternion.Euler (0f, 180f, 0f);
public override void Enter(AState from)
{
tutorialBlocker.SetActive(!PlayerData.instance.tutorialDone);
tutorialPrompt.SetActive(false);
inventoryCanvas.gameObject.SetActive(true);
missionPopup.gameObject.SetActive(false);
charNameDisplay.text = "";
themeNameDisplay.text = "";
k_UILayer = LayerMask.NameToLayer("UI");
skyMeshFilter.gameObject.SetActive(true);
UIGroundFilter.gameObject.SetActive(true);
// Reseting the global blinking value. Can happen if the game unexpectedly exited while still blinking
Shader.SetGlobalFloat("_BlinkingValue", 0.0f);
if (MusicPlayer.instance.GetStem(0) != menuTheme)
{
MusicPlayer.instance.SetStem(0, menuTheme);
StartCoroutine(MusicPlayer.instance.RestartAllStems());
}
runButton.interactable = false;
runButton.GetComponentInChildren<Text>().text = "Loading...";
if(m_PowerupToUse != Consumable.ConsumableType.NONE)
{
//if we come back from a run and we don't have any more of the powerup we wanted to use, we reset the powerup to use to NONE
if (!PlayerData.instance.consumables.ContainsKey(m_PowerupToUse) || PlayerData.instance.consumables[m_PowerupToUse] == 0)
m_PowerupToUse = Consumable.ConsumableType.NONE;
}
Refresh();
}
public override void Exit(AState to)
{
missionPopup.gameObject.SetActive(false);
inventoryCanvas.gameObject.SetActive(false);
if (m_Character != null) Addressables.ReleaseInstance(m_Character);
GameState gs = to as GameState;
skyMeshFilter.gameObject.SetActive(false);
UIGroundFilter.gameObject.SetActive(false);
if (gs != null)
{
gs.currentModifier = m_CurrentModifier;
// We reset the modifier to a default one, for next run (if a new modifier is applied, it will replace this default one before the run starts)
m_CurrentModifier = new Modifier();
if (m_PowerupToUse != Consumable.ConsumableType.NONE)
{
PlayerData.instance.Consume(m_PowerupToUse);
Consumable inv = Instantiate(ConsumableDatabase.GetConsumbale(m_PowerupToUse));
inv.gameObject.SetActive(false);
gs.trackManager.characterController.inventory = inv;
}
}
}
public void Refresh()
{
PopulatePowerup();
StartCoroutine(PopulateCharacters());
StartCoroutine(PopulateTheme());
}
public override string GetName()
{
return "Loadout";
}
public override void Tick()
{
if (!runButton.interactable)
{
bool interactable = ThemeDatabase.loaded && CharacterDatabase.loaded;
if(interactable)
{
runButton.interactable = true;
runButton.GetComponentInChildren<Text>().text = "Run!";
//we can always enabled, as the parent will be disabled if tutorial is already done
tutorialPrompt.SetActive(true);
}
}
if(m_Character != null)
{
m_Character.transform.Rotate(0, k_CharacterRotationSpeed * Time.deltaTime, 0, Space.Self);
}
charSelect.gameObject.SetActive(PlayerData.instance.characters.Count > 1);
themeSelect.gameObject.SetActive(PlayerData.instance.themes.Count > 1);
}
public void GoToStore()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(k_ShopSceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive);
}
public void ChangeCharacter(int dir)
{
PlayerData.instance.usedCharacter += dir;
if (PlayerData.instance.usedCharacter >= PlayerData.instance.characters.Count)
PlayerData.instance.usedCharacter = 0;
else if(PlayerData.instance.usedCharacter < 0)
PlayerData.instance.usedCharacter = PlayerData.instance.characters.Count-1;
StartCoroutine(PopulateCharacters());
}
public void ChangeAccessory(int dir)
{
m_UsedAccessory += dir;
if (m_UsedAccessory >= m_OwnedAccesories.Count)
m_UsedAccessory = -1;
else if (m_UsedAccessory < -1)
m_UsedAccessory = m_OwnedAccesories.Count-1;
if (m_UsedAccessory != -1)
PlayerData.instance.usedAccessory = m_OwnedAccesories[m_UsedAccessory];
else
PlayerData.instance.usedAccessory = -1;
SetupAccessory();
}
public void ChangeTheme(int dir)
{
PlayerData.instance.usedTheme += dir;
if (PlayerData.instance.usedTheme >= PlayerData.instance.themes.Count)
PlayerData.instance.usedTheme = 0;
else if (PlayerData.instance.usedTheme < 0)
PlayerData.instance.usedTheme = PlayerData.instance.themes.Count - 1;
StartCoroutine(PopulateTheme());
}
public IEnumerator PopulateTheme()
{
ThemeData t = null;
while (t == null)
{
t = ThemeDatabase.GetThemeData(PlayerData.instance.themes[PlayerData.instance.usedTheme]);
yield return null;
}
themeNameDisplay.text = t.themeName;
themeIcon.sprite = t.themeIcon;
skyMeshFilter.sharedMesh = t.skyMesh;
UIGroundFilter.sharedMesh = t.UIGroundMesh;
}
public IEnumerator PopulateCharacters()
{
accessoriesSelector.gameObject.SetActive(false);
PlayerData.instance.usedAccessory = -1;
m_UsedAccessory = -1;
if (!m_IsLoadingCharacter)
{
m_IsLoadingCharacter = true;
GameObject newChar = null;
while (newChar == null)
{
Character c = CharacterDatabase.GetCharacter(PlayerData.instance.characters[PlayerData.instance.usedCharacter]);
if (c != null)
{
m_OwnedAccesories.Clear();
for (int i = 0; i < c.accessories.Length; ++i)
{
// Check which accessories we own.
string compoundName = c.characterName + ":" + c.accessories[i].accessoryName;
if (PlayerData.instance.characterAccessories.Contains(compoundName))
{
m_OwnedAccesories.Add(i);
}
}
Vector3 pos = charPosition.transform.position;
if (m_OwnedAccesories.Count > 0)
{
pos.x = k_OwnedAccessoriesCharacterOffset;
}
else
{
pos.x = 0.0f;
}
charPosition.transform.position = pos;
accessoriesSelector.gameObject.SetActive(m_OwnedAccesories.Count > 0);
AsyncOperationHandle op = Addressables.InstantiateAsync(c.characterName);
yield return op;
if (op.Result == null || !(op.Result is GameObject))
{
Debug.LogWarning(string.Format("Unable to load character {0}.", c.characterName));
yield break;
}
newChar = op.Result as GameObject;
Helpers.SetRendererLayerRecursive(newChar, k_UILayer);
newChar.transform.SetParent(charPosition, false);
newChar.transform.rotation = k_FlippedYAxisRotation;
if (m_Character != null)
Addressables.ReleaseInstance(m_Character);
m_Character = newChar;
charNameDisplay.text = c.characterName;
m_Character.transform.localPosition = Vector3.right * 1000;
//animator will take a frame to initialize, during which the character will be in a T-pose.
//So we move the character off screen, wait that initialised frame, then move the character back in place.
//That avoid an ugly "T-pose" flash time
yield return new WaitForEndOfFrame();
m_Character.transform.localPosition = Vector3.zero;
SetupAccessory();
}
else
yield return new WaitForSeconds(1.0f);
}
m_IsLoadingCharacter = false;
}
}
void SetupAccessory()
{
Character c = m_Character.GetComponent<Character>();
c.SetupAccesory(PlayerData.instance.usedAccessory);
if (PlayerData.instance.usedAccessory == -1)
{
accesoryNameDisplay.text = "None";
accessoryIconDisplay.enabled = false;
}
else
{
accessoryIconDisplay.enabled = true;
accesoryNameDisplay.text = c.accessories[PlayerData.instance.usedAccessory].accessoryName;
accessoryIconDisplay.sprite = c.accessories[PlayerData.instance.usedAccessory].accessoryIcon;
}
}
void PopulatePowerup()
{
powerupIcon.gameObject.SetActive(true);
if (PlayerData.instance.consumables.Count > 0)
{
Consumable c = ConsumableDatabase.GetConsumbale(m_PowerupToUse);
powerupSelect.gameObject.SetActive(true);
if (c != null)
{
powerupIcon.sprite = c.icon;
powerupCount.text = PlayerData.instance.consumables[m_PowerupToUse].ToString();
}
else
{
powerupIcon.sprite = noItemIcon;
powerupCount.text = "";
}
}
else
{
powerupSelect.gameObject.SetActive(false);
}
}
public void ChangeConsumable(int dir)
{
bool found = false;
do
{
m_UsedPowerupIndex += dir;
if(m_UsedPowerupIndex >= (int)Consumable.ConsumableType.MAX_COUNT)
{
m_UsedPowerupIndex = 0;
}
else if(m_UsedPowerupIndex < 0)
{
m_UsedPowerupIndex = (int)Consumable.ConsumableType.MAX_COUNT - 1;
}
int count = 0;
if(PlayerData.instance.consumables.TryGetValue((Consumable.ConsumableType)m_UsedPowerupIndex, out count) && count > 0)
{
found = true;
}
} while (m_UsedPowerupIndex != 0 && !found);
m_PowerupToUse = (Consumable.ConsumableType)m_UsedPowerupIndex;
PopulatePowerup();
}
public void UnequipPowerup()
{
m_PowerupToUse = Consumable.ConsumableType.NONE;
}
public void SetModifier(Modifier modifier)
{
m_CurrentModifier = modifier;
}
public void StartGame()
{
if (PlayerData.instance.tutorialDone)
{
if (PlayerData.instance.ftueLevel == 1)
{
PlayerData.instance.ftueLevel = 2;
PlayerData.instance.Save();
}
}
manager.SwitchState("Game");
}
public void Openleaderboard()
{
leaderboard.displayPlayer = false;
leaderboard.forcePlayerDisplay = false;
leaderboard.Open();
}
}
When you are building an APK file it can be splited to APK + OBB.
So, it is possible that you are trying to load a scene that is absent in your APK. You need to force the creation of the APK without the OBB. To do it you need to check Player Settings in Android, go to Publishing Settings and uncheck Split Application Binary.

Porblem with game once builded

in my game to pass from one level to another you must answer some questions. Each question has an associated image. For each level of play there is a different set of questions. During the Unity game simulation there is no problem, but when I play the build of the game, the set of questions is not updated between one level and another, in fact the last question made at the first level is re-proposed.
public class MiniGameManager : MonoBehaviour
{
public Question[] questions;
private static List<Question> unanswered;
private Question currentQuestion;
[SerializeField]
private Image sprite;
[SerializeField]
private float timeQuestion = 1f;
public static int contCorrect;
public Button firstSelected;
void Start()
{
if(unanswered == null || unanswered.Count == 0)
{
unanswered = questions.ToList<Question>();
}
contCorrect = 0;
SetCurrentQuestion();
firstSelected.Select();
}
void SetCurrentQuestion()
{
int index = UnityEngine.Random.Range(0, unanswered.Count);
currentQuestion = unanswered[index];
sprite.sprite = currentQuestion.questionSprite;
}
IEnumerator ToQuestion()
{
unanswered.Remove(currentQuestion);
yield return new WaitForSeconds(timeQuestion);
SetCurrentQuestion();
}
public void UserSelectFirst()
{
if (currentQuestion.answer == 0)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectSecond()
{
if (currentQuestion.answer == 1)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectThird()
{
if (currentQuestion.answer == 2)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
public void UserSelectFourth()
{
if (currentQuestion.answer == 3)
{
currentQuestion.correct = true;
countCorrect++;
}
else
{
currentQuestion.correct = false;
}
if (unanswered.Count > 1)
{
StartCoroutine(ToQuestion());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, LoadSceneMode.Single);
}
}
}
I solved the problem. I just had to clear the list of questions before moving to the next level

trying to save and load a value in c# unity but having trouble

I am trying to get part of my game working but having trouble solving this problem!
I have boosters that remove game pieces if the user has enough in game coins to use. It is working to an extent and once a booster has been used the coins will decrement but upon exiting and reloading the game it does not update the correct value!
For example, the boosters can be used at a cost of 50 coins, if I have 200 coins and use 4 boosters the value updates to 0 and this works. However if I restart then the value of 200 is still there!
I have a coin manager script set up with playerprefs that gives the user 50 coins after each level, this is linked to the booster script.
Booster class:
public class Booster : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {
Image m_image;
RectTransform m_rectXform;
Vector3 m_startPosition;
Board m_board;
Tile m_tileTarget;
public static GameObject ActiveBooster;
public Text instructionsText;
public string instructions = "drag over game piece to remove";
public bool isEnabled = false;
public bool isDraggable = false;
public bool isLocked = false;
public List<CanvasGroup> canvasGroups;
public UnityEvent boostEvent;
public int boostTime = 15;
//coin test
CoinManager coinManager;
private void Awake()
{
m_image = GetComponent<Image> ();
m_rectXform = GetComponent<RectTransform> ();
m_board = Object.FindObjectOfType<Board> ().GetComponent<Board> ();
coinManager = GameObject.Find ("CoinManager").GetComponent<CoinManager> ();
}
void Start ()
{
EnableBooster (false);
}
public void EnableBooster(bool state)
{
isEnabled = state;
if (state)
{
DisableOtherBoosters ();
Booster.ActiveBooster = gameObject;
}
else if(gameObject == Booster.ActiveBooster)
{
Booster.ActiveBooster = null;
}
m_image.color = (state) ? Color.white : Color.gray;
if (instructionsText != null)
{
instructionsText.gameObject.SetActive (Booster.ActiveBooster != null);
if (gameObject == Booster.ActiveBooster)
{
instructionsText.text = instructions;
}
}
}
void DisableOtherBoosters()
{
Booster[] allBoosters = Object.FindObjectsOfType<Booster> ();
foreach (Booster b in allBoosters)
{
if (b != this)
{
b.EnableBooster (false);
}
}
}
public void ToggleBooster()
{
EnableBooster (!isEnabled);
}
public void OnBeginDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked)
{
m_startPosition = gameObject.transform.position;
EnableCanvasGroups (false);
}
}
public void OnDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked && Camera.main != null)
{
Vector3 onscreenPosition;
RectTransformUtility.ScreenPointToWorldPointInRectangle (m_rectXform, eventData.position,
Camera.main, out onscreenPosition);
gameObject.transform.position = onscreenPosition;
RaycastHit2D hit2D = Physics2D.Raycast (onscreenPosition, Vector3.forward, Mathf.Infinity);
if (hit2D.collider != null)
{
m_tileTarget = hit2D.collider.GetComponent<Tile> ();
}
else
{
m_tileTarget = null;
}
}
}
public void OnEndDrag (PointerEventData eventData)
{
if (isEnabled && isDraggable && !isLocked)
{
gameObject.transform.position = m_startPosition;
EnableCanvasGroups (true);
if (m_board != null && m_board.isRefilling)
{
return;
}
if (m_tileTarget != null)
{
if (boostEvent != null)
{
boostEvent.Invoke (); //can do things here like play a sound effect
}
EnableBooster (false);
m_tileTarget = null;
Booster.ActiveBooster = null;
}
}
}
void EnableCanvasGroups(bool state)
{
if (canvasGroups != null && canvasGroups.Count > 0)
{
foreach (CanvasGroup cGroup in canvasGroups)
{
if (cGroup != null)
{
cGroup.blocksRaycasts = state;
}
}
}
}
public void RemoveOneGamePiece()
{
if (m_board != null && m_tileTarget != null)
{
if (coinManager.currentCoinCount >= 50)
{
m_board.ClearAndRefillBoard (m_tileTarget.xIndex, m_tileTarget.yIndex);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
public void AddTime()
{
if (GameManager.Instance != null)
{
if (coinManager.currentCoinCount >= 50){
GameManager.Instance.AddTime (boostTime);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
public void DropColorBomb() //big bomb
{
if (m_board != null && m_tileTarget != null)
{
if (coinManager.currentCoinCount >= 50)
{
m_board.MakeColorBombBooster (m_tileTarget.xIndex, m_tileTarget.yIndex);
coinManager.AddCoins (-50);
coinManager.SetCoinCount();
coinManager.ShowCoinCount ();
}
}
}
}
CoinManager class:
public class CoinManager : Singleton<CoinManager>{
// reference to the UI Text element
public Text coinText;
// current number of coins earned during gameplay
public int currentCoinCount = 0;
// because we don't have a key set, we should create it and initialize its value to 0
public void InitCoinCount()
{
if (!PlayerPrefs.HasKey("TotalCoins"))
{
PlayerPrefs.SetInt("TotalCoins",0);
}
}
// returns the number of coins stored in PlayerPrefs
public int GetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
return PlayerPrefs.GetInt("TotalCoins");
}
// if we don't have the key set, so return 0
return 0;
}
// sets a number of coins into PlayerPrefs if the current coin count is greater
public void SetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
int oldCoins = PlayerPrefs.GetInt("TotalCoins");
if (currentCoinCount > oldCoins)
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}
}
}
// display the coin count as a TextUI
public void ShowCoinCount()
{
if (coinText != null)
{
coinText.text = currentCoinCount.ToString();
}
}
//new method for oldcoin count
public void AddCoins(int coins)
{
currentCoinCount = currentCoinCount + coins;
}
}
// sets a number of coins into PlayerPrefs if the current coin count is greater
Well, this comment basically tells you the problem. If the coin count isn't greater than what was saved previously, it doesn't save!
Change this:
// sets a number of coins into PlayerPrefs if the current coin count is greater
public void SetCoinCount()
{
if (PlayerPrefs.HasKey("TotalCoins"))
{
int oldCoins = PlayerPrefs.GetInt("TotalCoins");
if (currentCoinCount > oldCoins)
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}
}
}
To this:
// sets a number of coins into PlayerPrefs
public void SetCoinCount()
{
PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}

C# GUI Texture Button Script

I've finally found a script that can be used with a GUI Button in an IOS project. I am using Unity3d game engine. I'm a little familiar with JavaScript buttons and animation but am not familiar at all with C#. My problem is not knowing were to write the function that will play a queued animation in the C# button script when the button is touched. Below is copy of the IOS Button Script and then the code I have to play the queued animation.
using UnityEngine;
using System.Collections;
public enum Btn
{
normal,
hover,
armed
}
[System.Serializable] // Required so it shows up in the inspector
public class ButtonTextures
{
public Texture normal=null;
public Texture hover=null;
public Texture armed=null;
public ButtonTextures() {}
public Texture this [ButtonState state]
{
get
{
switch(state)
{
case ButtonState.normal:
return normal;
case ButtonState.hover:
return hover;
case ButtonState.armed:
return armed;
default:
return null;
}
}
}
}
[RequireComponent(typeof(GUITexture))]
[AddComponentMenu ("GUI/Button")]
public class GuiButton : MonoBehaviour
{
public GameObject messagee;
public string message = "";
public string messageDoubleClick = "";
public ButtonTextures textures;
protected int state = 0;
protected GUITexture myGUITexture;
private int clickCount = 1;
private float lastClickTime = 0.0f;
static private float doubleClickSensitivity = 0.5f;
protected virtual void SetButtonTexture(ButtonState state)
{
if (textures[state] != null)
{
myGUITexture.texture = textures[state];
}
}
public virtual void Reset()
{
messagee = gameObject;
message = "";
messageDoubleClick = "";
}
public bool HitTest(Vector2 pos)
{
return myGUITexture.HitTest(new Vector3(pos.x, pos.y, 0));
}
public virtual void Start()
{
myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture;
SetButtonTexture(ButtonState.normal);
}
public virtual void OnMouseEnter()
{
state++;
if (state == 1)
SetButtonTexture(ButtonState.hover);
}
public virtual void OnMouseDown()
{
state++;
if (state == 2)
SetButtonTexture(ButtonState.armed);
}
public virtual void OnMouseUp()
{
if (Time.time - lastClickTime <= doubleClickSensitivity)
{
++clickCount;
}
else
{
clickCount = 1;
}
if (state == 2)
{
state--;
if (clickCount == 1)
{
if (messagee != null && message != "")
{
messagee.SendMessage(message, this);
}
}
else
{
if (messagee != null && messageDoubleClick != "")
{
messagee.SendMessage(messageDoubleClick, this);
}
}
}
else
{
state --;
if (state < 0)
state = 0;
}
SetButtonTexture(ButtonState.normal);
lastClickTime = Time.time;
}
public virtual void OnMouseExit()
{
if (state > 0)
state--;
if (state == 0)
SetButtonTexture(ButtonState.normal);
}
#if (UNITY_IPHONE || UNITY_ANDROID)
void Update()
{
int count = Input.touchCount;
for (int i = 0; i < count; i++)
{
Touch touch = Input.GetTouch(i);
if (HitTest(touch.position))
{
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
SetButtonTexture(ButtonState.normal);
}
else
{
SetButtonTexture(ButtonState.armed);
}
if (touch.phase == TouchPhase.Began)
{
if (touch.tapCount == 1)
{
if (messagee != null && message != "")
{
messagee.SendMessage(message, this);
}
}
else if (touch.tapCount == 2)
{
if (messagee != null && messageDoubleClick != "")
{
messagee.SendMessage(messageDoubleClick, this);
}
}
}
break;
}
}
}
#endif
}
Most of this seems to deal with button states, where my touch button only has one state, which is 'normal'. Should references to 'hover' and armed just be deleted? I also get an error in the console saying; "the type or namespace "Button State" could not be found. Are you missing a using directive or an assembly reference?"
The code for C# GUI Button play queued animation I want to insert reads like this:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
if (Input.GetButtonDown("Btn"))
animation.PlayQueued("shoot", QueueMode.PlayNow);
}
}
I suppose of queued animation script snippet; Input.GetButtondown....would change to
void Update() {
if(Input.GetTouch("Btn"))
animation.PlayQueued("shoot", QueueMode.PlayNow);
}
and be inserted at about line 148 of the GUI Button script. Please help me if you can, I'm
feeling down. Seriously! Any help in reformatting this script would be greatly appreciated
and used as a template as I have two other GUI buttons to setup. It maybe asking a lot or what's a heaven for?
Respectfully,
Digital D
an analog man,
in a digital world
Okay, there's a lot going on in the script, but it appears to be designed so you don't have to modify it.
What you need to do is go to the object that will be playing the animation, and create a function on it that you wish to be called when the button is clicked.
So something like (you can do this in JS if you like):
public class ShootyMan : MonoBehaviour {
public void Shoot() {
animation.PlayQueued("shoot", QueueMode.PlayNow);
}
}
Now, look at the button in the inspector. I'm not sure if you're familiar with how messaging works in Unity, but basically the button will send a "message" to a "messagee". (ie. it will call a function of name message on any script attached to the messagee).
Set the "messagee" to the GameObject with the "Shoot" function. And set the message to the string "Shoot".

Categories