为什么在运行游戏时,编辑器脚本中的目标为空?

时间:2019-07-14 08:32:06

标签: c# unity3d

第一次为空,后来为空,但是在运行游戏时为空。

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

[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
    private ConversationTrigger _conversationTrigger;

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();

    private void OnEnable()
    {
        _conversationTrigger = (ConversationTrigger)target;

_conversationTrigger为空,目标也为空

同样在InInspectorGUI内部,在运行游戏时,sessionsLists第一次为null,然后又不为null。

public override void OnInspectorGUI()
    {
        serializedObject.Update();

        // if there are no elements reset _currentlySelectedConversationIndex
        if (conversationsList.serializedProperty.arraySize - 1 < _currentlySelectedConversationIndex) _currentlySelectedConversationIndex = -1;

这是ConversationTrigger脚本:

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

//[ExecuteInEditMode]
public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();
    public static List<int> conversationsToPlay = new List<int>();

    [HideInInspector]
    public GameObject canvas;
    [HideInInspector]
    public bool conversationEnd;
    [HideInInspector]
    public static int conversationIndex;

    private DialogueManager dialoguemanager;

    private void Start()
    {
        conversationIndex = 0;
        dialoguemanager = FindObjectOfType<DialogueManager>();
    }

    public IEnumerator PlayConversations()
    {
        conversationEnd = false;
        var conversations = conversationsToPlay.ToArray(); // Copy the list
        conversationsToPlay.Clear(); // Immediately clear the original list

        for (int i = 0; i < conversations.Length; i++) // iterate over the array
        {
            // Now you also don't need to remove items anymore, 
            // since you already cleared the list
            yield return StartCoroutine(PlayConversation(conversations[i]));
        }
    }

    public IEnumerator PlayConversation(int index)
    {
        if (conversations.Count > 0 &&
            conversations[index].Dialogues.Count > 0)
        {
            for (int i = 0; i < conversations[index].Dialogues.Count; i++)
            {
                if (dialoguemanager != null)
                {
                    dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
                }

                while (DialogueManager.dialogueEnded == false)
                {
                    yield return null;
                }
            }

            conversationIndex = index;
            conversationEnd = true;
            canvas.SetActive(false);
            Debug.Log("Conversation Ended");
        }
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
        File.WriteAllText(@"d:\json.txt", jsonTransform);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        conversations.Clear();
        conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
    }
}

这并不影响游戏对话的正常进行,但在首次运行时,两个地方的异常都为null。

0 个答案:

没有答案