Quiz Generator Unity Asset

Quiz Generator Logo

Overview

Quiz Generator is a system to generate an unlimited number of random questions. Quiz Generator can be used to generate questions for a unique and complete Trivia/Quiz game (such as Geography Trivia and The Beatles Trivia). It can also be embedded inside another game and be exposed as a mini game.

Features

  • Fully commented C# code.
  • Perfect for creating trivia games
  • Contains a fully working example!
  • Contains randomization engine, that ensures unique questions!
  • Works on all platforms, PC, Mac, iOS, Android, etc.
  • Easy integration with Puppeteer’s Triva Quiz Game (READ DOCS)

Purchase Quiz Generator from the Unity Assets Store here >>

Screenshots

How to use Quiz Generator

Define your object model

Decide on 2 or more objects that will be the base of your data set and define the relations between them. For example:

  • Actor
  • Character
  • Movie

And their relations:

  • An Actor portrays a Character
  • An Actor appears in a Movie
  • A Character appears in a Movie

And of course, you have their completing relations:

  • A Character is portrayed by an Actor
  • A Movie features an Actor
  • A Movie features a Character

Create your questions

Implement a class that implements the IQuestionGenerator interface for each question type. For example:

public class WhichActorAppearsInMovie : IQuestionGenerator
{
    public QuizQuestion GetQuestion(IQuiz quiz)
    {
        var actor = quiz.OfType("Actor").RandomSingleSample();
        var companies = quiz.Relationships<ActorAppearsInMovie>(actor, "Movie");

        // Select a random movie
        var movie = quiz.OfType("Movie").RandomSingleSample();
        // Find all the actors that appears in the movie
        var actors = quiz.Relationships<MovieFeaturesActor>(movie, "Actor");

        // Select a random actor as the "correct answer";
        var oneActor = actors.RandomSingleSample();
        if (oneActor == null)
            return null;

        // Find 3 random actors that **DON'T** appar in the movie
        var wrongActors = quiz.OfType("Actor")
            .Where(p => !actors.Contains(p))
            .RandomSample(3).ToList();
        // If we don't have 3 wrong answers, we don't have a question
        if (wrongActors.Count < 3)
            return null;

        // Define the question
        var question = new QuizQuestion(
            string.Format("Which of the following appears in {0}?", movie.name),
            oneActor,
            oneActor.ToList());
        return question;
    }

    public int GetPossibleSubjects(IQuiz quiz)
    {
        return quiz.CountOfType("Movie");
    }

    public int QuestionTypeLevel { get { return 1; } }
}

Create your Quiz object

Quiz Generator comes with a QuizBuilder helper class, that can load the data from a JSON stream:

var qb = new QuizBuilder<MoviesQuizData>(new[] { new WhichActorAppearsInMovie() });
using (var nodesStream = File.OpenRead("Assets\\Data\\Movies.json"))
{
    qb.Load(nodesStream);
}

The JSON stream should be the JSON representation of the provided IQuizData object – in the above case – MoviesQuizData.

By Code

Creating the dataset by code consists of just creating a collection of QuizObject and QuizRelation objects:

var actors = new List<QuizObject>();
actors.Add(new QuizObject { Id = "Actor1", Name = "Tom Hanks", Type = "Actor" });
actors.Add(new QuizObject { Id = "Actor1", Name = "Angelina Jolie", Type = "Actor" });

var movies = new List<QuizObject>();
movies.Add(new QuizObject { Id = "Movie1", Name = "Forrest Gump", Type = "Movie" });

var relations = new List<QuizRelation>();
relations.Add(new ActorAppearsInMovie(actors[0], movies[0]));

var quiz = new Quiz();
foreach (var actor in actors)
    quiz.AddObject(actor);
foreach (var movie in movies)
    quiz.AddObject(movie);

quiz.AddRelations(relations);

Note: It is your responsibility to add all relations, including their completing relations to the quiz.

Generating questions

Create an instance of the QuestionGenerator class, and provide it with all the questions you created in the previous step:

var questionsGenerator = new QuestionsGenerator(quiz);
questionsGenerator.Generators = new List<IQuestionGenerator>
{
    new Questions.WhichActorAppearsInMovie(),
    new Questions.WhichMovieFeaturesActor(),
    new Questions.WhichMovieFeaturesCharacter(),
};
questionsGenerator.Init();

You can now use the questionGenerator to generate 10 questions:

questionsGenerator.GetQuestions().Take(10);

FAQ

Where can I see samples of using the system?

Several Trivia games already use the system:

How can I use the Quiz Generator system?

You can create your own Trivia game from scratch, and make it look exactly the way you want. You can also embed the Quiz Generator system as part of a bigger game and use the generated questions as a mini game.

You can also use any of the existing Trivia game templates in the Assets Store – I strongly recommend Trivia Quiz Game Template by Puppeteer. You can read my tutorial on how to use Quiz Generator with Puppeteer’s Trivia Quiz Game.

I need more complex data in order to generate my questions…

You can implement your own classes of QuizObject, which add additional properties. In the provided sample, there are examples of implementations – SeaQuizObject adds an “IsOcean” property, which can be used when generating questions.

Subscribe now

and receive the latest updates and news.