基本操作演练
1. 下载 Fantasy Skybox FREE, 构建自己的游戏场景
- 方法一:在Asset Store中购买天空盒素材,在场景中直接添加下载后的天空盒素材
- 方法二
创建一个Material,把shader的值改为skybox->6side
- 拖入六张贴图素材
- 就完成天空盒了
- 下面创建地面,插入一个Terrain, 在Inspector面板调整它的形状属性。
- 插入一些树木和石头素材
- 完成一个如下的地面场景
运行游戏,试试在场景中穿梭的感觉:
编程实践
牧师与魔鬼 动作分离版
关于如何实现动作分离。我们可以将游戏对象的一个完整动作提取成几个基础动作,用类的方法来执行这些动作。同时,增加动作对象管理者,去调度游戏对象执行动作。
- 动作管理类,通过ActionManager接口管理动作集合
//动作管理接口
public class ActionManager : System.Object
{
private static ActionManager ins;
public static ActionManager get_instance()
{
if (ins == null)
{
ins = new ActionManager();
}
return ins;
}
public UAction ApplyMoveToAction(GameObject obj, Vector3 target, float speed, ActionCompleted completed)
{
MoveToAction ac = obj.AddComponent<MoveToAction>();
ac.setting(target, speed, completed);
return ac;
}
public UAction ApplyMoveToAction(GameObject obj, Vector3 target, float speed)
{
return ApplyMoveToAction(obj, target, speed, null);
}
//对象在平面上的平移动作
public UAction ApplyMoveToTrans(GameObject obj, Vector3 target, float speed, ActionCompleted completed)
{
MoveToTrans ac = obj.AddComponent<MoveToTrans>();
ac.setting(obj, target, speed, completed);
return ac;
}
public UAction ApplyMoveToTrans(GameObject obj, Vector3 target, float speed)
{
return ApplyMoveToTrans(obj, target, speed, null);
}
}
- MoveToAction类实现游戏对象动作的分配
- 方法
setting
设置target, speed和monitor的值 - 方法
update
以step位帧,对游戏对象的状态进行更新。当monitor为null时,意味着动作完成,Destroy实例
public class MoveToAction : UActionAuto
{
public Vector3 target;
public float speed;
private ActionCompleted monitor = null;
public void setting(Vector3 target, float speed, ActionCompleted monitor)
{
this.target = target;
this.speed = speed;
this.monitor = monitor;
GameSceneController.GetInstance().Set_move(true);
}
void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target, step);
if (transform.position == target)
{
GameSceneController.GetInstance().Set_move(false);
if (monitor != null)
{
monitor.OnActionCompleted(this);
}
Destroy(this);
}
}
}
- 对MoveToAction类进行进一步动作分离。我们可以把游戏对象(牧师、魔鬼、船)的动作归类为:左右平移(y方向)和上下平移(z方向)
- 我们建立MoveToTrans类实现动作的分离和组合
public class MoveToTrans : UActionAuto, ActionCompleted
{
public GameObject obj;
public Vector3 target;
public float speed;
private ActionCompleted monitor = null;
public void setting(GameObject obj, Vector3 target, float speed, ActionCompleted monitor)
{
this.obj = obj;
this.target = target;
this.speed = speed;
this.monitor = monitor;
GameSceneController.GetInstance().Set_move(true);
/*obj比target高,先移动target.z, 再移动target.y
obj比target低,先移动target.y,再移动target.z
*/
if (target.y < obj.transform.position.y)
{
Vector3 targetZ = new Vector3(target.x, obj.transform.position.y, target.z);
ActionManager.get_instance().ApplyMoveToAction(obj, targetZ, speed, this);
}
else
{
Vector3 targetY = new Vector3(target.x, target.y, obj.transform.position.z);
ActionManager.get_instance().ApplyMoveToAction(obj, targetY, speed, this);
}
}
public void OnActionCompleted(UAction action)
{
//用回调函数调用下一个动作
ActionManager.get_instance().ApplyMoveToAction(obj, target, speed, null);
}
void Update()
{
if (obj.transform.position == target)
{
GameSceneController.GetInstance().Set_move(false);
if (monitor != null)
{
monitor.OnActionCompleted(this);
}
Destroy(this);
}
}
-
设计一个裁判类,当游戏达到结束条件时,通知场景控制器游戏结束
- 在上一版游戏中,我用一个enum类型存储游戏的状态,而这一次我们用发送message的方式通知控制器游戏的状态
- 我把裁判类定义成一个public接口,其功能包括向控制器发送消息(是否继续游戏,是否终止)和检查游戏当前的状态
//裁判类,向控制器发送游戏信息 public interface Judgement { void Set_move(bool s); bool Is_moving(); void Set_message(string s); string Is_message(); }
- 发送消息
scene.Set_message("Win!"); //scene是 GameSceneController对象 scene.Set_message("Lose!");
- 查询状态
if (message != "") { if (GUI.Button(new Rect(stw(2f), sth(6f), width, height), "Your Win!")){ act.restart(); } } else { //通过接口实现动作 if (!state.Is_moving()) ......
游戏传送门
材料与渲染联系
- 从 Unity 5 开始,使用新的 Standard Shader 作为自然场景的渲染。
- 阅读官方 Standard Shader 手册 。
- 选择合适内容,如 Albedo Color and Transparency,寻找合适素材,用博客展示相关效果的呈现
Standard Shader是一个Unity的内置着色器,具有一组全面的特性。可以为物体渲染出具有 “真实世界” 的物理效果,如石头、木头、玻璃、塑料、金属等。
在Material的standard栏中,我们可以调整Albedo(反射率)
在Rendering Mode中选择transparency,调整透明度
调整Metallic的值,可以改变物体材料的金属感,smotheness表示材料的光滑程度。一般情况下金属的smothness值较高
我们制作几个材料来演示效果:
玻璃效果的球体: