Unity3D游戏学习制作之一
Unity版本:








Unity4.3.2f1
开发语言:
C#
1.确定一下这次要实现的内容:
这次主要是要实现一个第一人称的射击类游戏,整个游戏场景里面有
主角、几个怪物、药箱、子弹,类似CS.
2.先把要做的内容罗列出来:
- 菜单页面
-
- 菜单背景贴图
- 进入游戏按钮
- 退出游戏按钮
- 游戏界面
-
- 第一人称视觉实现
- 主角射击
- 血量管理
- 药箱补血
- 怪物动画制作实现
- 怪物AI实现
-
- 站立停顿
- 走路巡逻
- 冲向主角
- 攻击主角
打开Unity -> 创建项目UnityStudy:暂时还不需要导入什么包 -> 点击create后Unity重启创建好我们的项目
调整开发界面,在Unity界面的右上角有个Layout的按钮,点开选中2 by 3:
在Project的Assets里面创建两个文件夹:
在src目录里面创建:
文件GameState:状态类,用于标记游戏的状态
public class GameState{
public const int GAME_MENU = 1; //菜单页面
public const int GAME_START = 2;//游戏开始
public const int GAME_END = 3;//游戏结束
public const int GAME_QUIT = 4;//游戏退出
}
public const int GAME_MENU = 1; //菜单页面
public const int GAME_START = 2;//游戏开始
public const int GAME_END = 3;//游戏结束
public const int GAME_QUIT = 4;//游戏退出
}
文件SceneController:这个文件主要是用来控制整个游戏的场景切换。
使用状态来控制场景描绘内容。
//当前游戏状态为菜单页面
//之所以用定义为静态,主要是为了后面的代码分离,其他的类可以
//修改游戏状态
public static int currentGameState;
//上一次的游戏状态
//避免在update的时候重复调用游戏内容
private int preGameState = -1;
//菜单界面背景贴图
public Texture2D gameMenuBg;
//菜单界面
private GameMenu gameMenu;
// Use this for initialization
void Start () {
currentGameState = GameState.GAME_MENU;
}
void OnGUI()
{
switch(currentGameState)
{
case GameState.GAME_MENU:
//在这里必须要说明
//在类GameMenu.drawGameSelectPanel();里面使用了GUI类
//如果把该方法放在OnGUI方法之外,会出现错误空指针对象:
//NullReferenceException: Object reference not set to an instance of an object
if (null == gameMenu)
{
gameMenu = new GameMenu();
}
//描绘背景
gameMenuBg = gameMenu.renderSelectGameBg(gameMenuBg);
//制作按钮
gameMenu.drawGameSelectPanel();
break;
case GameState.GAME_START:
break;
case GameState.GAME_END:
break;
case GameState.GAME_QUIT:
break;
default:
Debug.Log("未知状态错误 state="+currentGameState);
break;
}
}
//之所以用定义为静态,主要是为了后面的代码分离,其他的类可以
//修改游戏状态
public static int currentGameState;
//上一次的游戏状态
//避免在update的时候重复调用游戏内容
private int preGameState = -1;
//菜单界面背景贴图
public Texture2D gameMenuBg;
//菜单界面
private GameMenu gameMenu;
// Use this for initialization
void Start () {
currentGameState = GameState.GAME_MENU;
}
void OnGUI()
{
switch(currentGameState)
{
case GameState.GAME_MENU:
//在这里必须要说明
//在类GameMenu.drawGameSelectPanel();里面使用了GUI类
//如果把该方法放在OnGUI方法之外,会出现错误空指针对象:
//NullReferenceException: Object reference not set to an instance of an object
if (null == gameMenu)
{
gameMenu = new GameMenu();
}
//描绘背景
gameMenuBg = gameMenu.renderSelectGameBg(gameMenuBg);
//制作按钮
gameMenu.drawGameSelectPanel();
break;
case GameState.GAME_START:
break;
case GameState.GAME_END:
break;
case GameState.GAME_QUIT:
break;
default:
Debug.Log("未知状态错误 state="+currentGameState);
break;
}
}
文件GameMenu:这个类主要是用来描绘菜单界面,包括背景以及两个按钮。
public Texture2D renderSelectGameBg(Texture2D selectGameBg)
{
Texture2D copy = selectGameBg;
//把贴图的起始点放在(0,0),并设置它的宽高是场景的宽高
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), copy);
return copy;
}
public void drawGameSelectPanel()
{
if (GUI.Button(new Rect((Screen.width - 100) * 0.5f, (Screen.height - 100) * 0.5f, 100, 30), "进入游戏"))
{
SceneController.currentGameState = GameState.GAME_START;
}
if (GUI.Button(new Rect((Screen.width - 100) * 0.5f, (Screen.height - 30) * 0.5f, 100, 30), "退出游戏"))
{
SceneController.currentGameState = GameState.GAME_QUIT;
}
}
{
Texture2D copy = selectGameBg;
//把贴图的起始点放在(0,0),并设置它的宽高是场景的宽高
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), copy);
return copy;
}
public void drawGameSelectPanel()
{
if (GUI.Button(new Rect((Screen.width - 100) * 0.5f, (Screen.height - 100) * 0.5f, 100, 30), "进入游戏"))
{
SceneController.currentGameState = GameState.GAME_START;
}
if (GUI.Button(new Rect((Screen.width - 100) * 0.5f, (Screen.height - 30) * 0.5f, 100, 30), "退出游戏"))
{
SceneController.currentGameState = GameState.GAME_QUIT;
}
}
把SceneController类在Unity中拖动到主场景的摄像机上面,这时选中摄像机,在Inspector
面板中会多出一个脚本组件:
上面这个变量是要为菜单面板制作的贴图背景,现在它还是空的,
如果直接运行程序,会报一个警告。
所以找一张背景图片导入到Assets文件夹,并把它拖动到里面,这样就可以给背景附上值了。
运行项目,就会发现菜单界面制作成功了。
转载于:https://my.oschina.net/u/185335/blog/199597