Hello,
im currently making a 2D Quad based GUI, using a quad mesh that is rotated towards an orthographic camera and has a box collider attached. The quads are only rendered by the orthographic camera due to layer setup.
the orthographicsize is set up as UICam.orthographicSize=Screen.height/2;
It seems that the BoxColliders position is somehow totaly messed up and i dont know how to fix that. the actual collider seems to be behind the camera, but with turned on gizmos i can see a collider right around my button :x
I place my UI elements all per code, so this might be the problem. When I Place them into scene before pressing play it does work correctly.
It looks like it has something todo with the Z-axis, when I tweak it up to 430, the colliders position is correct, but only inside the editor, in a webplayer build its again imprecise.
I can’t find any mistakes, can you maybe checkout my code?
public override void SetupProperties(int[] _frameCounts, string[] _MenuItemNames, Vector2[] _positions, Vector2[] _scales, bool[] _isenabled, Material _mat, Mesh framemesh){
/*This is a fake Constructor method*/
ElementCount = _frameCounts.Length;
MenuItems = new GameObject[_frameCounts.Length];
Items = new UIDisplay[_frameCounts.Length];
frameCounts = _frameCounts;
MenuItemNames = _MenuItemNames;
positions = _positions;
scales = _scales;
mat = _mat;
Vector3 startpoint= new Vector3(-3000,0,0); // location to spawn new buttons out of view
for(int i = 0; i < _frameCounts.Length; i++){
MenuItems[i] = (GameObject)Instantiate(FramePrefab,Vector3.zero+startpoint, Quaternion.identity) as GameObject;
MenuItems[i].name=MenuItemNames[i]+" Button";
Items[i] = MenuItems[i].GetComponent<UIDisplay>();
Debug.Log(MenuItems[i].name+" has been created!");
MenuItems[i].layer = 8; // 8 has to be UI Layer
MenuItems[i].tag = "MenuItem";
Items[i].setUVPos(frameCounts[i], (int)positions[i].x, (int)positions[i].y, (int)scales[i].x, (int)scales[i].y, this); // not related
Items[i].disable();
MenuItems[i].transform.parent = transform;
MenuItems[i].transform.localPosition=Vector3.zero;
MenuItems[i].transform.localEulerAngles=new Vector3(-90,0,0); // rotate button on X to make it look towards camera
}
StartCoroutine("rollOut");
}
public override IEnumerator rollOut () // Play flying button animation
{
float minDistanceSQ=6*6; // Squared MinDistance
float speed=12; // Movement Speed
Vector3 target=new Vector3(((Screen.width/2)-(MenuItems[0].transform.localScale.x/2)),0,430); //The button will fly to the screens right border/2-buttonwidth/2
for(int i=0;i < ElementCount; i++){
Vector3 spawnposition=new Vector3(Random.Range(-200,200),Random.Range(-200,200),430); // The button appears at this location
MenuItems[i].transform.position=spawnposition;
while(minDistanceSQ < DistanceSQ(MenuItems[i].transform.position , target)){ // Play the fly animation
MenuItems[i].transform.localPosition+=((target-MenuItems[i].transform.position).normalized)*speed; // add direction*speed to current position, towards target
yield return new WaitForSeconds(0.0001F); // sleep
}
MenuItems[i].transform.localPosition=target; // jump to target position as mindistance is good enough
target+=new Vector3(0,(MenuItems[i].transform.localScale.y/2)+5,0); // change target slightly in y axis, next button will fly to this location
}
EnableElements(true); // activate buttons for press access
StopCoroutine("rollOut"); // terminate the coroutine
}
This is the only place where the Issue can be as nowhere else I modified the position or scale of the MenuItems.
How can that be that placing something by hand into scene works good, but moving it there by code does cause issues??:roll: