Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

OnCodeLoadedAttribute

class in Unity.Scripting.LifecycleManagement

/

Inherits from:Unity.Scripting.LifecycleManagement.LifecycleAttributeBase

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Marks a static method as a callback that Unity invokes after it loads code at startup or after a code reload.

Methods marked with this attribute are called after assemblies have been loaded and initialized. This happens:

  • After initial code load (Editor and Player).
  • After a code reload (Editor only).

Use this callback to:

  • Perform type discovery using reflection, or TypeCache in Editor code.
  • Initialize caches that depend on loaded types.
  • Set up references to types from other assemblies.

Unavailable state:

Unity invokes these methods early in the code load, before it restores managed objects and before OnCodeInitializingAttribute callbacks run. Your code can rely on all assemblies being loaded, so type discovery through reflection works. In Editor code, you can also use TypeCache.

However, your code must not rely on:

  • Restored objects. After a code reload, Unity hasn't yet restored the serialized data of MonoBehaviour, ScriptableObject, and EditorWindow instances. Awake and OnEnable haven't run yet.
  • The AssetDatabase. Don't load, import, find, or refresh assets from this callback.

To work with restored objects, use OnCodeInitializingAttribute instead.

This is a method attribute that can only be applied to static methods with no parameters and return type void.

Additional resources: OnCodeUnloadingAttribute, OnCodeInitializingAttribute, RuntimeInitializeOnLoadMethodAttribute, InitializeOnLoadAttribute.

using Unity.Scripting.LifecycleManagement;
using UnityEngine;
using System;
using System.Collections.Generic;

public static partial class PluginRegistry { private static Dictionary<string, Type> s_PluginTypes;

[OnCodeLoaded] static void DiscoverPlugins() { s_PluginTypes = new Dictionary<string, Type>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in assembly.GetTypes()) { var attr = type.GetCustomAttributes(typeof(GamePluginAttribute), false); if (attr.Length > 0) s_PluginTypes[type.Name] = type; } } }

public static Type GetPlugin(string name) => s_PluginTypes.TryGetValue(name, out var t) ? t : null; }

public class GamePluginAttribute : Attribute { }

Constructors

Constructor Description
OnCodeLoadedAttributeMarks the decorated static method to be invoked after all assemblies have been loaded during Editor startup or after a code reload.

Inherited Members