[尝试构建游戏时,我在编辑器控制台中遇到错误,但在Visual Studio中却没有。我该如何解决?

问题描述 投票:0回答:1

Errors in editor

在脚本中,我正在使用SceneManagement:

using UnityEditor.SceneManagement;

但是它是灰色的,就像我不在脚本中使用它一样。但是,如果我要删除该行:

using UnityEditor.SceneManagement;

它不会在Visual Studio中显示错误,但是如果我现在尝试在编辑器中编译游戏,它将在需要SceneManagement的地方控制台中显示10个错误。

在此脚本中以前没有错误。我尝试制作游戏后就开始了。

我的部分脚本的屏幕截图。 SceneManagement为不需要或正在使用的灰色,但是它是:

Part of script

以及编辑器中的建筑物设置窗口:

Build Settings

我试图关闭Visual Studio的出口,然后从编辑器中重新打开它,双击脚本,但它没有任何改变。

完整脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;

public class ObjectsReplace : MonoBehaviour
{
    public GameObject prefabToInit;
    public bool deleteAllShaders = false;

    private const string c_doorRight = "Door_Right";
    private const string c_doorLeft = "Door_Left";
    private const string c_doorShieldFxLocked = "DoorShieldFXLocked";

    public List<GameObject> FindDoors(string[] SpecificParents)
    {
        GameObject[] doorsLeft = GameObject.FindGameObjectsWithTag(c_doorLeft);
        GameObject[] doorsRight = GameObject.FindGameObjectsWithTag(c_doorRight);

        List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList();

        if (deleteAllShaders == false)
        {
            List<GameObject> toRemove = new List<GameObject>();
            for (int i = 0; i < allDoors.Count; i++)
            {
                bool match = true;
                for (int x = 0; x < SpecificParents.Length; x++)
                {
                    match &= allDoors[i].transform.parent.name != SpecificParents[x];
                }
                if (match)
                {
                    toRemove.Add(allDoors[i]);
                }
            }
            foreach (var it in toRemove)
            {
                allDoors.Remove(it);
            }
        }

        return allDoors;
    }

    public void DeleteAllShaders()
    {
        if(deleteAllShaders == true)
        {
            UpdateOrAddShaderPrefabToDoors();
        }
    }

    public void UpdateOrAddShaderPrefabToDoors()
    {
        var allDoors = FindDoors(new string[]{ "Wall_Door_Long_01", "Wall_Door_Long_02", "Wall_Interior_Door_02" });

        HashSet<GameObject> prefabParentsOfDoorsNeedRemove = new HashSet<GameObject>();
        allDoors.ForEach(doorGameObject =>
        {
            List<GameObject> shadersChildren = new List<GameObject>();
            for (int i=0; i<doorGameObject.transform.childCount ;i++)
            {
                if (doorGameObject.transform.GetChild(i).name.StartsWith(c_doorShieldFxLocked))
                {
                    shadersChildren.Add(doorGameObject.transform.GetChild(i).gameObject);
                }
            }
            foreach (GameObject shader in shadersChildren)
            {
                GameObject outermostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(shader);
                prefabParentsOfDoorsNeedRemove.Add(outermostPrefabInstanceRoot);
            }
        });

        foreach (GameObject parent in prefabParentsOfDoorsNeedRemove)
        {
            Modify(parent, RemoveFunc);
        }

        HashSet<GameObject> prefabParentsOfDoors = new HashSet<GameObject>();
        allDoors.ForEach(doorGameObject =>
        {
            GameObject outermostPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(doorGameObject);
            prefabParentsOfDoors.Add(outermostPrefabInstanceRoot);
        });

        if (deleteAllShaders == false)
        {
            foreach (GameObject parent in prefabParentsOfDoors)
            {
                AddShaderToPrefab(parent);
            }
        }
    }

    private void AddShaderToPrefab(GameObject child)
    {
        Modify(child, AddShaderToAllDoorsFunc);
    }

    private GameObject AddShaderToAllDoorsFunc(GameObject prefab)
    {
        var children = prefab.GetComponentsInChildren<Transform>();

        //Debug.Log($"Total child count before:{children.Count()}");
        int doorsFound = 0;
        foreach (Transform trans in children)
        {
            if (trans.name == c_doorLeft || (trans.name == c_doorRight))
            {
                //Debug.Log("Found door, adding");
                GameObject shader = GetDoorShaderPrefab();

                // clone prefab and attach to parent
                Instantiate(shader, trans); 
                doorsFound++;
            }
        }

        children = prefab.GetComponentsInChildren<Transform>();
        //Debug.Log($"Total child count after:{children.Count()}, doors found:{doorsFound}");

        return prefab;
    }

    private GameObject GetDoorShaderPrefab()
    {
        string[] shieldPrefab = AssetDatabase.FindAssets(c_doorShieldFxLocked);
        //Debug.Assert(shieldPrefab.Length == 1, "Expected exactly 1 shield like this...");
        string shieldGuid = shieldPrefab[0];
        string prefabPath = AssetDatabase.GUIDToAssetPath(shieldGuid);
        GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        //Debug.Assert(prefab != null, "Expected prefab to load");
        return prefab;
    }

    private GameObject RemoveFunc(GameObject prefab)
    {
        var children = prefab.GetComponentsInChildren<Transform>();

        //Debug.Log($"child count:{children.Count()}");
        foreach (Transform trans in children)
        {
            if (trans.name.StartsWith(c_doorShieldFxLocked))
            {
                //Debug.Log("Found door shader");
                DestroyImmediate(trans.gameObject);
            }
        }

        children = prefab.GetComponentsInChildren<Transform>();
        //Debug.Log($"child count:{children.Count()}");

        return prefab;
    }

    private void Modify(GameObject parentPrefab, Func<GameObject,GameObject> modifyActionOnPrefab)
    {
        // Get the Prefab Asset root GameObject and its asset path.
        string assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(parentPrefab);

        // Load the contents of the Prefab Asset.
        GameObject prefab = PrefabUtility.LoadPrefabContents(assetPath);

        //PrefabUtility.UnpackPrefabInstance(mostPrefabInstanceRoot, PrefabUnpackMode.Completely, UnityEditor.InteractionMode.AutomatedAction);

        prefab = modifyActionOnPrefab(prefab);

        PrefabUtility.SaveAsPrefabAsset(prefab, assetPath);
        PrefabUtility.UnloadPrefabContents(prefab);
    }
}

这也是我也在mono脚本中使用的编辑器脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(ObjectsReplace))]
public class ObjectsReplaceEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        ObjectsReplace myScript = (ObjectsReplace)target;

        if (GUILayout.Button("Add"))
        {
            myScript.UpdateOrAddShaderPrefabToDoors();
        }

        GUILayout.Space(20);

        if(GUILayout.Button("Delete all shaders"))
        {
            myScript.DeleteAllShaders();
        }
    }
}

再次!这是怎么回事:

  1. 我可以在编辑器中运行游戏,没有任何问题。没有错误,没有问题。

  2. 我可以在Visual Studio中编译并保存脚本,没有任何问题,没有错误,没有问题。

  3. 我不能只在编辑器中构建游戏!!!制作游戏时,它仅在unity编辑器控制台中显示错误!在Visual Studio中仍然没有错误。我可以在编辑器中玩游戏,但不能构建游戏。

  4. 使用UnityEngine或UnityEditor的脚本中存在命名空间SceneManagement,但这不是问题。问题是无论我将UnityEngine或UnityEditor与SceneManagement一起使用是什么,SceneManagement都是灰色的,好像没有使用,但正在使用中!

  5. 如果我删除删除脚本,mono和编辑器都可以构建游戏!

  6. 我还不知道为什么在构建设置时会在构建设置中出现此错误,但这些错误未在Visual Studio中显示!

c# unity3d
1个回答
2
投票

使用UnityEngine.SceneManagement,而不是UnityEditor.SceneManagement

© www.soinside.com 2019 - 2024. All rights reserved.