Data Classes

확인 완료한 버전: 5.4

-

난이도: 초급

In this live training session we will look at creating a multiple choice quiz game. We will create the core game loop - timer, score, win and lose states. We will also show code architecture best practices - keeping data and logic separate, and how to structure a game in a way that makes it easy to maintain and extend. We will look at some of the ways we can extend this in session two.

Data Classes

초급 Scripting

AnswerData

Code snippet

using UnityEngine;
using System.Collections;
[System.Serializable]
public class AnswerData
{
public string answerText;
public bool isCorrect;
}

QuestionData

Code snippet

using UnityEngine;
using System.Collections;
[System.Serializable]
public class QuestionData
{
public string questionText;
public AnswerData[] answers;
}

RoundData

Code snippet

using UnityEngine;
using System.Collections;
[System.Serializable]
public class RoundData
{
public string name;
public int timeLimitInSeconds;
public int pointsAddedForCorrectAnswer;
public QuestionData[] questions;
}

Code snippet

SimpleObjectPool

Code snippet

using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances of
public GameObject prefab;
// collection of currently inactive instances of the prefab
private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefab
public GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return that
if (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instance
else
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// enable the instance
spawnedGameObject.SetActive(true);
// return a reference to the instance
return spawnedGameObject;
}
// Return an instance of the prefab to the pool
public void ReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the pool
if(pooledObject != null && pooledObject.pool == this)
{
// disable the instance
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy it
else
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}

죄송합니다...

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

확인

We use cookies to ensure that we give you the best experience on our website. Visit our cookie policy page for more information.