美文网首页
Unity 实战(第2版)

Unity 实战(第2版)

作者: 学识浅薄的胖 | 来源:发表于2021-11-09 22:48 被阅读0次

书籍作者: 约瑟夫·霍金(Joseph Hocking) 著,蔡俊鸿 译
ISBN:9787302518952
推荐程度: 有简单编程经验的新手,强烈推荐。

在学习 Unity 3D 的过程中,从图书馆借来了此书。该书总体由浅入深,步骤清晰,文字简练,在阅读学习过程中,发现存在少许错误,特此记录。

书中大部分工程使用的 Unity 版本为 2017.1.0f3,本人使用的 Unity 版本为 2019.4.31f1c1 。经过代码实践,书中大部分函数均与 2019 一致,存在不同时,会在本文列出。


第 6 章 创建基本的 2D 平台游戏

  • 第 125 页的代码清单 6.5 玩家站在地上时,关闭重力,其中代码拼写错误,应为:
body.gravityScale = grounded && Mathf.Approximately(deltaX, 0) ? 0 : 1;

第 7 章 在游戏中放置 GUI

  • 第 148 页 7.4.1 集成事件系统,这里描述的 Messenger 脚本需要在对应网址页面复制,但目前 wiki.unity3d.com 网址无法正常打开。可参考 Manning 网站上下载的作者源码,或者参考以下代码实现:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Messenger
{
    protected static Dictionary<string, List<Action>> eventsDic = new Dictionary<string, List<Action>>();

    public static void AddListener(string gameEvent, Action action)
    {
        if (!eventsDic.ContainsKey(gameEvent))
        {
            List<Action> actions = new List<Action>();
            actions.Add(action);
            eventsDic.Add(gameEvent, actions);
        }
        else
        {
            List<Action> actions = eventsDic[gameEvent];

            if (!actions.Contains(action))
            {
                actions.Add(action);
            }
        }
    }

    public static void RemoveListener(string gameEvent, Action action)
    {
        if (eventsDic.ContainsKey(gameEvent))
        {
            List<Action> actions = eventsDic[gameEvent];

            if (actions.Contains(action))
            {
                actions.Remove(action);
            }

            if (actions.Count == 0)
            {
                eventsDic.Remove(gameEvent);
            }
        }
    }

    public static void Boardcast(string gameEvent)
    {
        if (eventsDic.ContainsKey(gameEvent))
        {
            List<Action> actions = eventsDic[gameEvent];

            foreach(Action a in actions)
            {
                a.Invoke();
            }
        }
    }
}

public class Messenger<T>
{
    protected static Dictionary<string, List<Action<T>>> eventsDic = new Dictionary<string, List<Action<T>>>();

    public static void AddListener(string gameEvent, Action<T> action)
    {
        if (!eventsDic.ContainsKey(gameEvent))
        {
            List<Action<T>> actions = new List<Action<T>>();
            actions.Add(action);
            eventsDic.Add(gameEvent, actions);
        }
        else
        {
            List<Action<T>> actions = eventsDic[gameEvent];

            if (!actions.Contains(action))
            {
                actions.Add(action);
            }
        }
    }

    public static void RemoveListener(string gameEvent, Action<T> action)
    {
        if (eventsDic.ContainsKey(gameEvent))
        {
            List<Action<T>> actions = eventsDic[gameEvent];

            if (actions.Contains(action))
            {
                actions.Remove(action);
            }

            if (actions.Count == 0)
            {
                eventsDic.Remove(gameEvent);
            }
        }
    }

    public static void Boardcast(string gameEvent, T value)
    {
        if (eventsDic.ContainsKey(gameEvent))
        {
            List<Action<T>> actions = eventsDic[gameEvent];

            foreach (Action<T> a in actions)
            {
                a.Invoke(value);
            }
        }
    }
}

第 9 章 在游戏中添加交互设施和物件

  • 第 189 页的代码清单 9.9 数据管理器将实现基本接口,其中代码拼写错误,应为:
ManagerStatus status {get;}
  • 第 194 页的代码清单 9.15,其中 name 的变量使用错误,此处应使用自定义变量 itemName,否则当物体名称不一致时,无法正确实现功能。

第 10 章 将游戏连接到互联网

  • 第 250 页中提及的渐变6面天空盒,名为 SkyBoxBlended 的 Shade 脚本需要在对应网址页面复制,但目前 wiki.unity3d.com 网址无法正常打开。可参考 Manning 网站上下载的作者源码,或者参考以下代码实现:
Shader "Custom/SkyBoxBlended" {
    Properties{
    _Tint("Tint Color", Color) = (.5, .5, .5, .5)
    _Blend("Blend", Range(0.0,1.0)) = 0.5
    _FrontTex("Front (+Z)", 2D) = "white" {}
    _BackTex("Back (-Z)", 2D) = "white" {}
    _LeftTex("Left (+X)", 2D) = "white" {}
    _RightTex("Right (-X)", 2D) = "white" {}
    _UpTex("Up (+Y)", 2D) = "white" {}
    _DownTex("Down (-Y)", 2D) = "white" {}
    _FrontTex2("2 Front (+Z)", 2D) = "white" {}
    _BackTex2("2 Back (-Z)", 2D) = "white" {}
    _LeftTex2("2 Left (+X)", 2D) = "white" {}
    _RightTex2("2 Right (-X)", 2D) = "white" {}
    _UpTex2("2 Up (+Y)", 2D) = "white" {}
    _DownTex2("2 Down (-Y)", 2D) = "white" {}
    }

        SubShader{
        Tags { "Queue" = "Background" }
        Cull Off
        Fog { Mode Off }
        Lighting Off
        Color[_Tint]
        Pass {
            SetTexture[_FrontTex] { combine texture }
            SetTexture[_FrontTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
        }
        Pass {
            SetTexture[_BackTex] { combine texture }
            SetTexture[_BackTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
        }
        Pass {
            SetTexture[_LeftTex] { combine texture }
            SetTexture[_LeftTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
        }
        Pass {
            SetTexture[_RightTex] { combine texture }
            SetTexture[_RightTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
        }
        Pass {
            SetTexture[_UpTex] { combine texture }
            SetTexture[_UpTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
        }
        Pass {
            SetTexture[_DownTex] { combine texture }
            SetTexture[_DownTex2] { constantColor(0,0,0,[_Blend]) combine texture lerp(constant) previous }
          }
    }

        Fallback "Skybox/6 Sided", 1
}

第 12 章 将各部分整合为一个完整的游戏

  • 第 277 页的代码清单 12.24 函数过时,Application.LoadLevel(name); 应为 SceneManager.LoadScene(name);

第 13 章 将游戏部署到玩家的设备

  • 第 293 页的代码清单 13.2 用于测试与浏览器通信的 WebTestObject 脚本,代码重复,删除一部分 void Start() 函数代码。

  • 第 301 页的文字描述:但是如果需要调整纹理上的压缩,请调整如图 13-6 所示设置,此处图示有误,应该为如图 13-9 所示设置

相关文章

网友评论

      本文标题:Unity 实战(第2版)

      本文链接:https://www.haomeiwen.com/subject/hfhkzltx.html