在这一节,实现绘制主角的功能。

笔刷显示逻辑

修改LevelEditor脚本,在其中新建一个枚举,用于存储笔刷类型

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
49
50
51
52
53
54
55
56
using System;
using UnityEngine;

namespace ShootingEditor2D
{
public class LevelEditor : MonoBehaviour
{
//...
public enum BrushType
{
Ground,
Player
}

BrushType mCurrentBrushType = BrushType.Ground;
//...
Lazy<GUIStyle> mRightButtonStyle = new Lazy<GUIStyle>(() =>
{
return new GUIStyle(GUI.skin.button)
{
fontSize = 25,
};
});
private void OnGUI()
{
//修改之前的代码添加一个判断,在Draw模式下显示当前的笔刷名称
var modeLabelRect = RectHelper.RectForAnchorCenter(Screen.width * 0.5f, 35, 300, 50);

if (mCurrentOperateMode == OperateMode.Draw)
{
GUI.Label(modeLabelRect,mCurrentOperateMode + ":" + mCurrentBrushType,mModeLableStyle.Value);//
}
else
{
GUI.Label(modeLabelRect, mCurrentOperateMode.ToString(), mModeLableStyle.Value);
}
//...

if (mCurrentOperateMode == OperateMode.Draw)//只有在绘制模式下才显示按钮
{
var groundButtonRect = new Rect(Screen.width - 110, 10, 100, 50);
if (GUI.Button(groundButtonRect, "Ground", mRightButtonStyle.Value))
{
mCurrentBrushType = BrushType.Ground;
}

var playerButtonRect = new Rect(Screen.width - 110, 70, 100, 50);
if (GUI.Button(playerButtonRect, "Player", mRightButtonStyle.Value))
{
mCurrentBrushType = BrushType.Player;
}
}
}
//...
}
}

绘制逻辑

修改LevelEditor脚本,在其中的Update代码块中直接修改绘制逻辑

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
if ((Input.GetMouseButtonDown(0) || Input.GetMouseButton(0)) && GUIUtility.hotControl == 0)
{
if (mCanDraw && mCurrentOperateMode == OperateMode.Draw)
{
if (mCurrentBrushType == BrushType.Ground)//判断当前的笔刷
{
var groundPrefab = Resources.Load<GameObject>("Ground");
var groundGameObj = Instantiate(groundPrefab,transform);
groundGameObj.transform.position = mouseWorldPos;
groundGameObj.name = "Ground";
mCanDraw = false;

}else if (mCurrentBrushType == BrushType.Player)//这里的Player依然用Ground代替
{
var groundPrefab = Resources.Load<GameObject>("Ground");
var groundGameObj = Instantiate(groundPrefab, transform);
groundGameObj.transform.position = mouseWorldPos;
groundGameObj.name = "Player";

groundGameObj.GetComponent<SpriteRenderer>().color = Color.cyan;
mCanDraw = false;
}
}else if(mCurrentObjectMouseOn && mCurrentOperateMode == OperateMode.Erase)
{
Destroy(mCurrentObjectMouseOn);
mCurrentObjectMouseOn=null;
}
}