游戏对象与图形基础

Apr 9, 2018


操作与总结

参考 Fantasy Skybox FREE 构建自己的游戏场景

Image text

写一个简单的总结,总结游戏对象的使用

一个scene,一个camera,camera的子对象light,一个terrain

camera的skybox组件用到了一个material

terrain中有三个paint texture,一个paint details


编程实践

牧师与魔鬼 动作分离版

为牧师与魔鬼添加了ActionManager,用来管理游戏对象的动作:上船,下船,开船

  1. SSAction

    是游戏对象所有动作的基类

  2. CCGetOnShip,CCGeOffShip,CCShipMove

    是动作的具体实现,继承自SSAction

  3. ISSActionCallback

    是动作回调接口,来通知ActionManager动作已完成

  4. SSActionManager

    用来管理游戏对象的动作,是一个基类,要挂载到游戏对象上

  5. CCActionManager

    SSActionManager的具体实现


  • CCAction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class CCGetOnShip : SSAction
{
	public FirstController sceneController; //需要和场记通信

	public static CCGetOnShip GetSSAction()  
	{  
		CCGetOnShip action = ScriptableObject.CreateInstance<CCGetOnShip>(); //让Unity 创建动作类,确保内存正确回收
		return action;  
	}  
	// Use this for initialization  
	public override void Start() //重载Start
	{  //通过导演获得当前场记
		sceneController = (FirstController)SSDirector.Instance.currentSceneController; 
	}  

	// Update is called once per frame  
	public override void Update()  
	{
        //code: How To GetOnShip
		this.destroy = true; //动作完成,销毁
		this.callback.SSActionEvent(this); //通知ActionManager动作完成
	}
}
  • CCActionManager
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
public class CCActionManager : SSActionManager, ISSActionCallback
{
	public FirstController sceneController;
	public CCGetOnShip getOn;
	public CCGetOffShip getOff;
	public CCShipMove shipMove;

	private bool flag = false;

	public void SSActionEvent(SSAction source,
		SSActionEventType events = SSActionEventType.Competeted,
		int intParam = 0,
		string strParam = null,
		System.Object objectParam = null) {}

	void Start () {
		sceneController = (FirstController)SSDirector.Instance.currentSceneController;
		sceneController.actionManager = this;  
	}  

	protected new void Update () {
		if (Input.GetMouseButtonDown(0))
			flag = true;
		else if (Input.GetMouseButtonUp(0) && flag && sceneController.Check() == 0) {
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hitInfo;

			if(Physics.Raycast(ray,out hitInfo))
			{
				GameObject gameObj = hitInfo.collider.gameObject;
				if (gameObj.name.Equals ("Ship(Clone)")) {
					shipMove = CCShipMove.GetSSAction();  
					this.RunAction(gameObj, shipMove, this);  
				} else if (gameObj.name.Equals ("Good(Clone)") || gameObj.name.Equals ("Bad(Clone)")) {
					if (gameObj.transform.position.x > 1.5 || gameObj.transform.position.x < -1.5) {
						getOn = CCGetOnShip.GetSSAction();
						this.RunAction(gameObj, getOn, this);
					} else {
						getOff = CCGetOffShip.GetSSAction();
						this.RunAction(gameObj, getOff, this);
					}
				} else
					return;
			}  
		}

		base.Update();  
	}
}

参考资料