基本操作演练

下载Fantasy Skybbox FREE,构建自己的游戏场景

直接在上一次作业的Priests and Devils游戏场景中添加天空盒和地形构建场景。首先在Unity Assets Store中将Fantasy Skybox FREE添加至“我的资源”,选择“在Unity中打开”,则网页自动打开Editor的Package manager,下载并导入Fantasy Skybox FREE的全部资源后可以看到Fantasy Skybox FREE目录出现在项目中:


其中,Cubemaps是立方图和六面体贴图,Panoramics是全景图,Scenes是天空盒的演示场景。接下来要将天空盒分配给当前场景。从菜单栏中选择Window > Rendering > Lighting,在随后出现的如下窗口中选择Environment选项卡:

将新的天空盒材质(Assets > Fantasy Skybox FREE > Panoramics > FS002 > FS002_Sunset.mat)拖放到Skybox Material字段:

最后为摄像机设置特定天空盒。在Camera对象中添加部件Rendering > Skybox,将天空盒拖入Skybox:

接下来通过GameObject > 3D Object > Terrain创建地形并修改地形的Transform属性:

然后在Inspector窗口选择Paint Terrain > Paint Texture,通过Terrain Layers > Edit Terrain Layers > Add Layers添加地面图层,选择Assets > Fantasy Skybox FREE > Scenes中的TerrainDirt:

应用于该地形后场景如图所示:

绘制各种地形和地面图层等后如图:

完成后运行游戏,场景画面如图:

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

1.概念
游戏对象本身相当于组件的容器,在没有添加组件时只是一个空容器,通过向其中添加不同的组件来调用不同的功能,一个对象挂载某组件才能拥有该组件的相关属性,才成为一个角色/一种环境/一种特殊效果。概括地说,就是游戏组件共同完成游戏对象的行为,游戏对象由它拥有的组件决定功能。
2.分类
不同类型的游戏对象能实现的功能各不相同:

Camera:摄像机,作为游戏的“眼睛”,是游戏运行时玩家观察游戏世界的媒介;
Light:光源,既可以用来照明也可以用于添加阴影;
Empty:空对象,一般作为载体挂载游戏脚本或成为其他对象的父对象等;
3D Object:包括Cube、Sphere等,是搭建游戏世界的基本元素,通过设置其Transform属性来改变位置、角度、大小等属性;
Terrain:既是组成元素又是编辑工具,既是地形本身,又能绘制地面,如造山、植树、钟花草等。

