Skip to content

Develop in Unity

Franklin Chen edited this page Jan 4, 2018 · 1 revision

Once finished Configure on Dev Center, you can switch to Unity for developing. A complete sample scene can be found at Assets\Examples\Main.unity.

  1. Create a new Unity project.

    New Unity Project

  2. Import the Unity plugin and Enable Xbox Live

  3. Sign into Xbox Live in Unity

    Drag the UserProfile and XboxLiveServices prefab onto the scene. And also add a EventSystem from GameObject > UI.

  4. Set up stats

    1. Drag two IntegerStat prefabs onto the scene, set one's name to "ScoreStat" and the other's to "KillsStat".

    2. For ScoreStat, set its ID to score and Display Name to High Score which are configured on Dev Center.

      Score Stat

    3. For KillsStat, set its ID to kills and Display Name to Kill Count. This stat is not configured on Dev Center and is used for "Shooting Master" achievement.

      Kills Stat

    4. Drag two StatPanel prefabs onto the scene, also set one's name to "ScoreStatPanel" and the other's to "KillsStatPanel".

    5. For the ScoreStatPanel, set its Stat to ScoreStat in Inspector. For the ScoreStatPanel, set its Stat to KillsStat.

      Score Stat Panel

    6. Add two UI buttons to your scene, and in the OnClick event of each button, in the Unity inspector, add an IntegerStat object, and call the Increment function to increase the value of the stat by one every time you click the button.

      Increment Button

    7. Add a "FlushStatsButton" UI button to call RequestFlushToService method to flush the data manually.

  5. Display global leaderboard

    Drag a Leaderboard prefab onto the scene, set the Stat to ScoreStat, Leaderboard Type to Global and Entry Count to 3.

  6. Set up "Shooting Master" achievement

    1. Create a ShootingMasterAchievement class inherited from AchievementBase.

      using System;
      
      public class ShootingMasterAchievement : AchievementBase
      {
          public int targetNumber;
      
          private IntegerStat stat;
      
          // Use this for initialization
          void Start()
          {
              stat = GetComponent<IntegerStat>();
              if (stat != null)
              {
                  stat.StatChanged += UnlockAchievement;
                  base.XboxLiveUser = stat.XboxLiveUser;
              }
          }
      
          public override uint CalculateProgress()
          {
              return (uint)Math.Round(Convert.ToDouble(stat.Value) / targetNumber * 100);
          }
      }
    2. And add it as an component of KillsStat. So that KillsStat can be used to keeps track of the kill count that needed to unlock the achievement.

    3. In Inspector, set ID to 1 and TargetNumber to 10.

  7. Save the scene.

After all these done, you can Build and test the project.