- Home /
Question by
justadeveloper · Jan 09, 2016 at 10:45 PM ·
uipositionrandomrecttransformanchor
this code doesnt work when the buttons anchor are not same(custom anchor)
this code works only if the buttons have same anchor presets but it doesnt work when buttons have different anchor presets(custom) how to make this work?
using UnityEngine;
using System.Collections;
public class ShuffleChoice : MonoBehaviour
{
public GameObject option1;
public GameObject option2;
public GameObject option3;
public GameObject option4;
bool stat2=false;
bool stat3=false;
bool stat4=false;
public float x;
public float y;
public int mover;
public int before=1;
public int movequestion=0;
public int count=1;
// Use this for initialization
void Start ()
{
RectTransform choice1 = option1.GetComponent<RectTransform> ();
RectTransform choice2 = option2.GetComponent<RectTransform> ();
RectTransform choice3 = option3.GetComponent<RectTransform> ();
RectTransform choice4 = option4.GetComponent<RectTransform> ();
x = choice1.anchoredPosition.x;
y = choice1.anchoredPosition.y;
do
{
randomcount();
if (randomcount () == 2 && stat2 == false)
{
if(count==1)
{
choice1.anchoredPosition = new Vector2 (choice2.anchoredPosition.x,choice2.anchoredPosition.y);
choice2.anchoredPosition = new Vector2 (x, y);
}
else if(before==3)
{
choice3.anchoredPosition = new Vector2 (choice2.anchoredPosition.x, choice2.anchoredPosition.y);
choice2.anchoredPosition = new Vector2 (x, y);
}
else if(before==4)
{
choice4.anchoredPosition = new Vector2 (choice2.anchoredPosition.x, choice2.anchoredPosition.y);
choice2.anchoredPosition = new Vector2 (x, y);
}
x = choice2.anchoredPosition.x;
y = choice2.anchoredPosition.y;
count++;
stat2 = true;
before=2;
//Debug.Log ("count=" + count);
}
else if (randomcount () == 3 && stat3 == false)
{
if(count==1)
{
choice1.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
choice3.anchoredPosition = new Vector2 (x, y);
}
else if(before==2)
{
choice2.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
choice3.anchoredPosition = new Vector2 (x, y);
}
else if(before==4)
{
choice4.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
choice3.anchoredPosition = new Vector2 (x, y);
}
x = choice3.anchoredPosition.x;
y = choice3.anchoredPosition.y;
count++;
stat3 = true;
before=3;
}
else if (randomcount () == 4 && stat4 == false)
{
if(count==1)
{
choice1.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
choice4.anchoredPosition = new Vector2 (x, y);
}
else if(before==2)
{
choice2.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
choice2.anchoredPosition = new Vector2 (x, y);
}
else if(before==3)
{
choice3.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
choice4.anchoredPosition = new Vector2 (x, y);
}
x = choice4.anchoredPosition.x;
y = choice4.anchoredPosition.y;
count++;
stat4 = true;
before=4;
//Debug.Log ("count=" + count);
}
} while(count<=3);
}
void update()
{
//randomcount ();
}
int randomcount()
{
mover = Random.Range (1,5);
return mover;
}
}
Comment