PrivateFieldWarnings

From Unify Community Wiki
Jump to: navigation, search

Author: Fredrik Ludvigsen (Steinbitglis)

Contents

Background

Because Unity's newest compiler in 2018.3 is giving warning every time you create a field that is both serializable, private and not assigned to from any script; to keep your console from being spammed with warnings, you should decorate each such field with a pragma that disables such warnings (hinting to the compiler that Unity is going to assign data to the field during deserialization, and not through scripting in runtime).

In short:

[SerializableField] private int age;

should be

#pragma warning disable 0649
[SerializableField] private int age;
#pragma warning restore 0649

but at the same time, you are responsible for actually assigning data to the variable from within the editor.

The C# compiler does no such checking, this is your own responsibility, whether the warning is disabled or not.

The following script can automatically enter the pragma statements.

Description

This script edits c# scripts (inserts the following)

#pragma warning disable 0649 // Before any private serializable field

#pragma warning restore 0649 // After

Usage

Make sure you have grep and sed available. Either in the git bash, or through some other means.

Make sure to commit your changes to your repository before running the script, so you can keep track of which changes have been written.

Run pragma_warning_writer.sh inside of your assets folder.

Bash script - pragma_warning_writer.sh

#! /bin/sh
 
#   [SerializeField] followed by newline,      whitespace and private|protected|internal until semicolon and beyond
grep -rl --include \*.cs -Pazo "\[[^\]]*SerializeField[^\]]*\](\s*\[[^\]]*\])*\s*(private|protected|internal).*;[^\n]*\n" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -E -e 's/\n[ \t]*(\[[^]]*\]\s*|\/\/[^\n]*\s*)*\[[^]]*SerializeField[^]]*\](\s*\[[^]]*\])*\s*(private|protected|internal)[^=;]*;[^\n]*/\n#pragma warning disable 0649\0\n#pragma warning restore 0649/g'
 
#   remove 'restore/disable' lines followed by 'disable/disable' lines
grep -rl --include \*.cs -Pazo "#pragma warning restore 0649((\n *)*)\n#pragma warning disable 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -E -e 's/\n#pragma warning restore 0649((\n *)*)\n#pragma warning disable 0649/\1/g'
grep -rl --include \*.cs -Pazo "#pragma warning restore 0649((\n *)*)\n#pragma warning disable 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -E -e 's/\n#pragma warning restore 0649((\n *)*)\n#pragma warning disable 0649/\1/g'
 
grep -rl --include \*.cs -Pazo "#pragma warning restore 0649\n#pragma warning restore 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -e 's/\n#pragma warning restore 0649\n#pragma warning restore 0649/\n#pragma warning restore 0649/g'
grep -rl --include \*.cs -Pazo "#pragma warning restore 0649\n#pragma warning restore 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -e 's/\n#pragma warning restore 0649\n#pragma warning restore 0649/\n#pragma warning restore 0649/g'
 
grep -rl --include \*.cs -Pazo "#pragma warning disable 0649\n#pragma warning disable 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -e 's/\n#pragma warning disable 0649\n#pragma warning disable 0649/\n#pragma warning disable 0649/g'
grep -rl --include \*.cs -Pazo "#pragma warning disable 0649\n#pragma warning disable 0649" . | xargs sed -i -e ':a' -e 'N' -e '$!ba' -e 's/\n#pragma warning disable 0649\n#pragma warning disable 0649/\n#pragma warning disable 0649/g'
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox