- Home /
How will i update my table values every time?
Hi All,
I am developing sample simple pendulum experiment. i done that with the help of Mr.Robertbu.
My pendulum code is like this:
using UnityEngine;
using System.Collections;
public class Pendulum2 : MonoBehaviour {
public Transform pivot;
public static float speed=0.0f ;
private Vector3 v3Pivot; // Pivot in screen space
private bool dragging = false;
private float startAngle = 0.0f;
private float endAngle = -180.0f;
private float fTimer = 0.0f;
private float angleDiff;
private Vector3 v3T = Vector3.zero;
private float angle;
private float anglespeed,angles1,angles2;
public GUIStyle fontstyle;
private float length1,length2,length3;
private float Timeperiod=0.0f;
//var fontSize : int = 20;
private int count=0;
private float fPrev;
void Start () {
v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
}
void Update() {
if( !dragging ) {
if (anglespeed>0 && anglespeed<180)
{
length1 = ((anglespeed*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>180 && anglespeed<=270)
{
angles1 = anglespeed-180;
angles2 = 180-angles1;
length1 = ((angles2*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
else if(anglespeed>-90 && anglespeed<0)
{
angles1 = anglespeed+90;
length1 = ((angles1*2*(Mathf.PI)*GUIMenu.StripeValue)/180);
speed=length1/Timeperiod;
}
float f = (Mathf.Sin (fTimer * speed - Mathf.PI / 2.0f) + 1.0f) / 2.0f ;
v3T.Set (0.0f, 0.0f, Mathf.Lerp(startAngle, endAngle, f));
pivot.eulerAngles = v3T;
fTimer += Time.deltaTime;
Timeperiod=(2*(Mathf.PI)*(Mathf.Sqrt(GUIMenu.StripeValue/9.81f))) ;
Debug.Log(startAngle+","+endAngle);
if (f >= 0.5 && fPrev < 0.5) {
if(startAngle>endAngle){
startAngle -=1;
endAngle +=1;
count +=1;
}
else {
startAngle = 270;
endAngle = 270;
GUIMenu.abo = true;
}
}
fPrev = f;
}
}
void OnMouseDown() {
dragging = true;
Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
}
void OnMouseDrag() {
Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
float angleT = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
angleDiff = Mathf.DeltaAngle(angle, angleT);
pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
angle = angleT;
}
void OnMouseUp() {
Vector3 v3T = Camera.main.WorldToScreenPoint(transform.position);
v3T = v3T - v3Pivot;
float angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
Debug.Log (angle);
anglespeed=angle+90;
if (angle < 0.0f)
angle += 360.0f;
count = 0;
startAngle = angle;
if (angle <= 90.0f) {
endAngle = angle - 2.0f * angle - 180.0f;
}
else if (angle <= 180.0f) {
endAngle = angle + 2.0f * (180.0f - angle) + 180.0f;
}
else if (angle <= 270.0f) {
endAngle = angle + 2.0f * (270.0f - angle);
}
else {
endAngle = angle - 2.0f * (angle - 270.0f);
}
if(startAngle < endAngle) {
float temp = startAngle;
startAngle = endAngle;
endAngle = temp;
}
dragging = false;
fTimer = 0.0f;
}
void OnGUI()
{
GUI.Label (new Rect (Screen.width-(Screen.width/3.5f), 20, 200, 130), " Timeperiod :"+Timeperiod,fontstyle);
GUI.Label (new Rect (Screen.width-(Screen.width/5), 50, 200, 130), " Count :"+count,fontstyle);} }
And My GUi Code is like this:
using UnityEngine; using System.Collections;
public class GUIMenu : MonoBehaviour {
public static float StripeValue,bottomValue ;
public GameObject Stripe,Bottom;
public int x ,y;
public Transform pivot;
public bool realBool = false;
public bool OneByFourBool = false;
public bool OneBySixteenBool = false;
public GUIStyle fontstyle,pauseStyle,resumeStyle,newfontStyle;
public Rect windowRect,windowRect1,windowRect3;
public static bool temp=false;
public bool aboutFlag;
public static bool abo = false;
void Start()
{
x=20;
y=60;
StripeValue=20f;
bottomValue=15;
windowRect = new Rect(20, 10, 280, 600);
windowRect1 = new Rect(300, 20, 400, 200);
windowRect3 = new Rect(Screen.width-(Screen.width/3),Screen.height-300, 400, 200);
}
void OnGUI()
{
if(!temp){
if(GUI.Button(new Rect(700,50,60,60),"",pauseStyle))
{ Time.timeScale=0;
Pendu.stopTimer=true;
temp=true;
} }
if(temp)
{
if(GUI.Button(new Rect(700,50,60,60),"",resumeStyle))
{ temp=false;
Pendu.stopTimer=false;
Time.timeScale=1;
}}
Stripe. transform.localScale = new Vector3(1,StripeValue/33,1);
Bottom. transform.localScale = new Vector3(bottomValue*1.1f,bottomValue/24,bottomValue*1.1f);
windowRect = GUI.Window(0, windowRect, DoMyWindow, "");
if(aboutFlag)
{
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow1, "");
}
if(abo)
{
windowRect3 = GUI.Window(2, windowRect3, DoMyWindow3, "");
}
if(GUI.Button(new Rect(690,523,150,70),"Pendulum Formulas"))
{
aboutFlag=true;
}
}
void DoMyWindow1(int windowID)
{
GUI.Label(new Rect(60,40,330,40),
" T=2PI(Sqrt(Length/Gravity))",fontstyle);
if(GUI.Button(new Rect(160,150,80,30),"OK"))
{
aboutFlag=false;
}
}
void DoMyWindow3(int windowID)
{
GUI.Label(new Rect(0,0,400,100),
" S.no Angle Timeperiod Length ",fontstyle);
}
void DoMyWindow2(int windowID)
{
if(GUI.Button(new Rect(160,150,80,30),"Earth"))
{
}
if(GUI.Button(new Rect(160,150,80,30),"Moon"))
{
}
if(GUI.Button(new Rect(160,150,80,30),"Moon"))
{
}
}
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(90,20,100,40),"Real Time"))
{
Pendulum2.speed=1.0f;
}
if(GUI.Button(new Rect(90,70,100,40),"1/4 Time"))
{
Pendulum2.speed=1.0f/4.0f;
}
if(GUI.Button(new Rect(90,120,100,40),"1/16 Time"))
{
Pendulum2.speed=1.0f/16.0f;
}
if(GUI.Button(new Rect(100,310,80,30),"Moon"))
{
Pendu.angle=360;
Pendu.start=0;
Pendu.stop=360;
Pendu.speed=0;
}
if(GUI.Button(new Rect(100,350,80,30),"Earth"))
{
Pendu.angle=130;
Pendu.start=50;
Pendu.stop=130;
Pendu.speed=50;
pivot.transform.localEulerAngles =new Vector3(0,0,270);
}
if(GUI.Button(new Rect(100,390,80,30),"Gravity"))
{
Pendu.angle=90;
Pendu.start=70;
Pendu.stop=110;
Pendu.speed=50;
pivot.transform.localEulerAngles =new Vector3(0,0,270);
}
GUI.Label(new Rect(50,175,100,25),"LENGTH",newfontStyle);
GUI.Label(new Rect(50,240,100,25),"MASS",newfontStyle);
StripeValue = GUI.HorizontalSlider ( new Rect(20, 205, 200, 30), StripeValue, 8f, 30.0f);
bottomValue = GUI.HorizontalSlider ( new Rect(20, 270, 200, 30), bottomValue, 8f, 30.0f);
}}
After completion of total oscilations the pendulum comes into 0 position. on that case i want to pass s.no,angle,time period and length values to that window shown in image. how will i do that. I want every time get that values and updated in to that window.
Please help me. Thanksyou
Shankar
Take a variable to store the values and in update function display the value of that variable into this GUI box and it will work fine.......due to update function is called every frame you will get current values
i want when ever pendulum stops that values i want to pass that table. how can i do that. can u give a small type of code or any link.
Let me assume that at each and every moment when the pendulum is moving you want to store that values and at the end you want to display it like a table (Result of the test) am i right?
Yes.. After stop the pendulum i want to display result values.. i don't over ride values.. For suppose if i drag pendulum,after some time that will stop. the result will be stored into that window. in the same way second time and third time and so on.. then cape those values. i want to display like
Got it man all you have to do is have a Array to store the values and use that array in update function to store the values at every frame and at last disaply that array on GUI and you will be done.....
Answer by whydoidoit · Apr 02, 2013 at 08:22 AM
So your best bet for that would be GUILayout. You create rows and columns and fix the width of the items in them. Presuming your result data is in separate arrays It would look a bit like this (though hopefully its actually in classes):
GUILayout.BeginVertical("box");
//Title row
GUILayout.BeginHorizontal("box");
//Display the titles
GUILayout.Label("S.No", "button", GUILayout.Width(120));
GUILayout.Label("Count", "button", GUILayout.Width(120));
GUILayout.Label("Length", "button", GUILayout.Width(120));
GUILayout.Label("Timeperiod", "button", GUILayout.Width(120));
GUILayout.EndHorizontal();
//Draw the elements
for(var i = 0; i < numberOfResults; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(i.ToString(), GUILayout.Width(120));
GUILayout.Label(count[i].ToString(), GUILayout.Width(120));
GUILayout.Label(length[i].ToString() + "m", GUILayout.Width(120));
//Right justify
GUILayout.BeginHorizontal(GUILayout.Width(120));
GUILayout.FlexibleSpace();
GUILayout.Label(string.Format("{0:0.00}s", timePeriod[i]));
GUILayout.EndHorizontal();
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
I've used some simple styles for the title etc. You can create new ones based off an existing style in the code using:
var myTitleStyle = new GUIStyle("box");
Then set colours etc and use myTitleStyle in place of where I've used "button"
Your answer
Follow this Question
Related Questions
I have been struggling with a GUI window with table data for over a week now. What am I doing wrong? 1 Answer
How will i get new line in window data? 1 Answer
how will i get every time values in table window? 0 Answers
Passing values every time to table? 0 Answers
need help for Displaying Values? 0 Answers