- Home /
GUITexture multiple component activation
Hi guys,
I basically have a certain number of GUITextures in my scene and i want to be able to scroll through each one seperately. Generally what will happen is; the player will start off with GUITexture1 and when they press either a left or right button, they should go to a previous or next GUITexture.
So TLDR; i want to scroll through multiple GUITextures by enable/disable but at the moment it goes through to my second GUITexture(notes2) and then quickly onto 3. How do i get it to stay on 2?
Here is what i have tried so far:
if (rightButton.tapCount>0&¬eSwitch == NoteSwitch.Notes1)
{
noteSwitch = NoteSwitch.Notes2;
}
else if (rightButton.tapCount>0&¬eSwitch == NoteSwitch.Notes2)
{
noteSwitch = NoteSwitch.Notes3;
}
if (leftButton.tapCount >0&¬eSwitch == NoteSwitch.Notes3)
{
noteSwitch = NoteSwitch.Notes2;
}
else if (leftButton.tapCount >0&¬eSwitch == NoteSwitch.Notes2)
{
noteSwitch = NoteSwitch.Notes1;
}
I should update.
So i tried it with an array and funnily enough the same problem occurs. Starts with note 1 and shows 2 for a brief split second and goes to 3.
What i'll do is break down my hierarchy, game objects and the main script i have putting it all together. If anyone could chime in, i would be grateful.
So basically in this scene; what i want to achieve is a controlling a set of GUITextures with some basic Buttons i have created.
The 4 buttons do the following:
Up and Down scroll up and down on the GUITextures(Notes1,2 and 3). Left and Right are meant to select a GUITexture and put it on display. A basic previous/Next Button.
$$anonymous$$y Notes$$anonymous$$anager will be handling and putting to action what i describe above.
I have a script running on the buttons that handles touch and i use tapCount on each to get a reading for which one is being pressed.
Here is my main Script i have attached on Notes$$anonymous$$anager:
public class TouchGUINotes : $$anonymous$$onoBehaviour
{
//The buttons we'll be using to flip through the notepad
public $$anonymous$$PJoystick upButton;
public $$anonymous$$PJoystick downButton;
public $$anonymous$$PJoystick rightButton;
public $$anonymous$$PJoystick leftButton;
//We'll use this to transform the GUITexture.
private Rect pixelinset;
private Rect pixelinset2;
private Rect pixelinset3;
//Array for the notes + a current note that will be used to display the current note.
public GUITexture[] notepad;
private GUITexture currentNote;
//index for the array.
private int index = 0;
// Update is called once per frame
void Update ()
{
#region scroll notes
//Unless the note is being displayed, through current note, do not enable.
foreach (var note in notepad)
{
note.enabled = false;
}
//Scroll Controls.
if (upButton.tapCount > 0)
{
pixelinset = currentNote.pixelInset;
pixelinset.y += new Vector2(0, 5).y;
currentNote.pixelInset = pixelinset;
}
if (downButton.tapCount > 0)
{
pixelinset = currentNote.pixelInset;
pixelinset.y += -new Vector2(0, 5).y;
currentNote.pixelInset = pixelinset;
}
#endregion
//Controls to change GUITexture
if (rightButton.tapCount >= 1)
{
index++;
}
if (leftButton.tapCount >= 1)
{
index--;
}
index = $$anonymous$$athf.Clamp(index, 0, notepad.Length - 1);
currentNote = notepad[index];
currentNote.enabled = true;
}
}
@nobleGas - I don't know the $$anonymous$$PJoystic class, you need to check out how it is processing the tap count. Between line 38 and 39, and between line 44 and 45, insert the following code:
Debug.Log(Time.frameCount);
If you get more than one value per click, then you know this is the heart of your issue. If you don't, you can rule out that as an issue
Okay i think i see the problem here. Time.frameCount shows that it is being pressed more than once, like 200/300 times the amount.
I've removed $$anonymous$$PJoystick and put a much simpler script that counts an Int if it detects a touch on the function On$$anonymous$$ouseDown().
public class TouchButton : $$anonymous$$onoBehaviour
{
public int touchCount;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (touchCount==1)
{
touchCount = 0;
}
}
void On$$anonymous$$ouseDown()
{
touchCount++;
}
This allows me to display the right GUITexture but my issue is that the framecount is still high, im getting upwards 1000. The other issue is that the Up and Down scroll buttons now make the game freeze.
Thanks for all your help so far.
Answer by robertbu · Jan 17, 2014 at 04:29 PM
You need to put some 'else' clauses in your code.
if (rightButton.tapCount>0&¬eSwitch == NoteSwitch.Notes1)
{
noteSwitch = NoteSwitch.Notes2;
}
else if (rightButton.tapCount > 0 && noteSwitch == NoteSwitch.Notes2)
{
noteSwitch = NoteSwitch.Notes3;
}
if (leftButton.tapCount > 0 && noteSwitch == NoteSwitch.Notes3)
{
noteSwitch = NoteSwitch.Notes2;
}
else if (leftButton.tapCount > 0 && noteSwitch == NoteSwitch.Notes2)
{
noteSwitch = NoteSwitch.Notes1;
}
Note this kind of behavior is easier handled by using an array rather that state and somewhat complex 'if' statements.
Thanks for the reply.
I just put in the else if statements, still doing the same thing. How would i do it with an array? Go through each variable with a foreach statement?
For your current code is rightButton.tapCount true only for the current frame?
You are not calling this from inside OnGUI() are you? OnGUI() gets called multiple times per frame.
As for an array, if you textures were in an array, you would just manipulate the index to the array. Something like:
if (rightButton.tapCount > 0)
index++;
if (leftButton.tapCount > 0)
index--;
index = $$anonymous$$athf.Clamp(index, 0, lengthOfArray-1);
Then index would be used when you display the texture to pick the correct texture.
Im calling rightbutton.tapcount from inside update. I tried it from inside fixedUpdate and LateUpdate, and the same issue occurs. Im currently trying the array idea.
Im just running into a few problems, first of all; what is index? Is it an int? Here is what im trying :
public GUITexture[] notepad;
if (rightButton.tapCount > 0)
notepad++;
if (leftButton.tapCount > 0)
notepad--;
I know im doing something wrong but not sure which way up to try arrays.
Your answer
Follow this Question
Related Questions
Augmented reality - LeanTouch/Lean Touch - What to do with multiple objects & how to reset position 1 Answer
iOS touch triggers the wrong object 0 Answers
Debug iPhone game without smartphone. 1 Answer
Processes two touches instead of one 0 Answers
What is the protocol Accepted by TUIO Input for asset Touchscript? 0 Answers