Hey guys!
I have tried all different variations and nothing works, so I just ask you straightaway, even it might be a very dumb mistake.
I constructed a menu: http://wapple.ch/noah/Tron/Tron.html … when you press M you see the Menu.
But just AFTER you move the Cursor. I know that it is because of line103 of this Code:
using UnityEngine;
using System.Collections;
public class Circle{
public Circle[] subs;
public Texture textureMain;
public Texture textureHover;
public string name;
private bool isBuildingUp;
private bool isBuildingDown;
private Vector2 center;
private float angle;
private int subsBuilt;
private float r;
private float timeToBuildUp;
private Vector2 middle;
private bool started;
private Circle parent;
private bool active;
private Vector2 mouse;
public Circle(string name, Vector2 center, float r, float timeToBuildUp, Texture textureMain, Texture textureHover){
this.subs = new Circle[0];
this.center = center;
this.r = r;
this.timeToBuildUp = timeToBuildUp;
this.textureMain = textureMain;
this.textureHover = textureHover;
this.middle = new Vector2();
this.started = false;
this.name = name;
this.parent = null;
this.subsBuilt = 0;
this.isBuildingUp = true;
this.active = false;
this.mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
}
public void setSubs(Circle[] subs){
this.subs = subs;
foreach(Circle sub in this.subs){
sub.parent = this;
}
}
public void Update(){
//Debug.Log(Event.current.mousePosition);
Debug.Log(1);
this.mouse = new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y);
if(started){
if(isBuildingUp){
angle += Time.deltaTime * 360 / timeToBuildUp;
if(angle >= 360f / subs.Length){
angle -= 360f / subs.Length;
subsBuilt++;
}
if(subsBuilt == subs.Length - 1)
isBuildingUp = false;
}
}
foreach(Circle sub in subs){
if(active){
if(sub.Click(r)){
Debug.Log("Clicked " + sub.name);
if(sub.subs.Length == 0){
stopAll();
}
else{
sub.start();
deactivate();
}
}
}
sub.Update();
}
if(active){
Event current = Event.current;
if(current.type == EventType.MouseUp current.button == 1){
stop();
current.Use();
}
if(Click(r)){
stop();
current.Use();
}
if(current.keyCode == KeyCode.Escape){
Debug.Log("Abort all");
stopAll();
current.Use();
}
if(current.keyCode == KeyCode.M parent == null){
Debug.Log("Start");
if(!started)
start();
// else
// stopAll();
current.Use();
Debug.Log("Swaaaaaaaag");
}
}
}
public bool Click(float size){
Event current = Event.current;
if(current.type == EventType.MouseUp current.button == 0)
if((center - mouse).sqrMagnitude <= size * size){
current.Use();
return true;
}
else
return false;
else
return false;
}
public bool Hover(float size){
if((center - mouse).sqrMagnitude <= size * size)
return true;
else
return false;
}
private void DrawParent(){
if(parent == null || parent.started){
if(!Hover(r))
GUI.DrawTexture(new Rect(center.x - r, center.y - r, r * 2, r * 2), textureMain);
else
GUI.DrawTexture(new Rect(center.x - r, center.y - r, r * 2, r * 2), textureHover);
}
}
private void DrawChildren(){
float radius = r / Mathf.Sin(Mathf.PI / subs.Length);
middle = new Vector2(0, - radius);
if(isBuildingUp){
int i = 0;
bool rotated = false;
foreach(Circle sub in subs){
float a;
sub.center = center + middle;
if(active){
if(!sub.Hover(r))
GUI.DrawTexture(new Rect(sub.center.x - r, sub.center.y - r, r * 2, r * 2), sub.textureMain);
else
GUI.DrawTexture(new Rect(sub.center.x - r, sub.center.y - r, r * 2, r * 2), sub.textureHover);
}
if(i < subsBuilt){
a = 360f / subs.Length;
}
else{
a = angle;
rotated = true;
}
if(!rotated){
Vector3 middle3 = new Vector3(middle.x, 0, middle.y);
Quaternion q = Quaternion.AngleAxis(- a, Vector3.up);
middle3 = q * middle3;
middle = new Vector2(middle3.x, middle3.z);
}
i++;
}
}
else{
foreach(Circle sub in subs){
sub.center = center + middle;
if(active){
if(!sub.Hover(r))
GUI.DrawTexture(new Rect(sub.center.x - r, sub.center.y - r, r * 2, r * 2), sub.textureMain);
else
GUI.DrawTexture(new Rect(sub.center.x - r, sub.center.y - r, r * 2, r * 2), sub.textureHover);
}
Vector3 middle3 = new Vector3(middle.x, 0, middle.y);
Quaternion q = Quaternion.AngleAxis(-360f / subs.Length, Vector3.up);
middle3 = q * middle3;
middle = new Vector2(middle3.x, middle3.z);
}
}
foreach(Circle sub in subs){
sub.Draw();
}
}
private void DrawRelation(){
int length;
float angle;
if(parent != null){
//Relation from parent to current
length = (int)(parent.center - center).magnitude;
angle = Mathf.Atan2((center - parent.center).x, (center - parent.center).y) * Mathf.Rad2Deg;
GUIUtility.RotateAroundPivot(-angle, parent.center);
GUI.DrawTexture(new Rect (parent.center.x - 1, parent.center.y, 2, length), textureMain);
GUIUtility.RotateAroundPivot(angle, parent.center);
}
if(active){
//Relation from current to cursor
length = (int)(mouse - center).magnitude;
angle = Mathf.Atan2((mouse - center).x, (mouse - center).y) * Mathf.Rad2Deg;
GUIUtility.RotateAroundPivot(-angle, center);
GUI.DrawTexture(new Rect (center.x - 1, center.y, 2, length), textureMain);
GUIUtility.RotateAroundPivot(angle, center);
}
}
public void Draw(){
if(started){
DrawParent();
DrawChildren();
DrawRelation();
}
}
public void start(){
Debug.Log("Started " + name);
this.subsBuilt = 0;
this.isBuildingUp = true;
this.started = true;
activate();
}
public void stopAll(){
stop();
if(parent != null)
parent.stopAll();
}
public void stop(){
Debug.Log("Stopped " + name);
this.subsBuilt = 0;
this.isBuildingUp = true;
this.started = false;
deactivate();
if(parent != null)
parent.activate();
}
public void activate(){
this.active = true;
Debug.Log("Activated " + name);
}
public void deactivate(){
this.active = false;
Debug.Log("Deactivated " + name);
}
}
But when you click on Button number 4 (top one is 1) of the first menu then the sub menu opens and everything is displayed fine and smoothly updated, but as you can see on line 113 I use event.Use() as well …
How can it work one time and one time not?
Any help is appreciated
Comments on ugly code too ![]()
Maybe you wanna have a look in the script which is attached to an empty GO and where the menu object is instantiated:
using UnityEngine;
using System.Collections;
public class CircleUI : MonoBehaviour {
Circle circle;
Vector2 center;
public Texture text;
public Texture text2;
// Use this for initialization
void Start () {
Circle[] elements = new Circle[10];
for(int i = 0; i < elements.Length; i++){
elements[i] = new Circle(i.ToString(), Vector2.zero, 30f, 1, text, text2);
elements[i].textureMain = text;
elements[i].textureHover = text2;
}
Circle[] elements2 = new Circle[10];
for(int i = 10; i < elements.Length + 10; i++){
elements2[i-10] = new Circle(i.ToString(), Vector2.zero, 30f, 1, text, text2);
elements2[i-10].textureMain = text;
elements2[i-10].textureHover = text2;
}
Circle[] elements3 = new Circle[10];
for(int i = 20; i < elements.Length + 20; i++){
elements3[i-20] = new Circle(i.ToString(), Vector2.zero, 30f, 1, text, text2);
elements3[i-20].textureMain = text;
elements3[i-20].textureHover = text2;
}
center = new Vector2(Screen.width/2, Screen.height/2);
circle = new Circle("Test", center, 30f, 1, text, text2);
circle.setSubs(elements);
circle.subs[3].setSubs(elements2);
circle.subs[3].subs[8].setSubs(elements3);
circle.activate();
}
// Update is called once per frame
void Update () {
// if(Input.GetKeyUp(KeyCode.M))
// circle.start();
}
void OnGUI(){
circle.Update();
circle.Draw();
}
}
Thanks in advance!
Peace Rynns