I’m making an interactive comic, and the first panel should slide down immediately. Then the different parts of the panel animate in, and it’s all very pretty. This works without flaws in the editor, OS X build and web build. However, when I build it for Android, the panel doesn’t slide in automatically, and when you touch the screen it slides down way too far and the first animation happens, but is slightly offset. All following animations work as expected. I’m at a loss as to why this happens, because the only difference in the code between Android and anything else is that it is a touch event rather than pressing the spacebar which makes all the actions happen. That’s all. Here is the code:
public class Controller : MonoBehaviour
{
public static int sequenceNo;
public GameObject[] panels;
private int activePanel;
// Use this for initialization - called when the object is loaded
void Start ()
{
sequenceNo = -1;
activePanel = 0;
}
// Update is called once per frame
void Update ()
{
GetInput ();
foreach (GameObject panel in panels)
{
Panel pScript = panel.GetComponent<Panel> (); // Get the panel script, which allows me to use public variables and functions
if (pScript.panelNumber == activePanel) // if the panel is active
{
if (!pScript.transitioning) // if the panel is not mid-transition
{
panel.SetActive(true); // activate the panel
pScript.TransitionIn(); // transition in
}
if (sequenceNo != -1 && sequenceNo < pScript.numSequences) // if the sequence number is not -1 (panel transition) and is in range
pScript.ActivateSequence(sequenceNo); // activate the correct animation
if (sequenceNo >= pScript.numSequences) // if the sequence number is greater than the amount of animations in this panel
{
sequenceNo = -1; // reset the sequence number
++activePanel; // move to the next panel
pScript.ResetLerp (); // reset lerp variables (otherwise the animation doesn't play)
pScript.TransitionOut (); // move the panel out of view
}
}
if (pScript.panelNumber != activePanel)
if (!pScript.transitioning)
panel.SetActive(false);
}
}
void GetInput ()
{
if (Input.GetKey (KeyCode.Escape)) // 'Back' key on Android
Application.Quit (); // Quit the application
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.WP8Player)
{ // if the platform requires touch input
if (Input.GetTouch (0).phase == TouchPhase.Began) // as soon as a touch is registered
++sequenceNo; // increment the sequence
} else {
if (Input.GetKeyDown (KeyCode.Space)) // if the platform doesn't have touch input
++sequenceNo; // increment the sequence
}
}
}
public class Panel : MonoBehaviour
{
// set in the inspector
public int numSequences;
public string transitionIn;
public string transitionOut;
public float transitionSmoothing;
public int panelNumber;
public bool transitioning = false;
public GameObject[] objects;
private Vector3 endPos;
private Vector3 startPos;
private Vector3 finishPos;
private Color startCol;
private Color endCol;
private Vector3 startSize;
private Vector3 endSize;
private float lerpTime = 1f;
private float currentLerpTime = 0f;
void Start ()
{
transitionIn = transitionIn.ToUpper (); //allows multiple variations, e.g. Zoom, zoom, ZOOM, etc
transitionOut = transitionOut.ToUpper ();
//position the panel so it enters from the correct direction
if (transitionIn.Contains ("LEFT"))
transform.position = new Vector3 (22, 0);
else if (transitionIn.Contains ("RIGHT"))
transform.position = new Vector3 (-22, 0);
else if (transitionIn.Contains ("DOWN"))
transform.position = new Vector3 (0, 16);
else if (transitionIn.Contains ("UP"))
transform.position = new Vector3 (0, -16);
else
transform.position = new Vector3 (0, 0);
//position the panel so it leaves in the correct direction
if (transitionOut.Contains ("LEFT"))
finishPos = new Vector3 (-22, 0);
else if (transitionOut.Contains ("RIGHT"))
finishPos = new Vector3 (22, 0);
else if (transitionOut.Contains ("DOWN"))
finishPos = new Vector3 (0, -16);
else if (transitionOut.Contains ("UP"))
finishPos = new Vector3 (0, 16);
else
finishPos = new Vector3 (0, 0);
startPos = transform.position; // get the current position
endPos = new Vector3(0f, 0f, 0f); // get the target position
endCol = GetComponent<SpriteRenderer> ().color; // get the current color
startCol = new Color (endCol.r, endCol.g, endCol.b, 0f); // get the same color with alpha , so the panel fades in
startSize = new Vector3 (0f, 0f, 0f); // get the start size
endSize = transform.localScale; // get the target size
if (transitionIn.Contains ("FADE"))
GetComponent<SpriteRenderer> ().color = startCol; // if the panel is going to fade in, make it invisible to start
else if (transitionIn.Contains ("ZOOM"))
transform.localScale = startSize; // if the panel is going to zoom in, make it tiny to start
}
public void ResetLerp()
{ // reset the lerp
lerpTime = 1;
currentLerpTime = 0;
}
public void ActivateSequence (int num)
{
if (objects [num].GetComponent<_genericPanelObject> ()) // if there is a panel object attached to the script
{
objects [num].GetComponent<_genericPanelObject> ().active = true; // activate it
objects [num].gameObject.SetActive (true);
}
}
public void TransitionIn()
{
// choose which action to perform (set in the inspector)
if (transitionIn.Contains ("SLIDE"))
StartCoroutine(Slide (startPos, endPos));
else if (transitionIn.Contains ("FADE"))
StartCoroutine(Fade (startPos, endPos, startCol, endCol));
else if (transitionIn.Contains ("ZOOM"))
StartCoroutine(Zoom (startPos, endPos, startSize, endSize));
else
StartCoroutine(Slide (startPos, endPos));
}
public void TransitionOut()
{
// choose which action to perform (set in the inspector)
if (transitionOut.Contains ("SLIDE"))
StartCoroutine(Slide (endPos, finishPos));
else if (transitionOut.Contains ("FADE"))
StartCoroutine(Fade (endPos, finishPos, endCol, startCol));
else if (transitionOut.Contains ("ZOOM"))
StartCoroutine(Zoom (endPos, finishPos, endSize, startSize));
else
StartCoroutine(Slide (endPos, finishPos));
}
IEnumerator Slide(Vector3 start, Vector3 end)
{
while (transform.position != end)
{
transitioning = true;
currentLerpTime += Time.deltaTime * transitionSmoothing;
if (currentLerpTime > lerpTime)
currentLerpTime = lerpTime;
float t = currentLerpTime / lerpTime;
t = Mathf.Sin (t * Mathf.PI * 0.5f);
transform.position = Vector3.Lerp (start, end, t);
yield return null;
}
transitioning = false;
yield return new WaitForSeconds (0f);
}
IEnumerator Fade(Vector3 sPos, Vector3 ePos, Color sCol, Color eCol)
{
StartCoroutine(Slide (sPos, ePos));
while (GetComponent<SpriteRenderer> ().color != eCol)
{
float t = currentLerpTime / lerpTime;
t = Mathf.Sin (t * Mathf.PI * 0.5f);
GetComponent<SpriteRenderer> ().color = Color.Lerp (sCol, eCol, t);
foreach (Transform child in transform)
{
if (child.GetComponent<SpriteRenderer> ())
child.GetComponent<SpriteRenderer> ().color = Color.Lerp (sCol, eCol, t);
}
yield return null;
}
yield return new WaitForSeconds (0f);
}
IEnumerator Zoom(Vector3 sPos, Vector3 ePos, Vector3 sSize, Vector3 eSize)
{
StartCoroutine(Slide (sPos, ePos));
while (transform.localScale != eSize)
{
float t = currentLerpTime / lerpTime;
t = Mathf.Sin (t * Mathf.PI * 0.5f);
transform.localScale = Vector3.Lerp (sSize, eSize, t);
yield return null;
}
yield return new WaitForSeconds (0f);
}
}