- Create
ItemType.cs
- Make
ItemType
derive fromScriptableObject
- 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 ofstring
s - 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
- Like components that derive from MonoBehaviour, scriptable objects don't support constructors and can't be instantiated with
- But the
CreateAssetMenu
allows you to create your scriptable object assets right from the Unity editor, which is what we want
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Interactable : MonoBehaviour | |
{ | |
public abstract void Interact(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Achievements : MonoBehaviour | |
{ | |
public void Track(string identifier) | |
{ | |
// Track achievememnt progress | |
} | |
} |
- 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 toInteract()
Debug.Log("I've been interacted with!", gameObject)
- In Unity, Right-click the _Project/Scripts folder, expand the Create menu, and click C# Script
- Name the new file "Inventory" and open it in Visual Studio
- Add a public String array called "Items"
- Initialize the Items array in the Start method
- e.g., "Sword", "Potion", "Scroll"
- Use a for-loop to iterate over the Items array
- Log each item to the console
- Back in Unity, run the scene
- Point out that nothing happens because the script hasn't been instantiated
- Add the Inventory component to the Player GameObject in the Scene
- Comment out the existing logic (except for _nearestInteractable)
- We’re going to re-write Interactor using under-performant code
- Implement the Update method
- Instead of reacting to GameObjects that invoke the “OnTrigger” methods, we’re going to poll the scene for interactables
- 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
- Add a foreach that loop that iterates over interactables
- Check if _nearestInteractable is null:
- If it is, set it to the current interactable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
public class SimpleClass : MonoBehaviour | |
{ | |
private UnityEvent _event; | |
private void Awake() | |
{ | |
_event.AddListener(() => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyCollaborator : MonoBehaviour | |
{ | |
private void OnCollisionEnter(Collision collision) | |
{ | |
foreach (ContactPoint contact in collision.contacts) | |
{ | |
Debug.DrawRay(contact.point, contact.normal, Color.white); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |