简体   繁体   English

我正在尝试在 Inspector 中绘制列表项,但它不起作用如何用我自己的字符串替换 element0,element1....

[英]I'm trying to draw list items in the Inspector but it's not working how can I replace the element0,element1....with my own string?

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

[CustomEditor(typeof(PickupObjects))]
public class PickupObjectsEditor : Editor
{
    private static List<GameObject> pickeditems = new List<GameObject>();
    private static bool picked = false;
    private SerializedProperty _serializedpickeditems;

    [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;
                Selection.gameObjects[i].tag = "Pickup Item";

                pickeditems.Add(Selection.gameObjects[i]);
            }

            picked = true;
        }
    }

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

        PickupObjects myTarget = (PickupObjects)target;

        DrawDefaultInspector();

        if (picked == true)
        {
            for (int i = 0; i < pickeditems.Count; i++)
            {
                myTarget.pickUpObjects.Add(pickeditems[i]);

                var item = _serializedpickeditems.GetArrayElementAtIndex(i);
                var serializedItem = new SerializedObject(item.objectReferenceValue);
                serializedItem.Update();

                EditorGUILayout.PropertyField(item, new GUIContent("Picked Item " + i + " " + item.name));
                serializedItem.ApplyModifiedProperties();
            }

            pickeditems.Clear();
            picked = false;
            serializedObject.ApplyModifiedProperties();
        }
    }

    private void OnEnable()
    {
        _serializedpickeditems = serializedObject.FindProperty("pickUpObjects");
    }
}

And the mono script和单声道脚本

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

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

I tried to use serialize and PropertyField but still it's showing the List with Element0,Element1,Element2.... And I want it to be :我尝试使用 serialize 和 PropertyField 但它仍然显示带有 Element0,Element1,Element2...的列表,我希望它是:

Picked Item Box
Picked Item Can
Picked Item Cube
Picked Item Dock_Pod

Your您的

EditorGUILayout.PropertyField(item, new GUIContent("Picked Item " + i + " " + item.name));

sits within a code block that is only executed once .位于只执行一次的代码块中。

What you see currently is actually only the list drawn by你目前看到的实际上只是

DrawDefaultInspector();

since the rest is disappeared after 1 frame/draw call.因为其余的在 1 帧/绘图调用后消失了。


You would rather want to separate the pick "method" from the drawing like eg您宁愿将选择“方法”与绘图分开,例如

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

    if (picked)
    {
        for (var i = 0; i < pickeditems.Count; i++)
        {
            // NOTE: Never mix serializedProperties and direct access/modifications on the target!
            // This messes up the marking dirty and saving these changes!
            // Rather always go through the SerializedProperties so the editor handles everything automatically
            _serializedpickeditems.arraySize++;
            _serializedpickeditems.GetArrayElementAtIndex(i).objectReferenceValue = pickeditems[i];
        }

        picked = false;
        pickeditems.Clear();
    }

    for (var i = 0; i < _serializedpickeditems.arraySize; i++)
    {
        var item = _serializedpickeditems.GetArrayElementAtIndex(i);

        // little bonus from me: Color the field if the value is null ;)
        var color = GUI.color;
        if(!item.objectReferenceValue) GUI.color = Color.red;
        {
            EditorGUILayout.PropertyField(item, new GUIContent("Picked Item " + i + " " + (item.objectReferenceValue ? item.objectReferenceValue.name : "null")));
        }
        GUI.color = color;

        // The only case you would need to go deeper here and use 
        // your new SerializedObject would be if you actually make changes
        // to these objects/components like e.g. directly allow to edit their name
    }

    serializedObject.ApplyModifiedProperties();
}

Note you should also clear the pickeditems list before adding new items:请注意,您还应该在添加新项目之前清除已选择的pickeditems列表:

[MenuItem("GameObject/Generate as Pickup Item", false, 30)]
public static void GeneratePickupItems()
{
    if (Selection.gameObjects.Length > 0)
    {
        pickeditems.Clear();

        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            if (Selection.gameObjects[i].GetComponent<Whilefun.FPEKit.FPEInteractablePickupScript>() == null)
            {
                Selection.gameObjects[i].AddComponent<BoxCollider>();
                Selection.gameObjects[i].AddComponent<Whilefun.FPEKit.FPEInteractablePickupScript>();
            }

            Selection.gameObjects[i].layer = 9;
            Selection.gameObjects[i].tag = "Pickup Item";

            pickeditems.Add(Selection.gameObjects[i]);
        }

        picked = true;
    }
}

在此处输入图片说明


In general I always recommend to use a ReorderableList !一般来说,我总是建议使用ReorderableList

It's a bit tricky at first to get into it but as soon as you have set it up it is an amazing tool.刚开始使用它有点棘手,但是一旦您设置好它,它就是一个了不起的工具。 even if you don't make it actually reorderable it is still a huge advantage to be eg able to dynamically remove an item from the middle ;)即使你不让它真正可重新排序,它仍然是一个巨大的优势,例如能够从中间动态删除一个项目;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Inspector编辑器脚本中绘制列表及其所有项目? - How can I draw a List and all it's items in Inspector editor script? 我正在尝试在 Inspector 中使用 ReorderableList 但效果不佳。 我应该如何处理它? - I'm trying to use ReorderableList in Inspector but it's not working good. How should I work with it? 将我的列表中的1个元素写成字符串,进行处理并重复此操作,直到我遍历列表为止 - Write 1 element of my list in a string, handle it and repeat this until I'm through the list 如何使用图像列表在自己的窗口之外绘制C#拖动 - How can I use an Image List to draw C# dragging outside of my own windows 我正在尝试使用我的字符串列表indecies作为我的复选框cunstruct变量的名称,但我得到错误 - I'm trying to use my string list indecies as my checkboxs cunstruct variable's names but I get error 如何在自己的类中绘制精灵? - How can I draw a sprite in it's own class? 如何用 SVG 替换 Canvas 元素? - How can I replace Canvas element with SVG? 如何在XmlDocument中用元素替换文本 - How can I replace text with element in XmlDocument 如何优化列表中的插入/替换元素<string> - How do I can optimize inserting/replacement element in to the List<string> 如何获取字符串数组列表的最后一个元素? - How can I grab the last element of a List of string arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM