Unity notes


  • Macro: nameof(functionName) returns a string, which is the same for just doing “functionName”, but it is safer.
    • It is safer because we can change our function name in future. If we are using a normal string, it will compile and lose the ability to know that it needs to change. On the other hand, nameof() method will fail as the parameter is no longer valid.
  • Often used specifiers:
    • For member:
      • [Header(“catagory”)]
      • [Min(value)]
      • [Max(value)]
      • [Space]
      • [SerializeField]
      • [Tooltip(“tool tip”)]
      • [HideInInspector]
    • For class:
      • [RequireComponent(typeof(class))]
      • [System.Serializable]
        • Make sure you have this if you want to have this class editable in Unity editor
    • For function:
      • [SettingsProvider]
  • To create a custom project setting, make a class from ScriptableObject with the target variable with [SerializeField]
    • In your class, create a const string with the path of the asset for you setting
    • Create functions to create/save setting
    • Create another class from SettingsProvider, with a SerializedObject as member
    • Override the OnGUI function and use EditorGUILayout.PropertyField and call FindProperty on your SerializedObject to make the property on the project setting UI, followed by ApplyModifiedPropertiesWithoutUndo
    • EditorGUILayout.PropertyField(CustomSettings.FindProperty(“SettingName”));
    • Create a function that returns your SettingsProvider and use [SettingsProvider] on the function
  • When you have C++ DLL for unity project, Unity won’t be able to recover from critical errors like how it handles it in C#
    • The crash log and dump will be stored at this location: C:\Users\<username>\AppData\Local\Temp\Unity\Editor\Crashes\
  • In the situation where there are static data for the scene and you are loading scene asynchronously. The game might have issue as the static data will be polluted.
    • The solution for this is create a buffer map, something like loading map to load between the 2 scenes. To make sure the new map will be loaded after the old map unloaded
  • OnValidate is called when member got modified in editor, and when entering play/editor mode against default value. The latter might need to be avoid
    • you can have a bool to mark if OnValidate is called or not
    • use EditorApplication.isPlaying and EditorApplication.isPlayingOrWillChangePlaymode to prevent the logic being called during play mode
  • LineRenderer can only do 1 line, but it can do a lot of nodes
    • When want to render more than 1 line, you can use multiple game object with LineRenderer component or have the 1 LineRenderer component render a long line like embroidery
  • When doing singleton, don’t do it in constructor (That is what you should do in C++, but not Unity)
    • Do it in Awake function
,

Published by


Leave a comment