1.游戏控制器。游戏控制器是控制游戏中不同部分之间交流的中心,将构成游戏各种各样的组件黏在一起。
2.场景管理器。这个脚本主要处理加载游戏场景保存游戏场景的任务
3.UI管理器。UI管理器绘制出游戏中的一些用户界面,用来显示用户的一些信息。
4.声音管理器。声音管理器处理声音的播放控制等。
5.玩家。玩家有时直接就存在场景中,有时候是通过游戏控制器来实例化玩家。
6.AI和敌人。和玩家一样,有些敌人一开始就在,有些是实时加载。
创建核心框架脚本
BaseGameController.cs主要功能:
1.追踪游戏状态:储存游戏状态,提供函数方法去改变游戏状态,改变游戏状态通过调用一些像EndGame()这样的函数。
2.创建玩家对象
3.设置照相机(玩家跟随)
4.状态管理,用户管理以及游戏行为之间的通信
5.一些独立的脚本或者非必要重写脚本之间的通信
一个典型的游戏应该有以下几个核心的状态:
游戏开始,暂停,加载,结束。
基本游戏控制器脚本
public class BaseGameController : MonoBehaviour {
bool paused;
public GameObject explosionPrefab;
public virtual void PlayerLostLife()
{
}
public virtual void SpawnPlayer()
{
}
public virtual void Respawn()
{
}
public virtual void StartGame()
{
}
public void Explode(Vector3 aPosition)
{
Instantiate(explosionPrefab, aPosition, Quaternion.identity);
}
public virtual void EnemyDestroyed(Vector3 aPositon, int pointsValue, int hitByID)
{
}
public virtual void BossDestroyed()
{
}
public virtual void RestartGameButtonPressed()
{
Application.LoadLevel(Application.loadedLevelName);
}
public bool Paused
{
get
{
return paused;
}
set
{
paused = value;
if (paused)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
}
场景管理
public class SceneManager : MonoBehaviour {
public string[] levelName;
public int gameLevelNum;
void Start()
{
DontDestroyOnLoad(this.gameObject);
}
public void LoadLevel(string sceneName)
{
Application.LoadLevel(sceneName);
}
public void GoNextLevel()
{
if (gameLevelNum >= levelName.Length)
gameLevelNum = 0;
LoadLevel(gameLevelNum);
gameLevelNum++;
}
private void LoadLevel(int indexNum)
{
LoadLevel(levelName[indexNum]);
}
public void ResetGame()
{
gameLevelNum = 0;
}
}
扩展MonoBehavior是避免重复定义函数和变量的好方法
public class ExtendedCustomMonoBehaviour : MonoBehaviour {
public Transform myTransform;
public GameObject myGo;
public Rigidbody myBody;
public bool didInit;
public bool canControl;
public int id;
[System.NonSerialized]
public Vector3 tempVEC;
[System.NonSerialized]
public Transform tempTR;
public virtual void SetId(int anID)
{
id = anID;
}
}
基本用户管理
储存玩家信息:姓名,得分,最高得分,登记,是否通关等信息
public class BaseUserManager : MonoBehaviour {
private int score;
private int highScore;
private int level;
private int health;
private bool isFinished;
public string playerName = "Kitty";
public virtual void GetDefaultData()
{
playerName = "Kitty";
score = 0;
level = 1;
health = 3;
highScore = 0;
isFinished = false;
}
public string GetName()
{
return playerName;
}
public void SetName(string aName)
{
playerName = aName;
}
public int GetLevel()
{
return level;
}
public void SetLevel(int num)
{
level = num;
}
public int GetHighScore()
{
return highScore;
}
public int GetScore()
{
return score;
}
public virtual void AddScore(int amount)
{
score += amount;
}
public void LostScore(int num)
{
score -= num;
}
public void SetScore(int num)
{
score = num;
}
public int GetHealth()
{
return health;
}
public void Addhealth(int num)
{
health += num;
}
public void ReduceHealth(int num)
{
health -= num;
}
public void SetHealth(int num)
{
health = num;
}
public bool GetIsFinished()
{
return isFinished;
}
public void SetIsFinished(bool aVal)
{
isFinished = aVal;
}
}
基本玩家管理:
public class BasePlayerManager : MonoBehaviour {
public bool didInit;
public BasePlayerManager DataManager;
public virtual void Awake()
{
didInit = false;
Init();
}
public virtual void Init()
{
DataManager = gameObject.GetComponent<BaseUserManager>();
if (DataManager == null)
DataManager = gameObject.AddComponent<BaseUserManager>();
didInit = true;
}
public virtual void GameFinished()
{
DataManager.SetIsFinished(true);
}
public virtual void GameStart()
{
DataManager.SetIsFinished(false);
}
}
基本的输入控制
public class BaseInputController : MonoBehaviour {
public bool Up;
public bool Down;
public bool Left;
public bool Right;
public bool Fire1;
public bool Slot1;
public bool Slot2;
public bool Slot3;
public bool Slot4;
public bool Slot5;
public bool Slot6;
public bool Slot7;
public bool Slot8;
public bool Slot9;
public float vert;
public float horz;
public bool shouldReapawn;
public Vector3 TEMPVec3;
private Vector3 zeroVector = new Vector3(0, 0, 0);
public virtual void CheckInput()
{
horz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");
}
public virtual float GetHorizontal()
{
return horz;
}
public virtual float GetVertical()
{
return vert;
}
public virtual bool GetFire()
{
return Fire1;
}
public bool GetRespawn()
{
return shouldReapawn;
}
public virtual Vector3 GetMovementDirectionVector()
{
TEMPVec3 = zeroVector;
if (Left || Right)
{
TEMPVec3.x = horz;
}
if (Up || Down)
{
TEMPVec3.y = vert;
}
return TEMPVec3;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/fatestay_dc/article/details/48105421