3D编程作业第四章 游戏对象与图形基础
1、基本操作演练【建议做】
- 下载 Fantasy Skybox FREE, 构建自己的游戏场景
去Unity资源商店找到Fantasy Skybox FREE这个资源包,添加到自己的资源中,然后在unity打开。
在unity中,菜单->Window->Package Manager,下载资源包,导入到项目中。
直接把喜欢的Materials拖入场景中就可以了。
- 写一个简单的总结,总结游戏对象的使用
Empty:空对象,常用作载体,可用来挂载脚本或作为其他游戏对象的父类
3D Object:三维实体对象,有几何属性。游戏可见的对象之一
2D Object:二维实体对象,有几何属性,游戏中可见的对象之一
Light:光源,用来营造光影效果
Audio:音频,用以创造游戏音效
Video:视频
Camera:摄像机,用以从某个视角观察游戏世界
游戏对象可通过事先创建,脚本创建,加载预设等方式进行创建,通过改变其属性实现游戏场景的切换。
2、编程实践
牧师与魔鬼 动作分离版
【2019开始的新要求】:设计一个裁判类,当游戏达到结束条件时,通知场景控制器游戏结束
新增一个裁判类Judger、一个脚本ActionManager,将UserGUI中判断游戏是否结束以及结束时的操作全部移到Judger中,将游戏中控制小船和角色移动的动作全部移到ActionManager中。
ActionManager中的部分类
ActionManage类,动作管理基类,这是对动作对象管理器的基类,实现了所有动作的基本管理。
SSAction是所有动作的基类。SSAction继承了ScriptableObject代表SSAction不需要绑定GameObject对象,且受Unity引擎场景管理。
public class SSAction : ScriptableObject {
public bool enable = true;
public bool destroy = false;
public GameObject GameObject { get; set; }
public Transform Transform { get; set; }
public ISSActionCallback Callback { get; set; }
protected SSAction() { }
// Use this for initialization
public virtual void Start()
{
throw new System.NotImplementedException();
}
// Update is called once per frame
public virtual void Update()
{
throw new System.NotImplementedException();
}
}
SSMoveToAction(移动动作实现)
以speed的速度向target目的地移动
public class SSMoveToAction : SSAction{
public Vector3 target; // 移动目标
public float speed; // 移动速度
// 创建并返回动作的实例
public static SSMoveToAction GetSSMoveToAction(Vector3 target, float speed){
SSMoveToAction action = ScriptableObject.CreateInstance<SSMoveToAction>();
action.target = target;
action.speed = speed;
return action;
}
public override void Start(){}
// 在 Update 函数中用 Vector3.MoveTowards 实现直线运动
public override void Update(){
this.Transform.position = Vector3.MoveTowards(this.Transform.position, target, speed * Time.deltaTime);
if (this.Transform.position == target)
{
this.destroy = true;
// 完成动作后进行动作回掉
this.Callback.SSActionEvent(this);
}
}
}
SequenceAction顺序动作组合类,实现一个动作组合序列,顺序播放动作。
Judger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PriestsAndDevils;
public class Judger : MonoBehaviour{
void Start(){}
public void judge(UserGUI userGUI,GUIStyle style,GUIStyle buttonStyle){
if (userGUI.status == 1) {
GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-85, 100, 50), "You Lose!", style);
if (GUI.Button(new Rect(Screen.width/2-70, Screen.height/2, 140, 70), "Restart", buttonStyle)) {
userGUI.status = 0;
userGUI.action.restart ();
}
}
else if(userGUI.status == 2) {
GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-85, 100, 50), "You win!", style);
if (GUI.Button(new Rect(Screen.width/2-70, Screen.height/2, 140, 70), "Restart", buttonStyle)) {
userGUI.status = 0;
userGUI.action.restart ();
}
}
}
}