Week 1:
Intro and learn the tools. Please download and unzip the Week1.zip file
in the Files/WeeklyExmaples
folder.
NEED TO DO: explain when we are defining classes, when are
the classes instantiated
- When a GameObject is dragged/created in the Scene
(shows in Hierarchy)
- When a script is dragged into a GameObject that is in the
world/Hierarchy
- When a Prefab is created (in the Resource folder), NO NEW instances
are created
- Only created when we instantiate a new PreFab
Example 1. Intro To Tool: Working with GameObject
- BounceUpAndDown: scripting, the prototype: Start() + Update()
- On MyCube
- Public variables are visible on the UI editor (yRange)
- Warning: If you change this during runtime, the value WILL NOT be saved!
- Private variables are not visible
- Finding object by name: MyCylinder
- In general, learn how to formulate what you need and “google” for how to.
- I do not know how to use Unity or program it (and I am proud of it J).
- Accessing:
- Transform component: ALWAYS THERE!
- Renderer and the Material
- Creating and using: PreFab
- MyCube (in the Assets folder)
- Scene file: ClassExample
- This is like a “level” in a game
- Or simply, this is the saved working data
- Nice thing: you can have multiple versions of “saved data” per work session
- Creating a click-runnable system
- Take note of what must be included in the execution submission.
- MP (or machine problem, or programming assignment) submissions:
- EXE folder: containing something I (and the grader) can double click to test run
- Src folder: containing the “source system” where we can run the editor and interactive examine your system.
- Name your executables according to FirstLastnameMP#.exe (NO SPACE!)
- Debugging:
- Unity: Run Game
- Visual Studio: Attach to Unity (DebugàAttach Unity Debugger)
- Cameras: Notice, there are two
- Editor camera: this is the camera you use during the edit session. This camera is not visible when the game is running.
- ALT-LMB to tumble (rotate) this camera in the editor
- Main Camera: this is the camera that presents the game when you run.
- Lastly, BUT VERY IMPORTANTLY: Potential parent-child relationship of GameObject
- Find out how to establish parent-child, what is the effect (like grouping)
- We will learn much much more about this in a couple of weeks
Example 2. SimpleGUI: Creating GUI elements
- Importance of: GameObject --> UI
- Must create at least one Canvas to contain UI elements!
- EventSystem: automatically created [NEED THIS!!]
- Don’t need to change this, but DO NOT delete this.
- Rename Canvas to: MainCanvas
- Learn about Canvas and placement of GUI elements
- By default, Position specification is a percentage of the entire display
- MainController.cs: Script for the main canvas
- “using” UnityEngine.UI
- Note on Prefab instantiation: MyCube must be located in Resources folder
- Create UI elements as children to the MainCanvas! (-->RMB-->UI)
- UI elements are created in the UI-layer, see how to view/hide layers
- Notice:
- working on TWO separate groups of objects: GameObjects and UI
- Two distinct coordinate systems!!? [More about this later!]
- Structure of source code: anticipating growth
- Resources: resources to be loaded at run time
- Source: source code system, folder structure for future growth: Prefab, UI [there will be more, much more!]
- BUT, how to mouse-click to select? Before we learn about mouse-clicking, let's reaffirm variables and Unity component references ...
Example 3. Components and Variables:
- Two scripts:
- MoveInX: behavior is simple
- Bounces in X
- Allow changing of bouncing speed (Inc/Dec Delta)
- MainControl:
- Two references to components
- An object: the moving cube (Object)
- A script: the moving component on the cube
- Three buttons:
- DeleteMoveInX: delete the moving script (component)
- CreateCube + MoveInX: creates a MoveInX for the cube (what happens when a cube has two MoveInX?
- DeleteCube: deletes the cube
- Run the game: Observe the Cube, its speed alternates (controlled by MainControl). Try the following:
- Select the CubeHasMoveComponent from the Hierarchy and examine its components in the Inspector
- Click on DeleteCube'sMove, what happened?
- Click on CreateCuve + MoveInX, what happens? What happens when you click on this multiple times?
- Click on DeleteCube, and click on CreateCube again, what happens now? (A new Cube is created)
- First point to note: "C# Scripts" (MonoBehavior objects) are "components", just like GameObject
- Notice: how these things are Destroy() [same function call to remove them]
- Second point to note: components can be referenced by variables and create/delete dynamically at run time.
- Notice: both GameObject and "C# Scripts" can be created and deleted during run time.
- The action of "draggin" a script onto a GameObject to associate the defined behavior with the object, actually:
- Create a new instance of the script object (e.g., MoveInX)
- Create a reference from the GameObject to the new script (e.g., the MoveInX object.)
- This is exactly the same as calling GameObject.AddComponent<DataType>() [called in MainControl::CreateComponent()].
Example 4. MouseClickSelect: support mouse clicks
- MaiinController_MouseSupport.cs (Source/UI_Support)
- Notice C# Keyword: partial (separate source code files for the same class)
- MainController::DeleteSelectedCube();
- Implementation is simple!
- MainController::Update()
Example 5. GroupingUI: grouping related GUI objects together
- ObjectControlPanel UI element (in Hierarchy window: inside MainCanvas)
- Always (for us):
- Top-level is a Panel (so that the group can be instantiated in the MainCanvas)
- Note: the Rect Transform: reference placement (try changing the game window size and see ObjectControl is defined with respect to the lower-right corner).
- GUI elements:
- GroupLabel: a Text
- Delete: A button
- SizeControl: A slider + text (Size Control)
- Behavior: is on the top-leevel Panel (the Script object defined in Source/Prefab Support/ UIPrefab)
- NOTE:
- RectTransform: is a specialization [subclass] of Transform
- Button: is a GameObject with Scripts! Button is a script!
- call GetComponent() to access the actual button!
- New ObjectControlPanel prefeb
- Resources/UI/ObjectControl
- Source/Prefab Support/UIPrefab: the script
- To create: drag ObjectControlPanel prefeb into the world
- MainController.cs
- No need to have any support for working with selected object
- Much cleaner!! Easier to maintain!
- Problem:
- Click to select any object, drag size control slider to make the object small
- Now, click to select another object in the scene
- Note: the newly selected object size is changed to the current size control value!
- Problem: UI/Object two-way binding is not properly supported