- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 357字
- 2021-06-18 19:04:15
There's more...
Like any other programming language, Cg does not allow mistakes. As such, your shader will not work if you have a typo in your code. When this happens, your materials are rendered in unshaded magenta:

When a script does not compile, Unity prevents your game from being exported or even executed. Conversely, errors in shaders do not stop your game from being executed. If one of your shaders appears as magenta, it is time to investigate where the problem is. If you select the incriminated shader, you will see a list of errors displayed in its Inspector tab:

Despite showing the line that raised the error, it rarely means that this is the line that has to be fixed. The error message shown in the previous screenshot is generated by deleting the sampler2D _MainTex variable from the SubShader{} block. However, the error is raised by the first line that tries to access such a variable. Finding and fixing what's wrong with code is a process called debugging. The most common mistakes that you should check for are as follows:
- A missing bracket. If you forgot to add a curly bracket to close a section, the compiler is likely to raise errors at the end of the document, at the beginning, or in a new section.
- A missing semicolon. One of the most common mistakes, but luckily one of the easiest to spot and fix. When looking at the Error definition, first check if the line above it has a semicolon or not.
- A property that has been defined in the Properties section but has not been coupled with a variable in the SubShader{} block.
- Compared to what you might be used to in C# scripts, the floating point values in Cg do not need to be followed by an f. It's 1.0, not 1.0f.
The error messages raised by shaders can be very misleading, especially due to their strict syntactic constraints. If you are in doubt about their meaning, it's best to search the internet. The Unity forums are filled with other developers who are likely to have encountered (and fixed) your problem before.
- The Complete Rust Programming Reference Guide
- 微信公眾平臺(tái)與小程序開發(fā):從零搭建整套系統(tǒng)
- C#程序設(shè)計(jì)實(shí)訓(xùn)指導(dǎo)書
- Magento 2 Theme Design(Second Edition)
- The DevOps 2.4 Toolkit
- Natural Language Processing with Java and LingPipe Cookbook
- Unity 2018 Augmented Reality Projects
- Java Web開發(fā)實(shí)例大全(基礎(chǔ)卷) (軟件工程師開發(fā)大系)
- 深入解析Java編譯器:源碼剖析與實(shí)例詳解
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- Microsoft HoloLens By Example
- Java程序設(shè)計(jì)
- 軟件開發(fā)中的決策:權(quán)衡與取舍
- Swift編程實(shí)戰(zhàn):iOS應(yīng)用開發(fā)實(shí)例及完整解決方案
- Hadoop MapReduce v2 Cookbook(Second Edition)