Как мне убедиться, что «это» не является нулевым внутри ondisable () при остановке игры?C#

Место общения программистов C#
Ответить
Anonymous
 Как мне убедиться, что «это» не является нулевым внутри ondisable () при остановке игры?

Сообщение Anonymous »

Код: Выделить всё

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

#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteAlways]
public class NPCSpawner : MonoBehaviour
{
[Header("NPC Settings")]
[Range(1, 100)]
public int npcCount = 5;
public GameObject npcPrefab;

[Header("References")]
public MazeGenerator mazeGenerator;
public GameObject crawlerPrefab;
public Transform startMarker;
public Transform endMarker;

[Header("Editor Settings")]
[Tooltip("If enabled, runtime-spawned NPCs will persist after exiting play mode.")]
public bool persistRuntimeChanges = false;

private List spawnedNPCs = new List();
private int lastNpcCount = -1;
private int editorNpcCount = -1;

private const string SaveFileName = "npc_maze_data.json";

private void Awake()
{
if (Application.isPlaying)
{
editorNpcCount = npcCount;
lastNpcCount = npcCount;
spawnedNPCs = GameObject.FindGameObjectsWithTag("Npc").ToList();
}
}

private void OnDisable()
{
#if UNITY_EDITOR
if (!Application.isPlaying) return;

EditorApplication.delayCall += () =>
{
if (this == null) return;

if (persistRuntimeChanges)
{
var allControlls = GameObject.FindGameObjectsWithTag("Npc");

SaveNPCsToFile(allNpcControllers);
Debug.Log($"[NPCSpawner] Persisted NPC count ({allNpcControllers.Length}) to disk.");

// Load the NPCs and update the npcCount variable
LoadNPCsFromSave();

// Explicitly set the npcCount variable to the number of NPCs loaded from the file
// This ensures the value persists in the editor
Undo.RecordObject(this, "Persist Runtime NPC Count");
this.npcCount = spawnedNPCs.Count;
EditorUtility.SetDirty(this);
}
else
{
this.npcCount = editorNpcCount;
ClearNPCs();
SpawnNPCs(this.npcCount);
Debug.Log($"[NPCSpawner] Restored original NPC count: {npcCount}");
}
};
#else
ClearNPCs();
#endif
}
< /code>
Если я не проверю, нуждается ли это ==, тогда, когда она достигнет этой строки: < /p>
Undo.RecordObject(this, "Persist Runtime NPC Count");
Я получу ошибку в редакторе, что отменить не может быть нулевым.>

Подробнее здесь: https://stackoverflow.com/questions/797 ... opping-the
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»