开始界面

先新建一个Scene,命名为GameStart,在其中新建一个空对象,命名为UI。

在ViewController——UI文件夹中新建UIGameStart脚本,挂载在UI上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace ShootingEditor2D
{
public class UIGameStart : MonoBehaviour
{

private readonly Lazy<GUIStyle> mLableStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.label)
{
fontSize = 60,
alignment = TextAnchor.MiddleCenter
};
});

private readonly Lazy<GUIStyle> mButtonStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.button)
{
fontSize = 40,
alignment = TextAnchor.MiddleCenter
};
});

private void OnGUI()
{
var labelWidth = 600f;
var labelHeight = 100f;
var labelSize = new Vector2(labelWidth, labelHeight);
var labelPosition = new Vector2(Screen.width - labelWidth, Screen.height - labelHeight)*0.5f;
var labelRect = new Rect(labelPosition, labelSize);
GUI.Label(labelRect,"ShootingEditor2D",mLableStyle.Value);

var buttonWidth = 300f;
var buttonHeight = 100f;
var buttonSize = new Vector2(buttonWidth, buttonHeight);
var buttonPosition = new Vector2(Screen.width - buttonWidth, Screen.height - buttonHeight) * 0.5f + Vector2.up * 150f;
var buttonRect = new Rect(buttonPosition, buttonSize);
if (GUI.Button(buttonRect, "开始游戏", mButtonStyle.Value))
{
SceneManager.LoadSceneAsync("SampleScene");
}
}
}
}

结束界面

先新建一个Scene,命名为GameOver,在其中新建一个空对象,命名为UI。

在ViewController——UI文件夹中新建UIGameOver脚本,挂载在UI上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace ShootingEditor2D
{
public class UIGameOver : MonoBehaviour
{

private readonly Lazy<GUIStyle> mLableStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.label)
{
fontSize = 60,
alignment = TextAnchor.MiddleCenter
};
});

private readonly Lazy<GUIStyle> mButtonStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.button)
{
fontSize = 40,
alignment = TextAnchor.MiddleCenter
};
});

private void OnGUI()
{
var labelWidth = 600f;
var labelHeight = 100f;
var labelSize = new Vector2(labelWidth, labelHeight);
var labelPosition = new Vector2(Screen.width - labelWidth, Screen.height - labelHeight)*0.5f;
var labelRect = new Rect(labelPosition, labelSize);
GUI.Label(labelRect,"游戏结束",mLableStyle.Value);

var buttonWidth = 300f;
var buttonHeight = 100f;
var buttonSize = new Vector2(buttonWidth, buttonHeight);
var buttonPosition = new Vector2(Screen.width - buttonWidth, Screen.height - buttonHeight) * 0.5f + Vector2.up * 150f;
var buttonRect = new Rect(buttonPosition, buttonSize);
if (GUI.Button(buttonRect, "回到首页", mButtonStyle.Value))
{
SceneManager.LoadSceneAsync("GameStart");
}
}
}
}

将GameStart和GameOver场景都放在Build Settings里

修改UIGamePass脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//...
using UnityEngine.SceneManagement;

namespace ShootingEditor2D
{
public class UIGamePass : MonoBehaviour
{
//...

private void OnGUI()
{
//...
if (GUI.Button(buttonRect, "返回首页", mButtonStyle.Value))
{
SceneManager.LoadSceneAsync("GameStart");
}
}
}
}

触发结束界面

修改HurtPlayerCommand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using FrameWorkDesign;
using UnityEngine.SceneManagement;

namespace ShootingEditor2D
{
public class HurtPlayerCommand : AbstractCommand
{
private readonly int mHurt;
public HurtPlayerCommand(int hurt = 1)
{
mHurt = hurt;
}
protected override void OnExecute()
{
var playerModel = this.GetModel<IPlayerModel>();//
playerModel.HP.Value -= mHurt;

if (playerModel.HP.Value <= 0)
{
SceneManager.LoadSceneAsync("GameOver");
}

}
}
}

这样游戏开始和结束界面就做好了。

注意游戏还没有做数据重置,重新开始的话生命值会降为负数

添加RectHelper工具层

设定一个工具,用来解决绘制IMGUI界面时初始代码量又多又重复的问题。

在Scripts文件夹内新建Utility文件夹,在其中新建RectHelper文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;

namespace ShootingEditor2D
{
public static class RectHelper
{
public static Rect RectForAnchorCenter(float x, float y, float width, float height)
{
var finalX = x - width*0.5f;
var finalY = y - height*0.5f;
return new Rect(finalX, finalY, width, height);
}

public static Rect RectForAnchorCenter(Vector2 pos,Vector2 size)
{
var finalPos = pos - size * 0.5f;
return new Rect(finalPos, size);
}
}
}

静态类不能实现接口,这套框架的Utility层可以接入任何第三方插件和库,并不需要每个都继承IUtility并在Architecture注册

重构UIGameStart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace ShootingEditor2D
{
public class UIGameStart : MonoBehaviour
{

//...

private void OnGUI()
{

var labelRect = RectHelper.RectForAnchorCenter(Screen.width * 0.5f,Screen.height * 0.5f,600f, 100f);
GUI.Label(labelRect,"ShootingEditor2D",mLableStyle.Value);


var buttonRect = RectHelper.RectForAnchorCenter(Screen.width * 0.5f, Screen.height * 0.5f + 150f, 300f, 100f);
if (GUI.Button(buttonRect, "开始游戏", mButtonStyle.Value))
{
SceneManager.LoadSceneAsync("SampleScene");
}
}
}
}

UIGameOverUIGamePass都按照上面的代码重构即可,这里就不贴出代码了。