- Home /
Spot the difference game having a strange behavior in Mobile
I'm making a spot the difference game but with words, like this: (When a word is clicked it tells you if there is one of the differents ones or not). As you can see it works fine in the editor, but when I test it on Android this happen Not pretty right? My principal suspect is the Canvas Scaler but i don't know how to fix this mess. Help please
Just in case here is my code:
public class TextArranger : MonoBehaviour
{
[SerializeField] private RectTransform context;
[SerializeField] private WordUI prefabWord;
[SerializeField] private BoxCollider2D bounds;
// (.....)
public void showWords(List<string> texts)
{
int i = 0;
if (texts.Count > 0)
{
foreach (string word in texts)
{
WordUI wordUI = Instantiate(prefabWord, context.transform);
//set word on UI
int wordLength = wordUI.setPalabra(word);
wordUI.isDif=checkChanged(i);
//check if it fit in the same line
isSameLine(wordLength);
//set word position
wordUI.setLocation(lastX, lastY);
//set vars for next word
setNextPos(wordUI, wordLength);
i++;
}
}
}
private bool checkChanged(int i)
{
foreach (int dist in textos.GetDistintos())
if (dist == i)
return true;
return false;
}
private void isSameLine(int wordLength)
{
wordLength += offsetPalabras;
float aux = bounds.bounds.size.x - margenDer;
var aux2 = wordLength + lastX;
Debug.Log("la palabra llega hasta " + aux2 + " y el margen derecho esta en " + aux);
if (wordLength + lastX > (bounds.bounds.size.x - margenDer))
{
lastX = 12;
lastY -= 15;
}
}
private void setNextPos(WordUI wordUI, int wordLength)
{
wordLength += offsetPalabras;
lastX += wordLength;
}
}
public class WordUI : MonoBehaviour
{
[SerializeField] private Text palabra;
public bool isDif;
public RectTransform location;
internal void setLocation(float x, float y)
{
location.anchorMin = new Vector2(0, 1);
location.anchorMax = new Vector2(0, 1);
location.pivot = new Vector2(0, 1);
location.transform.position = new Vector3(x, y, 0);
}
private int CalculateMessageLength(string message)
{
int totalLength=0;
Font myFont = palabra.font; //chatText is my Text component
CharacterInfo characterInfo = new CharacterInfo();
char[] arr = message.ToCharArray();
foreach (char c in arr)
{
myFont.RequestCharactersInTexture(c.ToString(), palabra.fontSize, palabra.fontStyle);
myFont.GetCharacterInfo(c, out characterInfo, palabra.fontSize);
totalLength += characterInfo.advance;
}
return totalLength;
}
internal int setPalabra(string word)
{
palabra.text = word;
return CalculateMessageLength(palabra.text);
}
}
untitled.png
(124.7 kB)
Comment