Version: 2019.3
Events API
Advanced Topics

Refactoring

Note
To use Bolt, which is Unity’s visual scripting solution, you must purchase it on the Unity Asset Store.

Bolt can automatically call methods, fields and properties from any custom script in your project. For example, you can create a node from a custom Player class with a TakeDamage method:

    using UnityEngine;

    public class Player : MonoBehaviour
    {
        public void TakeDamage(int damage)
        {
            // ...
        }
    }

In a flow graph, it would look like:

If you change your script and rename or remove the TakeDamage method or the Player class. For example:

    using UnityEngine;

    public class Player : MonoBehaviour
    {
        public void InflictDamage(int damage)
        {
            // ...
        }
    }

The node will turn red in the graph window and Bolt will log a warning to the console.

    Failed to define Bolt.InvokeMember:
    System.MissingMemberException: No matching member found: 'Player.TakeDamage'

Renaming Members

In order to fix it, you can reopen the script file and map the new name to the previous name with the [RenamedFrom] attribute.

It takes a single string parameter: the previous name of the member.

    using UnityEngine;
    using Ludiq;

    public class Player : MonoBehaviour
    {
        [RenamedFrom("TakeDamage")]
        public void InflictDamage(int damage)
        {
            // ...
        }
    }

It is recommended to leave the attribute in your source even after a successful recompile. This is because Bolt cannot guarantee Unity will reserialize all your graphs with the corrected name. Bolt’s [RenamedFrom] attribute works much like Unity’s own [FormerlySerializedAs] attribute in that regard.

Renaming Types

You can also rename types (including classes, structs and enums) using the [RenamedFrom] attribute.

For example, if you renamed your Player class to Character:

    using UnityEngine;
    using Ludiq;

    [RenamedFrom("Player")]
    public class Character : MonoBehaviour
    {
        [RenamedFrom("TakeDamage")]
        public void InflictDamage(int damage)
        {
            // ...
        }
    }

Note: The old name must include the namespace. In the previous example, it isn’t required because we are in the global namespace.

Events API
Advanced Topics
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961