These are notes from the Complete C# Unity Game Developer 2D Udemy Course
UI
Right click -> Create canvas
Any element that shows up on the canvas needs to be a child of the Canvas.
Canvases use Screen Space instead of game space. The camera takes care of world space, but screen space is something entirely different.
Some common things you’ll want to do:
- Create an image as a child of the canvas. Right click the canvas and UI -> Image
- Use the Rect Transform on the image to set its position. Hold down shift to change the anchor point, and alt to also set position. It’s good to hold both down at once. You can also stretch elements to full screen here.
- Create a pre-visualization layer so you can see what you expect your UI to look like (that doesn’t actually do anything) and set it later.
Grid Layout
For things you need organized together, you can make them children of an object such as AnswerButtonGroup. From that empty object, you can add stuff like buttons and other menu items. Add a component called “Vertical/Horizontal Layout Group” and it will automatically organize the children for you.
Scriptable Objects
Change the class type to ScriptableObject
when creating a new script, no start or step event needed.
[createAssetMenu( menuName = “Quiz Question”, fileName = “New Question” )] – lets you take the script you created and use it in the Unity UI by right clicking -> Add new -> [SO]
Getter Methods
- Gives a script read-only access to a private variable
- Protects the contents of a private variable
string question = "Enter new question text";
public string GetQuestion()
{
return question;
}
Lists
Like Arrays, but you can change their size.
Arrays:
Int [ ] oddNumbers = new int[5]
Methods:
array.length – return the length of array
Lists:
List<int> oddNumbers = new List<int>()
The first part declares the type of List we’re creating ( a list of ints ), stores it in a variable name and then creates a new list.
Methods:
List.Count – Return length of list.
Lust.Contains(3) – Check if item exists.
List.Add( 3 )- Add an item.
List.Remove(3) – Remove an item.
List.RemoveAt(0) – remove item at given index
List.Clear() – clear the list.
Things to Look Up
- How do attributes work in Unity?
- What is going on here?
[Header("Scoring")]
[SerializeField] TextMeshProUGUI scoreText;
ScoreKeeper scoreKeeper;
void Start()
{
timer = FindObjectOfType<Timer>();
scoreKeeper = FindObjectOfType<ScoreKeeper>();
}
ScoreKeeper is the class I created I think, but then later we assign it to the object that has the script attached to it?
- I don’t understand why we add a TextMeshUI object, then add a separate ScoreKeeper object…
- Why
Leave a Reply