3.使用方法

  • 游戏对象的创建

    在Hierarchy窗口右键或在菜单栏选择GameObject来Create;
    在游戏脚本中利用GameObject.CreatePrimitive()函数直接创建。

  • 游戏对象的获取

    在Inspector窗口把变量声明为Public后进行赋值;
    利用函数方法获取对象,包括Find()(通过对象名称获取)、FinfWithTag()(通过标签获取单个对象)、FindGameObjectsWithTags()(通过标签获取多个对象)、FindObjectOfType()(通过类型获取单个对象)、FindObjectsOfType()(通过类型获取多个对象)。

  • 游戏对象的属性

    activeInHierarchy(游戏对象在场景中是否活跃)
    activeSelf(游戏对象本地激活状态(只读))
    isStatic(仅编辑器的API,用于指定游戏对象是否为静态)
    layer(游戏对象所在的层次)
    scene(游戏对象所属的场景)
    tag(游戏对象的标签)
    transform(附加到该游戏对象上的Transform组件)

  • 游戏对象的构造函数

    GameObject:创建一个名为name的新游戏对象;
    AddComponent:将名为className的组件类添加到游戏对象;
    BroadcastMessage:在该游戏对象或其子对象中的每个MonoBehaviour上调用名为methodName的方法;
    CompareTag:返回该游戏对象是否带有标签,1表示是;
    GetComponent:若游戏对象附加了一个组件则返回组件类型Type,若无则返回null;
    SendMessage:在该游戏对象中的每个MonoBehaviour上调用名为methodName的方法
    SendMessageUpwards:在该游戏对象中的每个MonoBehaviour以及该行为的每个祖先上调用名为nethodName的方法;
    SetActive:根据给定的true或false值激活或停用游戏对象。

  • 游戏对象的静态方法

    CreatePrimitive:使用基本的网络渲染器和适当的碰撞器创建游戏对象;
    Find:通过名称查找游戏对象并返回该对象;
    FindGameObjectsWithTag:返回带有标签的活跃的游戏对象列表,若查无游戏对象则返回空数组;
    FindWithTag:返回一个带有标签的活跃的游戏对象,若查无游戏对象则返回null。

  • 组件的添加和修改

    在Inspector窗口中手动进行添加、赋值、修改和删除等操作;
    利用函数AddComponent<>()添加组件、利用函数GetComponent<\Rigidbody>().[属性值]=来赋值或修改属性。

  • 游戏对象的运动、缩放、旋转
    实现对象运动的三种方法:

    update()函数中直接修改物体的position(子对象的position以父对象为origin)
    transform.Translate(Vector3);
    transform.position += Vector3;
    实现对象缩放的函数:
    transform.localScale = Vector(x,y,z); //在x轴方向上缩放x,在y轴方向上缩放y,在z轴方向上缩放z

    实现对象旋转的函数:

    GameObject.[NameOfObject].transform.Rotate(Vector3.up * Time.deltaTime * 10); //自转
    this.transform.RotateAround(origin.position,axis,speed *Time.deltaTime); //公转

    
    
  • 克隆和预设

    克隆游戏对象(创建一个与当前对象功能与属性完全相同但是相互独立无关联的游戏对象):
    GameObject cloneGameObject = GameObject.Instantiate(template);
    预设相当于一个类模板,同样可以快捷地创建大量相同属性和功能的对象,但是创建的各个对象之间紧密关联,发生在任一对象上的改变会同步至使用该预设的其他对象上。
    可以利用编辑器可视化添加对象,在Resource文件夹中Create > Prefab创建预设;也可以利用脚本代码直接实例化对象:

    GameObject objPrefab = (GameObject)Resources.Load("NameOfPrefab");
    Instantiate(objPrefab);
    
  • 发送广播和消息

    SendMessage("函数名",参数,SendMessageOptions) //向游戏对象自身发送消息
    BroadcastMessage("函数名",参数,SendMessageOptions) //向游戏对象自身和子对象发送消息
    SendMessageUpwards("函数名",参数,SendMessageOptions) //向游戏对象自身和父对象发送消息

  • 销毁游戏对象

    GameObject.Destroy
    Destroy(gameObject);

编程实践

牧师与魔鬼 动作分离版

设计一个裁判类,当游戏达到结束条件时,通知场景控制器游戏结束。
按照该层次改进游戏:

