跳转至

Module 3: UI, Interaction, Game Manager, Gradual Changes, Autonomous Behavior

Coverage

Unity-specific skills you will learn, practice, and demonstrate include:

  • User Interface
    • Integrating the editor between the GameObject world and the UI coordinate system.
    • Working with UI.RectTransform.
  • Packages
    • Creating and importing Unity packages.

Concepts you will explore and understand include:

  • The lerp function for gradual rotation and chasing.
  • Simple implementations of finite state machines (FSMs).
  • Randomness and its simple applications in games.

1. User Interface and Packages

Example based on Module-2 Example-4:

Run Behavior

  • Press the spacebar to launch an egg at the slider-bar-value interval.

Package Import

  1. Go to Assets → Import Package, and select SliderWithEcho.unitypackage.
  2. From the GitHub repository, look under ClassExample/Packages.
  3. Locate and import:
    • PreFab: UI-SliderWithEcho.
    • Scripts/UI: SliderWithEcho.cs.
  4. To create your own package:
    • Use Assets → Export and select the appropriate components, ensuring to include necessary scripts.

UI: Using the Imported Package

  1. Ensure your scene includes a Canvas (UI → Canvas).
  2. Drag the SliderWithEcho prefab into the Canvas.
    • Align the anchor to the lower-left.
    • Rename the prefab to SpawnRate.
  3. Configure the SpawnRate in the Inspector:
    • mLabelText: Set this text field.
    • SliderValue: Customize its appearance (e.g., color).
    • SliderBar: Adjust Min Value, Max Value, and Init Value.

Note

Avoid manually adjusting the positions of SliderWithEcho’s child objects.

GreenArrowBehavior: Reference to SliderWithEcho

  • Drag the SliderWithEcho (SpawnRate) to the GreenArrowBehavior object in the editor.
  • The value() of SpawnRate determines the interval between spawning new eggs.

Additional Concepts

  • Time.time: The elapsed time in seconds since the game started.
  • Package Operations: Learn about importing/exporting prefabs and managing dependencies.

Alternate Implementation

  • Use SliderBar’s callback (event handler) to handle value changes:
    • Pros: Avoid polling in Update().
    • Cons: Implicit event references can make debugging difficult.

Exercise

  • Implement this alternative approach and explore UI overflow issues, particularly with long text values.

2. Cool Down Bar

A continuation of the previous example:

Run Behavior

  • Press the spacebar to launch eggs while observing the cool-down bar.

Package Import

  1. Import CoolDownBar.unitypackage.
  2. Locate and use:
    • PreFab: UI-CoolDownBar.
    • Scripts/UI: CoolDownBar.cs.

GreenArrowBehavior: Reference

  • Link both SliderWithEcho and CoolDownBar to the respective UI instance variables in the editor.

Key Takeaway

The CoolDownBar encapsulates spawn rate logic, illustrating the importance of object-oriented design.


4. Gradual Rotation and Chasing

Run Behavior

  • WASD: Move the egg.
  • UpArrow: Chase the egg.

Slider Behavior

  • 0: No rotation.
  • 1: Always points at the egg.
  • Between 0 and 1: Gradual rotation.

Learned Concepts

  • Gradual rotation with Vector3.LerpUnclamped.
  • Reusable PointAtPosition() for rotation alignment.

6. Autonomous Movement with Randomness

Run Behavior

  • GreenArrow chases the egg. Upon reaching it, the egg spawns in a random position.

Scene Setting

  • Main Camera includes CameraSupport.
  • GameManager initializes necessary components and defines logic for randomness and boundaries.

8. Finite State Machine

Run Behavior

  • Touch the plane to trigger a series of states (e.g., enlarge, rotate, shrink).

FSM Implementation

  • Use enum for state definitions and transitions.
  • Case statements and service functions handle each state.

10. FSM + Randomness

Run Behavior

  • Planes cycle through states with randomized durations.

Learned Concepts

  • Integrating randomness into FSM for natural behaviors.
  • Abstracting logic for easier expansion.

7. Orbiting

Run Behavior

  • Eggs orbit around the hero.
  • Modify properties (e.g., orbit radius and speed) from the editor.

Learned Concepts

  • Implementing orbit logic efficiently with minimal code.
  • Understanding quaternion operations for advanced movement.