天天看点

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

运行Unity 2018 Shaders and Effects Cookbook 3rd随书源码

  • ​​前言​​
  • ​​准备工作​​
  • ​​编译书中源码​​
  • ​​Unity2021版本中修改源代码​​
  • ​​1.修改:SimpleActivatorMenu.cs文件​​
  • ​​2.修改ForcedReset.cs文件​​
  • ​​运行结果​​

前言

[下载Unity 2018 Shaders and Effects Cookbook 3rd](https://github.com/PacktPublishing/Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition)
   可以用git下载,也可以直接下载      

准备工作

1.下载Unity

​​​Unity下载和安装​​​ 2. ​​下载Unity 2018 Shaders and Effects Cookbook 3rd 源码​​

编译书中源码

1.使用Unity打开书中源码,本书作者也在官方给出了使用方法

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

2. 点击“ Unity 2018 Shaders and”

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

作者指导我们运行书中的第一个程序

D:\unity3d\book\Unity 2018 Shaders and Effects Cookbook\Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition-master\Chapters 1-11\Unity Shaders and Effects\Assets\Chapter 01\Scenes

双击运行“Chapter 1 - Starting Point.unity”

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

3.编译错误

Unity 2018 Shaders and Effects Cookbook 3rd例子运行
Unity 2018 Shaders and Effects Cookbook 3rd例子运行

4.修改源码

双击第一个错误,会自动打开你的vs2015~vs2019,看你机器上装个哪个版本的vs,我这里打开的是vs2019

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

public GUIText camSwitchButton;      

改成

public UnityEngine.UI.Text camSwitchButton;      
Unity 2018 Shaders and Effects Cookbook 3rd例子运行

5.运行源码

Unity 2018 Shaders and Effects Cookbook 3rd例子运行

Unity2021版本中修改源代码

如果你使用的是Unity2021版本,按照前面的方法修改源码依然报错

Unity 2018 Shaders and Effects Cookbook 3rd例子运行
Assets\Chapter 01\Standard Assets\Utility\ForcedReset.cs(7,26): error CS0619: 'GUITexture' is obsolete: 'GUITexture has been removed. Use UI.Image instead.'      

1.修改:SimpleActivatorMenu.cs文件

目录:E:\Unity\book\Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition\Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition-master\Chapters 1-11\Unity Shaders and Effects\Assets\Chapter 01\Standard Assets\Utility

修改为:

using System;
using UnityEngine;
using UnityEngine.UI; //在此处添加


namespace UnityStandardAssets.Utility
{
    public class SimpleActivatorMenu : MonoBehaviour
    {
        // An incredibly simple menu which, when given references
        // to gameobjects in the scene
        //public GUIText camSwitchButton;
        //public UnityEngine.UI.Text camSwitchButton;
        public Text camSwitchButton; //在此处修改
        public GameObject[] objects;


        private int m_CurrentActiveObject;


        private void OnEnable()
        {
            // active object starts from first in array
            m_CurrentActiveObject = 0;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }


        public void NextCamera()
        {
            int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;

            for (int i = 0; i < objects.Length; i++)
            {
                objects[i].SetActive(i == nextactiveobject);
            }

            m_CurrentActiveObject = nextactiveobject;
            camSwitchButton.text = objects[m_CurrentActiveObject].name;
        }
    }
}      
Unity 2018 Shaders and Effects Cookbook 3rd例子运行

2.修改ForcedReset.cs文件

目录:E:\Unity\book\Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition\Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition-master\Chapters 1-11\Unity Shaders and Effects\Assets\Chapter 01\Standard Assets\Utility

修改为:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI; //在此处添加

//[RequireComponent(typeof(GUITexture))]
[RequireComponent(typeof(Image))]    //此处修改
public class ForcedReset : MonoBehaviour
{
    private void Update()
    {
        // if we have forced a reset ...
        if (CrossPlatformInputManager.GetButtonDown("ResetObject"))
        {
            //... reload the scene
            SceneManager.LoadScene(SceneManager.GetSceneAt(0).path);
        }
    }
}      

运行结果