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

Tutorial #3 – Transforms and other stuff

Mini Tutorials > Tutorial #3 - Transforms and other stuff

#3: Referencing Elements in an UghPublisher Script

In Tutorial #2, we made an UghButton and made it print a message when it was pressed by using an UghPublisher. In Tutorial #1, we made an UghText. Now we’re going to add that UghText 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->Scenes.

You can either follow along with the video, or step through the written tutorial below.

Part 1 – Referencing UghText in script

  1. Create an UghText. Refer to Tutorial #1 if you’re not sure how.
  2. Find the UghTextReferences array in your UghPublisher. Set its size to 1, and drag the UghText into the Reference field of the first element.
  3. 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”.
  4. 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.

private int numberOfAwesomeTimes = 0;

public void OnAwesomeButtonPressed()
{

    if (ughTexts["Awesome Text"] != null)
    {

        numberOfAwesomeTimes++;
        ughTexts["Awesome Text"].Text = "This dialog has been Awesome " + numberOfAwesomeTimes + " time" + (numberOfAwesomeTimes == 1 ? "!" : "s!");

    }

}

  1. Save the script and go back to the Unity editor.

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[string with the name of the UghText]. You can access ALL Ugh elements in a very similar way if you have added them to any UghPublisher!  (Tip:  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.)

Part 2 – Transforms

You can connect literally any Transform 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.

  1. 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.
  2. 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.
  3. Highlight the UghPublisher object, find the Transform References array, drop it down, and set its size to 1.
  4. 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.
  5. Go into AwesomePublisher.cs (our UghPublisher script), and add this code to it:
private Vector3 originalPanelPos = Vector3.zero;

void Start ()
{

    originalPanelPos = transforms["Panel Sprite"].position;

}

public void OnMoveButtonPressed()
{

    if (transforms["Panel Sprite"] != null)
    {

        transforms["Panel Sprite"].position = new Vector3(originalPanelPos.x, Random.Range(-2f, 2f), originalPanelPos.z);
        BroadcastMessage("Align");

    }

}
  1. 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.

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.

Next tutorial