Models.cs中按要求新增的裁判类Judge负责判断当前游戏状态(继续/失败/胜利):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;namespace game {public interface ISceneController {void LoadResources();}public interface IUserAction {void MoveBoat();                                                              void MoveRole(RoleModel role);                         void Restart();                                         }public class SSDirector : System.Object {private static SSDirector _instance;public ISceneController CurrentScenceController { get; set; }public static SSDirector GetInstance() {if (_instance == null) {_instance = new SSDirector();}return _instance;}}public class LandModel {GameObject land;                                Vector3[] positions;                            bool sign;                                  RoleModel[] roles = new RoleModel[6];           public LandModel(string land_mark) {positions = new Vector3[] {new Vector3(7, 2, 30), new Vector3(9, 2, 30), new Vector3(11, 2, 30), new Vector3(13, 2, 30), new Vector3(15, 2, 30), new Vector3(17, 2, 30)};if (land_mark == "start") {land = Object.Instantiate(Resources.Load("Prefabs/Land", typeof(GameObject)), new Vector3(12, 0, 30), Quaternion.identity) as GameObject;sign = true;} else {land = Object.Instantiate(Resources.Load("Prefabs/Land", typeof(GameObject)), new Vector3(-12, 0, 30), Quaternion.identity) as GameObject;sign = false;}}public int GetEmptyNumber() {for (int i = 0; i < roles.Length; i++) {if (roles[i] == null)return i;}return -1;}public bool GetLandSign() { return sign;}public Vector3 GetEmptyPosition() {Vector3 pos = positions[GetEmptyNumber()];if(!sign){pos.x = -1 * pos.x;}            return pos;}public void AddRole(RoleModel role) {roles[GetEmptyNumber()] = role;}public RoleModel DeleteRoleByName(string name) { for (int i = 0; i < roles.Length; i++) {if (roles[i] != null && roles[i].GetName() == name) {RoleModel role = roles[i];roles[i] = null;return role;}}return null;}public int[] GetRoleNum() {int[] count = { 0, 0};                    for (int i = 0; i < roles.Length; i++) {if (roles[i] != null) {if (roles[i].GetSign() == 0){count[0]++;}else{count[1]++;}}}return count;}public void Reset() {roles = new RoleModel[6];}}public class BoatModel {GameObject boat;                                          Vector3[] start_empty_pos;                                    Vector3[] end_empty_pos;                                      Move move;                                                    Click click;bool sign;                                            RoleModel[] roles = new RoleModel[2];                        public BoatModel() {boat = Object.Instantiate(Resources.Load("Prefabs/Boat", typeof(GameObject)), new Vector3(4, 0, 30), Quaternion.identity) as GameObject;boat.name = "boat";start_empty_pos = new Vector3[] { new Vector3(5, 1, 30), new Vector3(3, 1, 30)};end_empty_pos = new Vector3[] { new Vector3(-3, 1, 30), new Vector3(-5, 1, 30)};move = boat.AddComponent(typeof(Move)) as Move;click = boat.AddComponent(typeof(Click)) as Click;click.SetBoat(this);sign = true; }public bool IsEmpty() {for (int i = 0; i < roles.Length; i++) {if (roles[i] != null){return false;} }return true;}public void BoatMove() {if(sign){move.MovePosition(new Vector3(-4, 0, 30));sign = false;} else{move.MovePosition(new Vector3(4, 0, 30));sign = true;}}public bool GetBoatSign(){ return sign;}public RoleModel DeleteRoleByName(string name) {for (int i = 0; i < roles.Length; i++) {if (roles[i] != null && roles[i].GetName() == name) {RoleModel role = roles[i];roles[i] = null;return role;}}return null;}public int GetEmptyNumber() {for (int i = 0; i < roles.Length; i++) {if (roles[i] == null) {return i;}}return -1;}public Vector3 GetEmptyPosition() {Vector3 pos;if (sign) {pos = start_empty_pos[GetEmptyNumber()];}else{pos = end_empty_pos[GetEmptyNumber()];}  return pos;}public void AddRole(RoleModel role) {roles[GetEmptyNumber()] = role;}public GameObject GetBoat() { return boat;}public int[] GetRoleNum() {int[] count = { 0, 0};for (int i = 0; i < roles.Length; i++) {if (roles[i] == null)continue;if (roles[i].GetSign() == 0)count[0]++;elsecount[1]++;}return count;}public void Reset() {if (!sign)BoatMove();roles = new RoleModel[2];}}public class RoleModel {GameObject role;int role_sign;             Click click;bool on_boat;                 Move move;LandModel land_model = (SSDirector.GetInstance().CurrentScenceController as Controllor).start_land;public RoleModel(string role_name) {if (role_name == "priest") {role = Object.Instantiate(Resources.Load("Prefabs/Priest", typeof(GameObject)), Vector3.zero, Quaternion.Euler(0, -90, 0)) as GameObject;role_sign = 0;} else {role = Object.Instantiate(Resources.Load("Prefabs/Devil", typeof(GameObject)), Vector3.zero, Quaternion.Euler(0, -90, 0)) as GameObject;role_sign = 1;}move = role.AddComponent(typeof(Move)) as Move;click = role.AddComponent(typeof(Click)) as Click;click.SetRole(this);}public int GetSign() { return role_sign;}public LandModel GetLandModel(){return land_model;}public string GetName() { return role.name;}public bool IsOnBoat() { return on_boat;}public void SetName(string name) { role.name = name;}public void SetPosition(Vector3 pos) { role.transform.position = pos;}public void Move(Vector3 vec) {move.MovePosition(vec);}public void GoLand(LandModel land) {  role.transform.parent = null;land_model = land;on_boat = false;}public void GoBoat(BoatModel boat) {role.transform.parent = boat.GetBoat().transform;land_model = null;          on_boat = true;}public void Reset() {land_model = (SSDirector.GetInstance().CurrentScenceController as Controllor).start_land;GoLand(land_model);SetPosition(land_model.GetEmptyPosition());land_model.AddRole(this);}}public class Move : MonoBehaviour {float move_speed = 100;                   int move_sign = 0;                        Vector3 end_pos;Vector3 middle_pos;void Update() {if (move_sign == 1) {transform.position = Vector3.MoveTowards(transform.position, middle_pos, move_speed * Time.deltaTime);if (transform.position == middle_pos)move_sign = 2;} else if (move_sign == 2) {transform.position = Vector3.MoveTowards(transform.position, end_pos, move_speed * Time.deltaTime);if (transform.position == end_pos)move_sign = 0;           }}public void MovePosition(Vector3 position) {end_pos = position;if (position.y == transform.position.y) {               move_sign = 2;} else if (position.y < transform.position.y) {     middle_pos = new Vector3(position.x, transform.position.y, position.z);move_sign = 1;} else {                                            middle_pos = new Vector3(transform.position.x, position.y, position.z);move_sign = 1;}}}public class Click : MonoBehaviour {IUserAction action;RoleModel role = null;BoatModel boat = null;public void SetRole(RoleModel role) {this.role = role;}public void SetBoat(BoatModel boat) {this.boat = boat;}void Start() {action = SSDirector.GetInstance().CurrentScenceController as IUserAction;}void OnMouseDown() {if (boat == null && role == null) return;if (boat != null)action.MoveBoat();else if(role != null)action.MoveRole(role);}}public class Judge {LandModel start_land;LandModel end_land;BoatModel boat;public Judge(LandModel start_,LandModel end_,BoatModel boat_){start_land = start_;end_land = end_;boat = boat_;}public int Check(){int start_priest = (start_land.GetRoleNum())[0];int start_devil = (start_land.GetRoleNum())[1];int end_priest = (end_land.GetRoleNum())[0];int end_devil = (end_land.GetRoleNum())[1];if (end_priest + end_devil == 6)     return 2;int[] boat_role_num = boat.GetRoleNum();if (boat.GetBoatSign() == true)         {start_priest += boat_role_num[0];start_devil += boat_role_num[1];}else                                  {end_priest += boat_role_num[0];end_devil += boat_role_num[1];}if (start_priest > 0 && start_priest < start_devil) {      return 1;}if (end_priest > 0 && end_priest < end_devil)        {return 1;}return 0;                                             }}
}

运行过程如下:

3D游戏编程与设计作业4-Skybox_牧师与魔鬼进阶版相关推荐

  1. 3D游戏编程与设计作业10

    3D游戏编程与设计作业10 环境说明 Unity3D 导航与寻路 Agent 和 Navmesh 练习 Obstacle和Off-Mesh-Link练习 P&D 过河游戏智能帮助实现 状态图 ...

  2. 3D游戏编程与设计作业09

    3D游戏编程与设计作业09 UGUI基础 画布 基础概念 测试渲染模式 UI布局基础 基本概念 锚点练习 UI组件与元素 基本概念 Mask练习 动画练习 富文本测试 简单血条 血条(Health B ...

  3. 3D游戏编程与设计 HW 4.5 牧师与恶魔(动作分离版)

    3D游戏编程与设计 HW 4.5 牧师与恶魔(动作分离版) 文章目录 3D游戏编程与设计 HW 4.5 牧师与恶魔(动作分离版) 1.作业要求 2.游戏制作 ① 设计思路 ② 设计代码 一.Actio ...

  4. 3D游戏编程与设计作业六

    改进飞碟(Hit UFO)游戏 要求 按 adapter模式 设计图修改飞碟游戏 使它同时支持物理运动与运动学(变换)运动 实现 原项目:3D编程与游戏设计作业五 仅仅对其中的一些类进行改动就能实现. ...

  5. 3D游戏编程与设计作业6-Unity实现打飞碟游戏改进版(Hit UFO)

    改进飞碟(Hit UFO)游戏 游戏内容要求 按adapter模式设计图修改飞碟游戏 使它同时支持物理运动与运动学(交换)运动 编程实践 本次作业直接在上一次打飞碟游戏的基础上增加adapter设计模 ...

  6. 3D游戏编程与设计作业四

    第四次3D编程作业 本次作业源代码链接:点击此处进行跳转 一. 基本操作演练 下载Fantasy Skybox Free,构建自己的游戏场景 场景总览图: 游玩截图: 写一个简单的总结,总结游戏对象的 ...

  7. 3D游戏编程与设计作业三

    第三次3D编程作业 本次作业源代码链接:点击此处进行跳转 1. 简答并用程序验证 游戏对象运动的本质是什么 游戏对象空间属性(坐标.旋转度.大小)的变化 请用三种以上方法,实现物体的抛物线运动 此处我 ...

  8. 3D游戏编程与设计作业2-太阳系-Priests and Devils

    简答题 一.游戏对象运动的本质是什么?   游戏对象的本质是游戏对象每一帧的由空间属性决定的坐标随时间发生改变,其中空间属性包括对象transform属性中的空间位置Position属性.旋转角度Ro ...

  9. 3D游戏编程与设计作业一

    游戏分类与热点探索 使用思维导图描述游戏的分类.(游戏分类方法特别多) 所使用的思维导图绘图工具为mindmaster. 结合手机游戏市场的下载量与排名等数据,结合游戏分类图,描述游戏市场的热点. 华 ...

最新文章

  1. 语言 OJ 高低位逆转_16年詹姆斯带队逆转勇士夺冠含金量最高?乔丹六冠含金量低?...
  2. python课后题答案第五章_Python语言程序设计(美-梁勇)第5章习题解答
  3. CodeForces - 1267A Apprentice Learning Trajectory(贪心)
  4. 贪心——你可以获得的最大硬币数目(Leetcode 1561)
  5. 定制控件消息处理函数
  6. python爬虫ip代理池_爬虫教程-Python3网络爬虫开发——IP代理池的维护
  7. ic卡消费管理系统_详述食堂消费系统的功能特点
  8. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-9.使用JWT生成用户Token回写客户端...
  9. python bootstrap 中位数_【机器学习】Bootstrap详解
  10. SLAM之PTAM学习笔记
  11. 2022年网络安全行业发展趋势
  12. nginx源码分析—reuseport的使用
  13. 网络媒体教程:人物素描
  14. python实现百度贴吧自动顶贴机器人
  15. 数据结构--算法达人修炼学习安排及方法指导
  16. 路胜与阿斯利康达成战略合作;施乐辉携手镁信健康发布PICO;张云出任药研社首席运营官 | 医药健闻...
  17. 聚沙成塔--爬虫系列(二)(python3基础语法)
  18. linux内核网络队列,Linux 内核网络协议栈 ------ 清理重传队列中函数 tcp_clean_rtx_queue...
  19. HCL_路由器_ISIS配置
  20. STM32在线烧录程序的开发

热门文章

  1. 【开发工具集】TCPView——TCP和UDP连接状态查看工具
  2. Django思维导图(思路清晰,史上最全)
  3. 计算机科学家霍金的预言,霍金的预言可能是真的!科学家刚探测到史上最强“外星人信号”...
  4. POJ 3616 DP
  5. @supports的用法
  6. 一句话概括Kubernetes架构
  7. FZU 2243 Daxia like uber
  8. matlab 点球 蒙特罗,半场-富力2-2泰达 蒙特罗点球破僵扎哈维建功
  9. 论“结婚生孩子要趁早”,否则真正的是“上有老下小”
  10. 今天买到了邱吉尔《第一次世界大战回忆录》