Well, I thought I would try to be clever and create a class for my score popup which I could instantiate at any time through a single line of code. That will save time right? Well , I use following line of code to trigger a score popup (a little +200 above the gameObject), but it seems that I can only trigger one of these at a time.(ie the first object has to finish its anim before the next object can trigger a popup) I kind of thought that the class would be newly instantiated every time an object called the TriggerPopUp method so I could have hundreds of these things at once. How can I do that?
TriggerPopUp("+200",gameObject);
And the code for this class is here:
#pragma strict
import TMPro;
/*
Call this using TriggerPopUp("+200",SenderGameObject);
*/
public static class ScorePopup extends MonoBehaviour{
var targetObject : Transform; //the 3d object5 to align to
var score :RectTransform;
var canvasRectT : RectTransform ;
var textMesh : TextMeshProUGUI ;
var popUpScore :GameObject;
var popUpExists : boolean = false;
function Update () {
}
//always position the score at the parent obejct
function PositionUI()
{
print("POSITIONING:");
var screenPoint :Vector2 = RectTransformUtility.WorldToScreenPoint(Camera.main,targetObject.position);
score.anchoredPosition = screenPoint - canvasRectT.sizeDelta / 2f;
}
//make the score visible and animate it
function TriggerPopUp(textToDisplay : String,go:GameObject)
{
//print("Clicked in class");
if(popUpExists){ return;}
//create pop up score from prefab
popUpScore = Instantiate(Resources.Load("PopUpScore") , Vector3 (0, 0, 0), Quaternion.identity);
popUpScore.transform.parent = go.transform;
popUpScore.name = popUpScore.name.Replace("(Clone)","");
targetObject=go.transform;
canvasRectT = go.transform.FindChild("PopUpScore/").gameObject.GetComponent(RectTransform);
score = go.transform.FindChild("PopUpScore/Score Text").gameObject.GetComponent(RectTransform);
textMesh = score.gameObject.GetComponent(TextMeshProUGUI);
yield WaitForSeconds(0.01);
PositionUI();
if(popUpExists){ return;}
//var reset =true;
popUpExists =true;
textMesh.text=""+textToDisplay;
textMesh.color.a=1;
score.gameObject.transform.localScale=Vector3(1,1,1);
PositionUI();
//animate
iTween.ScaleBy(score.gameObject,iTween.Hash("x",1.5,"y",1.5,"time",0.5));
yield WaitForSeconds(0.5);
PositionUI();
//fade out
while (textMesh.color.a>0 )
{
textMesh.color.a-=0.03;
yield;
}
popUpExists =false;
Destroy(popUpScore);
}
}
Cheers