You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Unity中EditorUserBuildSettings.activeBuildTargetChanged过时警告?

解决Unity中CS0618警告与CS0119错误的方案

你遇到的问题是因为旧的EditorUserBuildSettings.activeBuildTargetChanged事件被弃用,而你错误地直接使用了接口类型IActiveBuildTargetChanged(它不是事件,不能直接订阅)。以下是正确的修改方案:

步骤说明

  1. 引入UnityEditor.Build命名空间,因为IActiveBuildTargetChanged定义在此处
  2. 替换旧的事件订阅逻辑,使用Unity新版的回调注册API
  3. 实现构建目标变化的回调方法

修改后的完整脚本

using System;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Build; // 新增命名空间
#endif
using UnityEngine;


namespace UnityStandardAssets.CrossPlatformInput
{
    [ExecuteInEditMode]
    public class MobileControlRig : MonoBehaviour
    {
        // this script enables or disables the child objects of a control rig
        // depending on whether the USE_MOBILE_INPUT define is declared.

        // This define is set or unset by a menu item that is included with
        // the Cross Platform Input package.

#if !UNITY_EDITOR
    void OnEnable()
    {
        CheckEnableControlRig();
    }
    #endif

        private void Start()
        {
#if UNITY_EDITOR
            if (Application.isPlaying) //if in the editor, need to check if we are playing, as start is also called just after exiting play
#endif
            {
                UnityEngine.EventSystems.EventSystem system = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();

                if (system == null)
                {//the scene have no event system, spawn one
                    GameObject o = new GameObject("EventSystem");

                    o.AddComponent<UnityEngine.EventSystems.EventSystem>();
                    o.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
                }
            }
        }

#if UNITY_EDITOR

        private void OnEnable()
        {
            // 替换旧的过时事件订阅
            EditorApplication.RegisterCallback<IActiveBuildTargetChanged>(OnBuildTargetChanged);
            EditorApplication.update += Update;
        }


        private void OnDisable()
        {
            EditorApplication.UnregisterCallback<IActiveBuildTargetChanged>(OnBuildTargetChanged);
            EditorApplication.update -= Update;
        }

        // 新增构建目标变化回调方法
        private void OnBuildTargetChanged(BuildTargetChangedContext context)
        {
            CheckEnableControlRig();
        }

        private void Update()
        {
            CheckEnableControlRig();
        }
#endif


        private void CheckEnableControlRig()
        {
#if MOBILE_INPUT
        EnableControlRig(true);
        #else
            EnableControlRig(false);
#endif
        }


        private void EnableControlRig(bool enabled)
        {
            foreach (Transform t in transform)
            {
                t.gameObject.SetActive(enabled);
            }
        }
    }
}

关键修改点解析

  • 旧的EditorUserBuildSettings.activeBuildTargetChanged是直接的事件委托,新版Unity改用基于回调接口的系统
  • EditorApplication.RegisterCallback<IActiveBuildTargetChanged>(方法名)是正确的注册方式,会在构建目标变化时触发指定方法
  • BuildTargetChangedContext参数包含构建目标变化的上下文信息,这里我们不需要额外信息,直接调用CheckEnableControlRig()即可

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:47:05