Unity 实战(第2版)

2021-11-09  本文已影响0人  学识浅薄的胖

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

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

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


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

body.gravityScale = grounded && Mathf.Approximately(deltaX, 0) ? 0 : 1;

第 7 章 在游戏中放置 GUI

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 章 在游戏中添加交互设施和物件

ManagerStatus status {get;}

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

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 章 将各部分整合为一个完整的游戏

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

上一篇下一篇

猜你喜欢

热点阅读