NRE在词典中

时间:2017-11-15 17:05:38

标签: c# unity3d

我在Unity中创建了一个可编写脚本的对象,以便我可以轻松访问我的地下城生成器中的预制件。问题是它在构建中不起作用。它给字典提供了一个空引用异常。为什么会这样?对我来说,重要的是我只需要一个枚举键即可查找正确的墙,门或金钱预制件。有没有其他方法可以在脚本化对象中执行此操作。请注意,字典不会在检查员中工作。

using System.Collections.Generic;
    using UnityEngine;
    using System;

    [CreateAssetMenu()]
    public class DungeonData : ScriptableObject
    {
        [SerializeField]
        private GameObject mainRoomItemsPrefab;
        [SerializeField]
        private GameObject keyPrefab;
        [SerializeField]
        private MoneyPrefab[] moneyPrefabs;
        [SerializeField]
        private WallPrefab[] wallPrefabs;
        [SerializeField]
        private DoorPrefab[] doorPrefabs;
        [SerializeField]
        private GameObject roomFloorPrefab;
        [SerializeField]
        private GameObject hallwayFloorPrefab;
        public Dictionary<Money.MoneyType, GameObject> MoneyPrefabs { get; private set; }
        public Dictionary<DungeonWall.EWallTypes, WallPrefab> WallPrefabs { get; private set; }
        public Dictionary<DungeonRoomDoor.DoorTypes, GameObject> DoorPrefabs { get; private set; }

        void OnValidate()
        {
            FillMoneyPrefabDictionary();
            FillWallPrefabDictionary();
            FillDoorPrefabDictionary();
        }

        private void FillMoneyPrefabDictionary()
        {
            MoneyPrefabs = new Dictionary<Money.MoneyType, GameObject>();
            foreach (MoneyPrefab moneyPrefab in moneyPrefabs)
            {
                MoneyPrefabs.Add(moneyPrefab.Type, moneyPrefab.Prefab);
            }
        }

        private void FillWallPrefabDictionary()
        {
            WallPrefabs = new Dictionary<DungeonWall.EWallTypes, WallPrefab>();
            foreach (WallPrefab wallPrefab in wallPrefabs)
            {
                WallPrefabs.Add(wallPrefab.Type, wallPrefab);
            }
        }

        private void FillDoorPrefabDictionary()
        {
            DoorPrefabs = new Dictionary<DungeonRoomDoor.DoorTypes, GameObject>();
            foreach (DoorPrefab doorPrefab in doorPrefabs)
            {
                DoorPrefabs.Add(doorPrefab.Type, doorPrefab.Prefab);
            }
        }

        public GameObject HallwayFloorPrefab
        {
            get
            {
                return hallwayFloorPrefab;
            }
        }

        public GameObject RoomFloorPrefab
        {
            get
            {
                return roomFloorPrefab;
            }
        }

        public GameObject KeyPrefab
        {
            get
            {
                return keyPrefab;
            }
        }

        public GameObject MainRoomItemsPrefab
        {
            get
            {
                return mainRoomItemsPrefab;
            }

            set
            {
                mainRoomItemsPrefab = value;
            }
        }

        [Serializable]
        public class WallPrefab
        {
            [SerializeField]
            private DungeonWall.EWallTypes type;
            [SerializeField]
            private GameObject wall;
            [SerializeField]
            private GameObject corner;

            public DungeonWall.EWallTypes Type
            {
                get
                {
                    return type;
                }
            }

            public GameObject Wall
            {
                get
                {
                    return wall;
                }
            }

            public GameObject Corner
            {
                get
                {
                    return corner;
                }
            }
        }

        [Serializable]
        private class DoorPrefab
        {
            [SerializeField]
            private DungeonRoomDoor.DoorTypes type;
            [SerializeField]
            private GameObject prefab;

            public DungeonRoomDoor.DoorTypes Type
            {
                get
                {
                    return type;
                }
            }

            public GameObject Prefab
            {
                get
                {
                    return prefab;
                }
            }
        }

        [Serializable]
        private class MoneyPrefab
        {
            [SerializeField]
            private Money.MoneyType type;
            [SerializeField]
            private GameObject prefab;

            public Money.MoneyType Type
            {
                get
                {
                    return type;
                }
            }

            public GameObject Prefab
            {
                get
                {
                    return prefab;
                }
            }
        }


    }

0 个答案:

没有答案