Skip to content

Instantly share code, notes, and snippets.

@charlieamat
charlieamat / Instructions.md
Last active September 30, 2021 20:18
[ta-edu-course-survival-game] Chapter 2 — ScriptableObjects
  • Create ItemType.cs
  • Make ItemType derive from ScriptableObject
  • Add a string field called "DisplayName"
    • We could add more meta information (e.g., stats, description, icon, etc.), but we don’t need to at the moment
    • Each instance of ItemType will represent a type of item and will be referenced in our code instead of strings
    • But how do we create item types?
  • Add the CreateAssetMenu attribute
    • Like components that derive from MonoBehaviour, scriptable objects don't support constructors and can't be instantiated with new
    • You can use ScriptableObject.CreateInstance
  • But the CreateAssetMenu allows you to create your scriptable object assets right from the Unity editor, which is what we want
@charlieamat
charlieamat / Interactable.cs
Last active September 30, 2021 18:15
[ta-edu-course-survival-game] Chapter 2 — Composition vs Inheritance (2)
public abstract class Interactable : MonoBehaviour
{
public abstract void Interact();
}
@charlieamat
charlieamat / Achievements.cs
Last active September 30, 2021 18:12
[ta-edu-course-survival-game] Chapter 2 — Composition vs Inheritance (1)
public class Achievements : MonoBehaviour
{
public void Track(string identifier)
{
// Track achievememnt progress
}
}
@charlieamat
charlieamat / Instructions.md
Last active September 23, 2022 21:32
[ta-edu-course-survival-game] Chapter 2 — MonoBehaviours & GameObjects
  • Create Interactor.cs
    • We want to let the Player interact with the environment
    • Or in other words, we want the Player to be an interactor of the environment
  • Create Interactable.cs
    • Interactors needs something to interact with, so let's define a new component called "Interactable"
  • Open Interactable.cs
  • Implement a public method called Interact()
    • The idea here is that Interactors will call this method
  • Add a Debug.Log() statement to Interact()
  • Debug.Log("I've been interacted with!", gameObject)
@charlieamat
charlieamat / Instructions.md
Last active September 13, 2021 19:58
[ta-edu-course-survival-game] Chapter 2 — Your First Script
  1. In Unity, Right-click the _Project/Scripts folder, expand the Create menu, and click C# Script
  2. Name the new file "Inventory" and open it in Visual Studio
  3. Add a public String array called "Items"
  4. Initialize the Items array in the Start method
    • e.g., "Sword", "Potion", "Scroll"
  5. Use a for-loop to iterate over the Items array
    • Log each item to the console
  6. Back in Unity, run the scene
    • Point out that nothing happens because the script hasn't been instantiated
  7. Add the Inventory component to the Player GameObject in the Scene
@charlieamat
charlieamat / Instructions.md
Last active September 14, 2021 15:55
[ta-edu-course-survival-game] Chapter 2—Performance 101 (2)
  1. Comment out the existing logic (except for _nearestInteractable)
    • We’re going to re-write Interactor using under-performant code
  2. Implement the Update method
    • Instead of reacting to GameObjects that invoke the “OnTrigger” methods, we’re going to poll the scene for interactables
  3. In Update, declare and initialize a variable called “interactables” using FindObjectsOfType()
    • Like the “GetComponent” methods, Unity has a set of “Find” methods that allow you to search the scene for GameObjects
    • These are generally unsafe because you can’t always predict how many objects will actually be in your scene
  4. Add a foreach that loop that iterates over interactables
  5. Check if _nearestInteractable is null:
  • If it is, set it to the current interactable
@charlieamat
charlieamat / SimpleClass.cs
Created November 10, 2020 23:09
An example of the compiler generating classes for anonymous functions.
using UnityEngine;
using UnityEngine.Events;
public class SimpleClass : MonoBehaviour
{
private UnityEvent _event;
private void Awake()
{
_event.AddListener(() =>
@charlieamat
charlieamat / Bootstrap.cs
Last active March 29, 2023 08:52
A custom Log4Net Appender that outputs log statements to the Unity console.
using System.IO;
using log4net.Config;
using UnityEngine;
public static class Bootstrap
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void ConfigureLogging()
{
XmlConfigurator.Configure(new FileInfo($"{Application.dataPath}/log4net.xml"));
public class MyCollaborator : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}
}
using UnityEngine;
using UnityEngine.Events;
public class Goal : MonoBehaviour {
public Players scoresTo;
public IScoreEvent scoreEvent;
public void Construct(Players scoresTo, IScoreEvent scoreEvent) {
this.scoresTo = scoresTo;