Unlock Items
How to persistently unlock items using PlayerData, using simple in-world achievements as a demo.
Visit the Unlock Items Example World to try it for yourself!
Using the Example
- Enter the world
- Perform actions to unlock achievements
- Spend time in the world (10sec,2min, 5min)
- Move around (10 units, 100 units, 300 units)
- Respawn
- Find the secret
- All of the above
- Observe the UI showing your unlocked achievements
- Leave and return - your achievements and stats (time spent in world, distance travelled) should persist
Importing the Example
Follow the steps below to add this example to your Unity project:
- Open the Example Central Window from the window from the Unity Editor Menu under "VRChat SDK > 🏠 Example Central"
- Find this prefab in the list or search for it by title (same as the title of this page).
- Press the "Import" button to import the Unitypackage into your project.
Technical Breakdown
The main UnlockItems
script checks for the local player's achievements when their data first loads in during the OnPlayerRestored
method, and restores all the unlocked items that are found.
It then triggers the UpdateStats
method to run, which runs the following methods:
- CheckTimeInWorld();
- CheckDistanceMoved();
- CheckHasPlayerRespawned();
- CheckHasFoundSecret();
- CheckAllAchievementsUnlocked();
After all methods are run and achievements updated, this method calls itself to run again after a delay of UPDATE_STATS_TIME
.
For each achievement that is unlocked during the UpdateStats
check, the UnlockAchievement
method is called, which updates the sprite, color and text of the target achievement.