Home › Page › Tutorial #3 – Transforms and other stuff

Tutorial #3 – Transforms and other stuff

Revision for “Tutorial #3 – Transforms and other stuff” created on 26 April, 2013 @ 20:45 [Autosave]

Tutorial #3 - Transforms and other stuff
<h2>#3: Referencing Elements in an UghPublisher Script</h2> In Tutorial #2, we made an <a title="UghButton" href="http://ugh.graveck.com/wiki/ugh-scripting-reference/ughbutton/">UghButton</a> and made it print a message when it was pressed by using an <a title="UghPublisher" href="http://ugh.graveck.com/wiki/ugh-scripting-reference/ughpublisher/">UghPublisher</a>. In Tutorial #1, we made an UghText. Now we're going to add that <a title="UghText" href="http://ugh.graveck.com/wiki/ugh-scripting-reference/ughtext/">UghText</a> to that UghPublisher and change what the UghText says whenever we press that UghButton. Note that we are picking up from the end of Tutorial #2, so we have a dialog box, a button, and some text on both. To follow along from this point, open up the Tutorial #3 scene under Tutorials-&gt;Scenes. You can either follow along with the video, or step through the written tutorial below. https://www.youtube.com/watch?v=9kAKKcnYHyM&amp;feature=youtu.be <h4>Part 1 - Referencing UghText in script</h4> <ol> <li>Create an UghText. Refer to Tutorial #1 if you're not sure how.</li> <li>Find the UghTextReferences array in your UghPublisher. Set its size to 1, and drag the UghText into the Reference field of the first element.</li> <li>Enter a Name for your UghText in the Ugh Text References array. We are going to use this Name to change our UghText later, so remember it! For this tutorial, we'll name ours "Awesome Text".</li> <li>Go into our UghPublisher script (AwesomePublisher.cs, if you've been following along), and copy in the following lines. Make sure they rewrite the function OnAwesomeButtonPressed that we already had in there.</li> </ol> [sourcecode language="csharp"] private int numberOfAwesomeTimes = 0; public void OnAwesomeButtonPressed() { if (ughTexts[&quot;Awesome Text&quot;] != null) { numberOfAwesomeTimes++; ughTexts[&quot;Awesome Text&quot;].Text = &quot;This dialog has been Awesome &quot; + numberOfAwesomeTimes + &quot; time&quot; + (numberOfAwesomeTimes == 1 ? &quot;!&quot; : &quot;s!&quot;); } } [/sourcecode] <ol start="5"> <li><span style="line-height: 13px;">Save the script and go back to the Unity editor.</span></li> </ol> If you Play this scene and click on the UghButton now, you will see the UghText change its contents. Take note of how we referenced our UghText in script: ughTexts[<em>string with the name of the UghText</em>]. You can access ALL Ugh elements in a very similar way if you have added them to any UghPublisher!  (<strong>Tip:</strong>  When assigning a new string to an UghText, make sure you remember to assign it to the "Text" variable with a capital 'T'.  There is also a "text" variable with lowercase 't' — it has to be public, but don't use it to set the UghText's contents.) <h4>Part 2 - Transforms</h4> You can connect literally any <a title="Unity Transform" href="http://docs.unity3d.com/Documentation/ScriptReference/Transform.html">Transform</a> to an UghPublisher, not just UghButtons and UghTexts. To illustrate this, let's add an arbitrary Transform to our UghPublisher and make it move when we click our button. <ol> <li>Create a new button. Add it to the UghPublisher button array with the Function Name "OnMoveButtonPressed." Its purpose will be to move the Transform that we select.</li> <li>I'd like to move the entire dialog as a Transform. There are several ways I can do this. For now, the way I'll be doing it is to parent everything except the UghPublisher to the dialog background, then to parent the dialog background to the UghPublisher (to keep the UghButtons under the UghPublisher in the hierarchy). Now, if we were to move the dialog background sprite, the entire dialog would move.</li> <li>Highlight the UghPublisher object, find the Transform References array, drop it down, and set its size to 1.</li> <li>Drag the dialog background Transform into the new slot in the Transform References array in the UghPublisher. Name it "Panel Sprite." Note that there is an Ugh Sprite References array in every UghPublisher, and it would also work to add the dialog background sprite to that array instead. However, the UghSprite list doesn't need to be used in this case, since we don't need any specific UghSprite functionality, like changing the UghSpriteDefinition on the sprite. Since we just want to change its location, we'll add it to the Transform References list.</li> <li>Go into AwesomePublisher.cs (our UghPublisher script), and add this code to it:</li> </ol> [sourcecode language="csharp"] private Vector3 originalPanelPos = Vector3.zero; void Start () { originalPanelPos = transforms[&quot;Panel Sprite&quot;].position; } public void OnMoveButtonPressed() { if (transforms[&quot;Panel Sprite&quot;] != null) { transforms[&quot;Panel Sprite&quot;].position = new Vector3(originalPanelPos.x, Random.Range(-2f, 2f), originalPanelPos.z); BroadcastMessage(&quot;Align&quot;); } } [/sourcecode] <ol start="6"> <li>The button in our project will now assign our panel's position a random y coordinate. You can adjust the Randomness to fit your own layout.</li> </ol> That's all for this one! Next up, we'll look at UghAligns, those powerful and flexible organizers, helpful for making nearly any Ugh layout neater and more professional. <a title="Tutorial #3 – The UghAlign" href="http://ugh.graveck.com/wiki/mini-tutorials/tutorial-3-the-ughalign">Next tutorial</a>

OldNewDate CreatedAuthorActions
26 April, 2013 @ 20:45 [Autosave] admin
25 April, 2013 @ 21:54 admin
17 April, 2013 @ 19:26 admin
17 April, 2013 @ 14:58 admin
16 April, 2013 @ 16:33 admin
16 April, 2013 @ 16:28 admin
16 April, 2013 @ 16:15 admin
16 April, 2013 @ 16:13 admin
16 April, 2013 @ 16:11 admin
16 April, 2013 @ 16:10 admin
15 April, 2013 @ 22:18 admin
11 April, 2013 @ 15:46 admin
11 April, 2013 @ 15:41 admin
9 April, 2013 @ 17:16 admin
9 April, 2013 @ 16:57 admin
9 April, 2013 @ 16:56 admin
8 April, 2013 @ 19:10 admin
8 April, 2013 @ 18:48 admin
8 April, 2013 @ 18:47 admin
5 April, 2013 @ 18:17 admin
4 April, 2013 @ 19:28 admin
4 April, 2013 @ 13:34 admin
2 April, 2013 @ 17:13 admin
2 April, 2013 @ 17:05 admin
2 April, 2013 @ 15:40 admin
2 April, 2013 @ 14:44 admin
29 March, 2013 @ 20:20 admin
29 March, 2013 @ 16:35 admin
29 March, 2013 @ 16:08 admin
29 March, 2013 @ 15:56 admin
29 March, 2013 @ 15:56 admin