I am a digital artist by trade, and trying to hammer my way through scripting. I am hoping I am going about this the right way (I am starting by trying to build tiny scripts and build up on them.)
I am trying to make an on screen toggle switch, that when in the "on " state, starts the camera scrolling at a certain speed in a single axis, and when in the "off" stage stays where it stops.
I am using the built in Unity toggle that address 2 different scripts with an If..ELse statement, that addresses 2 scripts. My questions are: 1. How can I make the camera keep moving at a certain speed until the toggle is tripped again? Right now it only moves a set distance per click.
I've been doing a lot of digging around but I haven't been able to get it right looking at the reference and other examples so far, but I am hoping to prevail out of sheer stubbornness.
This is what I have so far.. baby steps right?
Hers's how you'd go about it at the very basic level (ignoring custom textures):
Note how the *GUI.Toggle function accepts the "ignition" value as one of its inputs (which determines the current state to display), and it also returns a value which is put back into the "ignition" variable. The value it returns is usually unchanged, except on frames where the toggle was actually clicked, when it returns the new toggled value.
To add graphics for the toggle states, you need to use a custom GUI Style for that toggle. This can be done fairly easily by adding a GUIStyle variable, and then dragging textures into the appropriate slots in the inspector. Your modified script would look like this:
And then you would look in the inspector and expand the "style" variable shown in your script. Within, you will see the names of lots of states such as "Normal", "Hover", "Focused", etc. If you expand these, you'll see each has a background texture slot, and a text color selector. You can drag a texture into the background texture slot for each style to assign a texture to that state.
The states you need to modify for a toggle button are:
Hope this helps!
Ah! Thank you! for both the scripts and the explanations, I see I was attempting a more complicated route. The JS is exactly what I was looking for, but the C is also great for me to look through and learn a little more about.
answered Dec 05 '10 at 01:11 AM
Well I hope I can help out , even though I don't ever use JS, I do use C#. And adding Ducks sample you can do pretty much what you asked.
What I cna try to do to help is show you a bit of a C# design I wrote for this Q&A session. Hopefully it can help you get you on your way if you don't want to stick to JS or just want some design experience.
This design tries to keep an open mind so you can add and remove components or states without to much trouble, which is a "hard-mode" for programming but in the long run is a big pay. I think there should be something in here that can help you get on your way. Feel free to ask more!
Good luck with the project, nice to see artists going coding, just don't overdo it ok , we still want our jobs :D
Here's the code:
answered Nov 16 '10 at 10:16 AM