【问题标题】:How can I access and update a List from MonoBehaviour script in Editor script?如何从编辑器脚本中的 MonoBehaviour 脚本访问和更新列表?
【发布时间】:2020-01-16 14:33:37
【问题描述】:

MonoBehaviour 脚本:

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

public class PickupObjects : MonoBehaviour
{
    [SerializeField]
    public List<GameObject> pickUpObjects = new List<GameObject>();
}

还有编辑器脚本:

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

[CustomEditor(typeof(PickupObjects))]
public class PickupObjectsEditor : Editor
{
    [MenuItem("GameObject/Generate as Pickup Item", false, 30)]
    public static void GeneratePickupItems()
    {
        if (Selection.gameObjects.Length > 0)
        {
            for (int i = 0; i < Selection.gameObjects.Length; i++)
            {
                if (Selection.gameObjects[i].GetComponent<TestScript>() == null)
                {
                    Selection.gameObjects[i].AddComponent<BoxCollider>();
                    Selection.gameObjects[i].AddComponent<TestScript>();
                }

                Selection.gameObjects[i].layer = 9;
            }
        }
    }
}

我想在循环之后的编辑器脚本中将对象添加到 MonoBehaviour 中的列表中,这样我就可以在 MonoBehaviour 脚本的列表中看到现在被拾取的对象。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您正在使用Editor 冗余。至少如果它什么都不做。创建MenuItem 方法不需要它。

    您不能直接执行此操作,因为您需要以某种方式引用您的 MonoBehaviour 实例。

    一种选择是使用FindObjectOfType在场景中找到它

    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    public class PickupObjects : MonoBehaviour
    {
        [SerializeField]
        public List<GameObject> pickUpObjects = new List<GameObject>();
    
    #if UNITY_EDITOR
        // This disabled the menu item as long as there is no instance of PickupObjects in the scene.
        [MenuItem("GameObject/Generate as Pickup Item", true, 30)]
        private static bool CanGeneratePickupItems()
        {
            return FindObjectOfType<PickupObjects>();
        }
    
        [MenuItem("GameObject/Generate as Pickup Item", false, 30)]
        private static void GeneratePickupItems()
        {
            var instance = FindObjectOfType<PicupObjects>();
    
            if(!instance) return;
    
            // Clear the list first? Otherwise selection will be added to already existing entries
            // (Duplicates are filtered out later below anyway)
            instance.pickupObjects.Clear();
    
            if (Selection.gameObjects.Length <= 0) return;
    
            foreach (var selectedObj in Selection.gameObjects)
            {
                if (selectedObj.GetComponent<TestScript>() == null)
                {       
                    selectedObj.AddComponent<BoxCollider>();
                    selectedObj.AddComponent<TestScript>();               
                }
    
                selectedObj.layer = 9;
    
                if(!instance.pickupObjects.Contains(selectedObj)) instance.pickupObjects.Add(selectedObj);
            }
        }
    #endif
    }
    

    注意:在智能手机上输入,但我希望思路清晰

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多